11

I have a Node.js app using TypeScript and now I want Jasmine to run tests automatically each time I make changes in .ts files. So I'm just trying to find an appropriate command to be run as npm test in command line or a package that can watch my .ts files compile them on changes and run jasmine. Does anybody know a solution for it?

1
  • What is your current test command? IE, in your package.json, what is test in the scripts block? Commented Feb 1, 2018 at 19:22

4 Answers 4

12

The easiest way I found is

installing dependencies: npm install --save-dev jasmine-ts nodemon

initializing jasmine: node_modules/.bin/jasmine-ts init

In the package.json:

"scripts": {
    "test": "nodemon --ext ts --exec 'jasmine-ts \"src/**/*.spec.ts\"'"
}

Edit: the above solution doesn't work as of the 11th of Apr, 2019. I published a modified working example at https://github.com/erosb/ts-node-jasmine-example

1
  • 2
    Actually had problem with that on Windows (couldn't find jasmine-ts). Had to split it up into test and test-watch: "test": "jasmine-ts \"src/**/jasmine-test.spec.ts\"", "test-watch": "nodemon --ext ts --exec \"npm test \""
    – mmey
    Commented May 24, 2018 at 9:24
1

This may be done with two commands launched in separate terminals. Assuming packages are installed in global mode.

First command launches TypeScript compiler in watch mode:

tsc --watch

The second starts nodemon that watches .js files and restarts on changes. Each time it executes jasmine test runner:

nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'

This solution is fast enough though it also has a drawback of running in two terminals. So it is not ideal but the best I've found so far.

As a result scripts section in package.json looks like:

"scripts": {
  /* ... */
  "watch": "tsc --watch",
  "test": "nodemon --ext js --exec 'jasmine JASMINE_CONFIG_PATH=jasmine.json'",
  "devstart": "nodemon ./bin/www"
},

devstart also works in couple with watch restarting server each time .ts files are changed (after they are compiled to .js).

1

Previously described methods either did not work, or were slow to compile code. Here is my attempt to solve this, both fast and convenient, works great for me. The only downside is that jasmine would not know which tests are affected by TS recompilation and would run all the tests.

yarn add tsc-watch --dev
yarn run tsc-watch --onSuccess "yarn run jasmine --config=jasmine.json"

NPM version:

npm -i tsc-watch
npm run tsc-watch --onSuccess "npm run jasmine --config=jasmine.json"

In my case I needed to correctly map TS paths. The full command looks like this:

yarn run tsc-watch --onSuccess \
     "node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine \
     --config=jest/jasmine.json --require=dist/jest/setup.js $targetFile"

jasmine.json

{
  "spec_dir": "dist/src",
  "spec_files": ["**/*.e2e.js", "**/*.unit.js", "**/*.spec.js", "**/*.test.js"],
  "env": {
    "random": false
  }
}

Just an example, please adjust to your needs.

tsc-watch starts a TypeScript compiler with --watch parameter, with the ability to react to successful compilation and start tests.

0

You might consider using jasmine-node. I don't think that jasmine itself has a watch option.

npm i -g jasmine-node

Assuming that your test command in your package.json scripts block is something like this:

"scripts": {
    ...
    "test": "jasmine some-directory-or-glob-pattern"
    ...
}

Use jasmine-node and add the --autotest and --watch flags to that command:

"scripts": {
    ...
    "test": "jasmine-node --autotest --watch some-directory-or-glob-pattern"
    ...
}
3
  • It looks like in this case I still need a kind of a preprocessor to precompile .ts files before running tests. Does this package have any such options? I can make nodemon to watch my files and run jasmine on changes so this is not a problem. I even can make it run tsc first to compile all .ts to .js and then run jasmine but this is not the same as compiling .ts in watch mode because it compiles all files in the project directory and that takes quite a while. I'm looking for a fast seamless way of running tests to improve my workflow. Commented Feb 1, 2018 at 20:43
  • 1
    Ah, your tests are in TypeScript. I didn't get that. Then my answer will not help you. Maybe you should check out Jest instead? Jasmine-like syntax, very nice, and there's a TS version: github.com/kulshekhar/ts-jest Commented Feb 1, 2018 at 21:13
  • Thank you, I will pay my attention to it. Commented Feb 1, 2018 at 22:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.