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?
Asked
Active
Viewed 1,543 times
0
-
compile them into javascript – Max Koretskyi Jun 12 '17 at 15:26
-
@Maximus The jasmine framework do not compile them into javascript? I should write a script for it? – user2301 Jun 12 '17 at 15:45
-
I've added [the answer](https://stackoverflow.com/a/44503954/2545680), check it – Max Koretskyi Jun 12 '17 at 16:09
1 Answers
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
-
1I ran into the same issue, to solve it ensure that in the tsconfig.json "module": "commonjs" – Steverob2k Apr 23 '19 at 19:14