I have a typescript project containing multiple Jest test suites. If I run all of the suites together using
npm test
then all the tests are executed.
I can filter the tests that run by using a command like
npm test -- LayoutView
Jest then only executes the tests from modules whose names contain the text LayoutView. We get a response
> jest --config=./configs/jest.config.ts "LayoutView"
PASS tests/actions/LayoutViewHide.test.ts
PASS tests/actions/LayoutViewCopy.test.ts
PASS tests/actions/LayoutViewMove.test.ts
Test Suites: 3 passed, 3 total
Tests: 47 passed, 47 total
Snapshots: 0 total
Time: 6.724 s
Ran all test suites matching /LayoutView/i
However if I further restrict the pattern so that it only matches a single test file like this then it fails.
npm test -- LayoutViewH
giving an error like this:
> jest --config=./configs/jest.config.ts "LayoutViewH"
FAIL tests/actions/LayoutViewHide.test.ts
● Test suite failed to run
Cannot find module 'source-map-support' from 'node_modules/@cspotcode/source-map-support/source-map-support.js'
at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:306:11)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.095 s, estimated 5 s
Ran all test suites matching /LayoutViewH/i.
It seems that whenever I try to test from a single suite it fails, whenever the test covers multiple files then it succeeds. This is preventing me from debugging a failed test, since VSCode is trying to run a single test, and fails with the same error message I can see in the command line.
Looking at Jest - Cannot find module 'source-map' from 'source-map-support.js' I checked the moduleDirectories
property in my jest.config.ts
file. It reads
moduleDirectories: ['node_modules', 'src'],
I need to have 'src' here since I am using absolute paths within my code, relative to a directory called src below my root directory.
Besides if I have the includes miss-configured, why does it work when testing several suites at once? Any idea what might be causing this?
I am running:
- node --version: v16.15.1
- Jest version: 26.6.3
My jest.configure.ts file reads
module.exports = {
rootDir: '../',
roots: [ '<rootDir>/tests/' ],
transform: {
'.+\\.(css|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.tsx?$': 'ts-jest',
'^.+\\.js$': '<rootDir>/node_modules/babel-jest'
},
testRegex: '\\.test\\.(ts|tsx|js)$',
moduleFileExtensions: ['ts', 'tsx', 'js'],
moduleDirectories: ['node_modules', 'src'],
testEnvironment: 'node',
};
The relevant part of the "package.json" file contains
{
"scripts": {
"test": "jest --config=./configs/jest.config.ts"
}
}