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?

user2301
  • 1,857
  • 6
  • 32
  • 63

1 Answers1

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
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • I tried but I get this error after compiling typescript to javascript `import { TestBed } from '@angular/core/testing'; SyntaxError: Unexpected token import` – user2301 Jun 13 '17 at 07:37
  • 1
    I ran into the same issue, to solve it ensure that in the tsconfig.json "module": "commonjs" – Steverob2k Apr 23 '19 at 19:14