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?
-
compile them into javascriptMax Koretskyi– Max Koretskyi2017-06-12 15:26:57 +00:00Commented Jun 12, 2017 at 15:26
-
@Maximus The jasmine framework do not compile them into javascript? I should write a script for it?user2301– user23012017-06-12 15:45:13 +00:00Commented Jun 12, 2017 at 15:45
-
I've added the answer, check itMax Koretskyi– Max Koretskyi2017-06-12 16:09:05 +00:00Commented Jun 12, 2017 at 16:09
Add a comment
|
1 Answer
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
2 Comments
user2301
I tried but I get this error after compiling typescript to javascript
import { TestBed } from '@angular/core/testing'; SyntaxError: Unexpected token importSteverob2k
I ran into the same issue, to solve it ensure that in the tsconfig.json "module": "commonjs"