0

I have written unit tests using jasmine framework for angular 2 typescript application. I have not generated project using angular-cli. I installed jasmine and wrote the unit testing for angular component. How do I run this spec.ts ? I followed this https://github.com/jasmine/jasmine-npm and try to configure my project to run the tests. But this works only for spec.js test files. I tried with karma and did not work. How to setup the test environment to run tests written for typescript using jasmine?

3
  • compile them into javascript Commented Jun 12, 2017 at 15:26
  • @Maximus The jasmine framework do not compile them into javascript? I should write a script for it? Commented Jun 12, 2017 at 15:45
  • I've added the answer, check it Commented Jun 12, 2017 at 16:09

1 Answer 1

1

To get your typescript files compiled into javascript, use tsc compiler. Install it by running

$ npm i typescript
$ npm i typescript -g

And initialize tsconfig.json:

$ tsc --init

Now you can compile your files by simply running tsc in the project directory.

jasmine-npm specifies that you should run your tests by executing jasmine. So you can add the following to the package.json:

  "scripts": {
    "test": "tsc && jasmine"
  },

and execute tests by simply running:

$ npm test
Sign up to request clarification or add additional context in comments.

2 Comments

I tried but I get this error after compiling typescript to javascript import { TestBed } from '@angular/core/testing'; SyntaxError: Unexpected token import
I ran into the same issue, to solve it ensure that in the tsconfig.json "module": "commonjs"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.