Jest --changedSince tests ignored path patterns - jestjs

When I use --changedSince=master, jest runs all my changed files including config-files and files ignored according to testPathIgnorePatterns in the Jest config-file.
If I only have a small change plus a change in a config-file, jest --coverage --changedSince pre-hook fails because of that.
Is there a way to tell Jest to only test changed files and respect ignored patterns and config-files?

Related

Jest - run coverage report for all files in the project?

I want to run a coverage report for all files in my code repo, including those that currently don't have any tests.
I'm using this command:
jest --coverage --collectCoverageFrom='src/features/**/*.{ts,tsx}
But there are other folders i want to cover.
Is it not possible to "tell" Jest to look at all .ts and .tsx files across all folders, including nested folders?
The command you have should generate coverage report for all .ts and .jxs (was that meant to be jsx?) inside the the folders in src/features folder.
If you want to include other folders, for example everything inside /src then use
jest --coverage --collectCoverageFrom='src/**/*.{ts,jxs}'

How to Ignore testing file in specs tests folder with Jest?

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

Run specific jest project

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

Jest: Files names without .test or .spec in them

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

Keep Mocha tests alongside source files

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.

Resources