I have a folder specs which contains test suites and I want to ignore omit.spec.js from testing temporarily.
How could I do that?
you can use testPathIgnorePatterns
npm test -- --testPathIgnorePatterns=omit
Related
I'm setting up a lerna monorepo with jest, I'm using jest's projects like so: projects: ['<rootDir>/packages/*'].
Running tests work as expected, however, I'm not sure how can I run a specific project? Say I have:
/packages
jest.config.js
/core
jest.config.js
/blog
jest.config.js
Currently jest runs tests in both packages using their specific configs, however, I'm not sure how can I tell jest to just run tests in one of those packages?
Assuming you want to do this with Jest's projects property:
As of Jest v26.1.0, you can now run selected projects with Jest by doing the following:
jest --selectProjects myproj
This will find any "project" in your jest.config.js by it's displayName value.
See:
https://github.com/facebook/jest/issues/7542
https://github.com/facebook/jest/pull/8612
https://github.com/facebook/jest/releases/tag/v26.1.0
You can call jest with the name of a test that you want to run. You can also use just parts of the path to the test, or even a regular expression. So in your case, you could run tests in the core package like this:
jest packages/core
There is currently no clean way of doing it from the CLI (see https://github.com/facebook/jest/issues/6189), but you can use https://github.com/rogeliog/jest-watch-select-projects to achieve it in watch mode
I'm migrating from Mocha/Chai to Jest, and I have a lot of test files. The file names are in the format componentTest.js and are all in subdirectories of /test. Jest doesn't seem to like that. Running jest /test doesn't find anything. Running jest /test/components/componentTest.js also says no tests found. Changing the /test director name doesn't work either. It's only when I change the name of the file to component.test.js that it finds the tests, but that's not going to work for my case without a script. What am I missing?
Jest will find any tests that are located inside a __tests__ folder as well.
And then to run your tests, just run: jest
I currently have my NodeJS source files in src and test suites in test, e.g.:
/src/bar/baz/foo.js
/test/bar/baz/foo.spec.js
This leads to awkward require statements like var foo = require('../../../src/bar/baz/foo'). And it's hard to see at a glance which source files are missing tests. I would like to instead keep my test suites in the same directory as the relevant source files:
/src/bar/baz/foo.js
/src/bar/baz/foo.spec.js
But now running mocha --recursive src causes errors as Mocha tries to run my source files as tests.
I've seen suggestions of using find or gulp to filter the file list but I find it surprising that this can't be done with plain Mocha. What's the recommended way of organising files this way?
Just pass the pattern of your test files to mocha, like:
mocha "src/**/*.spec.js"
This is going to run the .spec.js files in all subdirectories of src.
Currently Istanbul is only generating coverage for files that are used in my tests, which is okay, but seems to defeat the purpose of having coverage somewhat.
I have no Istanbul configuration, and am invoking it via npm test with the following script string:
$ istanbul cover _mocha -- -R dot --check-leaks --recursive test/
Is there a way to generate coverage for all of my source code?
Found the answer, I think I'm partly lucky that the directory structure I have chosen allows me to use this option, but my test command is now:
$ istanbul --include-all-sources cover _mocha -- -R dot --recursive test/
The --include-all-sources is the important part.
Istanbul recommends using nyc in order to check code coverage. It suggests an approach like this:
nyc mocha
After running this command, we'll get the coverage report. But there is a couple of pitfalls.
First of all, by default mocha is looking for tests in folder test. In order to override it, we have to set our own path in file mocha.opts like this:
nyc mocha --opts ./mocha.opts
And mocha.opts contains such code, for example:
spec/unit/back-end/**/*.spec.js
Another problem is that by default nyc checks coverage of only required files, that is your question is about. The solution is to set two options for nyc (I run test as an npm script so I set options in package.json). Here is the code:
"nyc": {
"all": true,
"include": [
"routes/*.js",
"routes/**/*.js",
"models/*.js"
]
},
"scripts": {
"mocha": "nyc mocha --opts ./mocha.opts",
}
Another way to achieve it is to set not include, but exclude option in order to exclude from coverage checking inappropriate files. It's strange, but the sole option all doesn't work, it requires include or exclude options. You can get more info about nyc options via nyc --help.
P.S. I don't know nyc and mocha deeply and I'm only based on my own experience.
For generating coverage for all files, have the following in your package.json
"istanbulCoverage": "nyc --reporter=lcov --reporter=text-lcov --all -x
\"./node_modules/\" -x \"./coverage/\" check-coverage --functions 90 npm run test"
Here --all flag fetches all the files in your project. If u want to exclude specific files or folders, you can use -x option.
Apart from this, if you want to specify coverage rate for you application, then use check-coverage option to specify the threshold. In my case, I've specified functions to have a coverage threshold of 90%. Otherwise, the coverage report generation fails.(i am running my karma test after coverage report generation).
Hope this helped:)
In my case, --include-all-sources did not work for me. Files that were not require-d were still excluded from the final coverage report.
Eventually, I came across this issue on the istanbul GitHub where the maintainer stated:
Yes, that is expected behavior. istanbul works by hooking require so if a file is never require-d it is as if it doesn't exist.
The only fool-proof solution that I found was to manually require all files that I wanted to include in my coverage report. I create the file include-all.test.js alongside my other test scripts and added the following bit of code:
var glob = require( 'glob' )
var path = require( 'path' );
glob.sync( './path/to/js/code/*.js' ).forEach( function( file ) {
// we don't care about errors here, we just want to require the file
try {
require( path.resolve( file ) );
} catch(e) {}
});
This will absolutely ensure that your untested files are included in the istanbul coverage report.
I'm using jasmine-node to test my Meteor application and I want to use the auto-test feature so I don't have to rerun the tests all the time by myself.
My meteor application folder structure is like this:
server
foo.coffee
tests
foo.spec.coffee
And with the spec file I want to test code which is located in foo.coffee. I start jasmine-node with this args:
jasmine-node ./ --autotest --coffee --test-dir tests
And now I would assume that the autotest feature will react on all changes in the root folder but it just reacts on changes in the test folder. And I can't start it in the root folder because I get an error in the .meteor files (and I don't want to have jasmine testing/including the meteor code anyway).
So I want to have jasmine rerun the tests even if I change code in the server folder. How can I achieve that?
Use the --watch parameter along with --autotest and specify the directories that contain whatever files you want to have watched.