Specifying the location of mocha.opts when running mocha with nyc? - javascript

Is it possible to specify the location of mocha.opts when running nyc mocha? I'd like to have it in the root project folder. I tried this in my npm script:
"scripts": {
"test": "nyc mocha --config ./mocha.opts"
},
That did not work. I did get a test case working for the setup described here however, with mocha.opts in the test directory.

mocha --opts <path_to_opts_file>

Related

How to get an overview of all files in a directory using Istanbul / nyc?

I just started using nyc and Istanbul to generate a code coverage report. The fist thing I notest was that the coverage report only shows the coverage for files that are addressed in the tests. It does not show any coverage for files that are not tested at all.
If I have all my files in a directory called "src", is it possible to generate a report that includes all files in that directory (including those that have 0 tests so far)?
Currently my NPM scripts are (I am using Mocha and Chai).
"test": "mocha test/**/*.spec.js",
"nyc": "nyc --reporter=html npm test"
Try update package.json like:
"test": "mocha test/**/*.spec.js",
"coverage": "nyc --reporter=html npm test",
"nyc": {
"all": true
}

Why I am not able to use mocha through nodemon?

I am trying to run test cases using mocha framework. I am trying to run it through nodemon, but getting an error.
I have installed nodemon already through npm install nodemon and similarly with mocha.
How do I run my project through nodemon so that my test cases show the result using mocha framework?
In the Command Prompt, my test cases are not running for whatever I do with nodemon.
Lets assume you have an entry like following in your package.json -
scripts": {
"start:test": "mocha test/ --recursive --exit"
}
To run the mocha test using nodemon please use the following command:
nodemon --exec "npm run start:test"
"test": "nodemon --exec 'mocha -R min'"
then: npm run test

Use absolute paths with Jest

I have create an app with create-react-app.
I have set in .env my NODE_PATH=src/
All works fine when I launch react-script start but when I launch react-script test I have an error: import is not found.
In my package.json :
"test": "jest --colors --coverage test"
Edit
It work fine with:
"jest": {
"modulePaths": ["src/"],
"testURL": "http://localhost",
"jest": "^22.4.4"
}
You should execute your tests with
npm test
instead of directly using react-scripts here. Jest is automatically configured while using project created by create-react-app.
If you want to directly call jest instead using the bundling set in react-scripts, you have to call:
"test": "./node_modules/jest/bin/jest.js --colors --coverage test"

How to run Jasmine tests in watch mode for TypeScript

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?
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
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).
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"
...
}
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.

How to transform a npm test script to a grunt task?

I have the following script with my nodeJS.
"scripts": {
"test": "babel-tape-runner 'test/**/*.js' | tap-notify | tap-diff "
}
If I run my tests with npm test everything runs properly, but I would prefer to have it all in a gruntfile. Is there a way to configure grunt to run the tests?

Categories

Resources