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"
I've also just noticed that I can get watch mode to work in the following situation:
jest --watcha to re-run all testsI've made a repo in which you can reproduce this issue:
Your repo works fine for me with both --watch / press a and --watchAll

I've tested on 2x macOS devices and 1x windows device and --watch produces the original error on each. However, --watchAll seems to be working so I can use that as a work-around for now.
Our whole team has the same issue. Tests are running ok when we run all tests with jest command but often fail when running jest --watch. The same happens when I do jest someFilename or jest --findRelatedTests someFilename or jest --changedSince develop. The only solution found is to delete jest cache folder.
Error:
Configuration error:
Could not locate module ~/modules/product/productCard/productCardType mapped as:
C:\Users\ac\NewWeb\src\modules/product/productCard/productCardType.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^~(\\|\/)(.*)$/": "C:\Users\ac\NewWeb\src\$2"
},
"resolver": null
}
at createNoMappedModuleFoundError (node_modules/jest-resolve/build/index.js:472:17)
at Array.reduce (<anonymous>)
at SearchSource.findRelatedTests (node_modules/@jest/core/build/SearchSource.js:280:30)
at SearchSource.findTestRelatedToChangedFiles (node_modules/@jest/core/build/SearchSource.js:363:14)
My jest.config.js:
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mocks__/filePathMock.js',
},
testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$',
testPathIgnorePatterns: ['node_modules', 'e2e'],
moduleFileExtensions: [
'ts',
'tsx',
'js',
'json',
],
moduleNameMapper: {
'\\.(css|less|sass|scss)$': 'identity-obj-proxy',
'\\.svg\\?inline$': '<rootDir>/__mocks__/svgMockModule.js',
'(.*)\\.svg\\?external$': '$1.svg',
'^~(\\\\|/)(.*)$': '<rootDir>/src/$2',
},
setupFilesAfterEnv: ['<rootDir>/jest.enzyme.config.ts'],
setupFiles: [
'<rootDir>/__mocks__/shims.js',
'<rootDir>/jest.enzyme.config.ts',
'<rootDir>/__mocks__/webpackDefines.js',
'<rootDir>/__mocks__/localstorage.js',
],
cacheDirectory: '<rootDir>/.jest-cache',
coveragePathIgnorePatterns: [
'/*.svg',
'/node_modules/',
],
snapshotSerializers: [
'enzyme-to-json/serializer',
],
watchPathIgnorePatterns: ['backstop_data'],
reporters: ['default', 'jest-html-reporter', 'jest-junit'],
coverageReporters: ['cobertura', 'html'],
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig-ts-jest.json',
},
},
};
Jest version: 23.6.0 and 24.8.0
OS: Windows 10 pro
@NileDaley @jeysal I couldn't reproduce the error with the repo from https://github.com/facebook/jest/issues/8534#issuecomment-499156852 using --watch. But this works for me:
yarnkeyboard.ts file (for example add a comment)yarn test --changedSince masterConfiguration error:
Could not locate module @/types/keyboard mapped as:
C:\Users\ac\jest-watch-mode-issue\src\renderer\types/keyboard.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/@\/(.*)$/": "C:\Users\ac\jest-watch-mode-issue\src\renderer\$1"
},
"resolver": null
}
at createNoMappedModuleFoundError (C:\Users\ac\jest-watch-mode-issue\node_modules\jest-resolve\build\index.js:472:17)
at Resolver.resolveStubModuleName (C:\Users\ac\jest-watch-mode-issue\node_modules\jest-resolve\build\index.js:433:21)
at Resolver.getMockModule (C:\Users\ac\jest-watch-mode-issue\node_modules\jest-resolve\build\index.js:275:31)
at dependencies.reduce (C:\Users\ac\jest-watch-mode-issue\node_modules\jest-resolve-dependencies\build\index.js:67:45)
at Array.reduce (<anonymous>)
at DependencyResolver.resolve (C:\Users\ac\jest-watch-mode-issue\node_modules\jest-resolve-dependencies\build\index.js:53:25)
at DependencyResolver.resolveInverseModuleMap (C:\Users\ac\jest-watch-mode-issue\node_modules\jest-resolve-dependencies\build\index.js:175:30)
at DependencyResolver.resolveInverse (C:\Users\ac\jest-watch-mode-issue\node_modules\jest-resolve-dependencies\build\index.js:198:17)
at SearchSource.findRelatedTests (C:\Users\ac\jest-watch-mode-issue\node_modules\@jest\core\build\SearchSource.js:280:30)
at SearchSource.findTestRelatedToChangedFiles (C:\Users\ac\jest-watch-mode-issue\node_modules\@jest\core\build\SearchSource.js:363:14)
Alright, I can reproduce that. I believe the reason why this occurs is that .d.ts is 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 configure moduleFileExtensions: ["js", "ts", "d.ts"], but I'm not sure if this might cause other problems somewhere.
@jeysal Isn't d.ts just a subset of ts and thus included anyway, even without adding d.ts to moduleFileExtensions?
Jest does not use tsc for TS support, only ts-jest does.
Having a similar error message after deleting a source file and commenting everywhere the source file is referenced. While running jest in watch mode, this error is produced, however jest without watch, runs successfully. Somehow jest caches and wanted to imported the deleted source file ?
I have found in jest watch mode if I'm changing branches sometimes it thinks something is there that isn't there anymore, and throws a similar error message. I sometimes just have to delete node modules and re-install to get it to work.
The immediate fix here would be to configure
moduleFileExtensions: ["js", "ts", "d.ts"], but I'm not sure if this might cause other problems somewhere.
This indeed solved it @jeysal !
hi, I came from an issue in ts-jest related to jest watch mode and cache. Basically I have the scenario:
thing.ts contains:
export type Thing = {
a: number;
// b: number;
};
thing.test.ts contains:
import { Thing, doTheThing } from "./thing";
// See the Thing definition. Changing the type definition should result in a compile failure here.
export const thing: Thing = { a: 1 };
describe("Thing", () => {
it("should do the thing", () => {
expect(doTheThing()).toEqual(3);
});
});
jest caches.thing.ts by uncommenting b: number, test passes but actually it shouldn't. In the 2nd run, jest detects only thing.ts has changed but not all references to thing.ts therefore tests still pass.I wonder if this is something jest can solve. It would be great.
Guys, I am not sure if that issue stills haunting someone, but in my case, the issue was the existence of the coverage folder (seems like Jest looks at it for --watch mode) - 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.
+1 this bug. We are currently running into it. .d.ts files 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 --changedSince or --watch options
Having this failure, too. :( So annoying.
Tests pass when running jest
If I add --watch
It fails:
Configuration error:
Could not locate module src/path/to/MyModule mapped as:
/full/path/to/MyModule.
Please check your configuration for these entries:
{
"moduleNameMapper": {
"/^src\/(.*)/": "/full/path/prefix/src/$1"
},
"resolver": null
}
Tests break if I add "d.ts" as a module extension!
Using --watchAll works.
Package versions:
"jest": "24.9.0",
"jest-circus": "^24.9.0",
"ts-jest": "24.3.0",
"typescript": "3.8.3",
jest config:
const wdio = '<rootDir>/src/webdriver';
module.exports = {
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig.wdio.json',
diagnostics: false,
},
},
moduleFileExtensions: [
"ts",
"tsx",
"js",
"node",
],
// This should be the default behavior, but without this imports don't work. :(
moduleNameMapper: {
"^src/(.*)": "<rootDir>/src/$1"
},
testEnvironment: 'node',
testRunner: "jest-circus/runner",
testRegex: '.*\\.wdio(\\.[^.]+)?\\.ts$',
setupFilesAfterEnv: [`<rootDir>/build/beforeTestFile.js`],
preset: 'ts-jest',
transform: {
'^.+\\.(js|jsx|mjs)$':
'<rootDir>/node_modules/react-scripts-ts/config/jest/babelTransform.js',
},
testEnvironment: `<rootDir>/build/webdriverEnvironment.js`,
testEnvironmentOptions: { ..unrelated.. },
testTimeout: 5 * 60 * 1000, // 5 minutes in milliseconds.
testURL: 'http://localhost:3000',
verbose: true,
Most helpful comment
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.