jest: Tests fail to run in watch mode
π Bug Report
My tests run just fine when using the jest command. However, as soon as I run jest --watch I get the following error.
Configuration error:
Could not locate module @/types/keyboard mapped as:
/Users/niledaley/Code/project/src/renderer/types/keyboard.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/@\/(.*)$/": "/Users/niledaley/Code/project/src/renderer/$1"
},
"resolver": null
}
Hereβs a tree of my src/renderer/types folder:
src/renderer/types
βββ api.d.ts
βββ cart.d.ts
βββ checkout.d.ts
βββ filters.d.ts
βββ index.d.ts
βββ keyboard.d.ts
βββ modules.d.ts
βββ payment.d.ts
βββ product.d.ts
βββ productlist.d.ts
βββ settings.d.ts
βββ thunk.d.ts
My jest.config.js file contains the following:
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
'\\.(css|scss)$': 'identity-obj-proxy',
// Tell jest to look in the src dir when we use @/
'@/(.*)$': '<rootDir>/src/renderer/$1',
},
setupFilesAfterEnv: ['./jest.setup.js'],
globals: {
window: {},
},
watchPathIgnorePatterns: [
'<rootDir>/node_modules/',
'<rootDir>/src/renderer/utils/setNativeValue.ts',
],
coveragePathIgnorePatterns: [
'<rootDir>/node_modules/',
'<rootDir>/src/renderer/utils/setNativeValue.ts',
],
};
And my tsconfig.json file looks like this:
{
"extends": "./node_modules/electron-webpack/tsconfig-base.json",
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"jsx": "react",
"baseUrl": ".",
"paths": {
"@/*": ["src/renderer/*"],
"@public/*": ["public/*"]
},
"module": "es2015",
"moduleResolution": "node",
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"sourceMap": true,
"target": "esnext"
},
"exclude": ["node_modules"]
}
Jest version: β^24.5.0β, ts-jest version: β^24.0.0β, typescript version: β^3.4.5β
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 9
- Comments: 22 (4 by maintainers)
Alright, I can reproduce that. I believe the reason why this occurs is that
.d.tsis not a known extension. When running the test this is not a problem because the import is just used as a type and thus stripped out, but when using something like--changedSince, the problem occurs when Jest looks at the module dependencies to find affected tests. The immediate fix here would be to configuremoduleFileExtensions: ["js", "ts", "d.ts"], but Iβm not sure if this might cause other problems somewhere.Iβve tested on 2x macOS devices and 1x windows device and
--watchproduces the original error on each. However,--watchAllseems to be working so I can use that as a work-around for now.Very strange failure trying to use a shim on JSON for BigInt. I put this shim in setupFiles:
BigInt.prototype.toJSON = function() { return this.toString() };Running just
jestworks great if I have a test failure on.toEquals(BigInt(100), every time - the shim prevents the classic βDo not know how to serialize BigIntβ error.Running
jest --watchAllworks for a while then fails and can fail catastrophically (uncaught promise rejection) and it gets stuck. Quite strange.This blog posts describes almost exactly the same issue in some detail: https://realonetech.wordpress.com/2021/03/24/jest-custom-matcher-in-typescript/
+1 this bug. We are currently running into it.
.d.tsfiles and coverage directories seem unrelated to us (although we are using TS). From what we can determine it has something to do with the diff generated by the--changedSinceor--watchoptionsGuys, I am not sure if that issue stills haunting someone, but in my case, the issue was the existence of the
coveragefolder (seems like Jest looks at it for--watchmode) - it should also apply to whatever location you set the coverage reports to be stored.If the modules described there are somehow different from your current file structure, there is a chance to see this annoying issue. Deleting the forenamed folder did the trick and made the watch mode work great as usual.
Iβve also just noticed that I can get watch mode to work in the following situation:
jest --watchato re-run all tests