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)

Most upvoted comments

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.

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.

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 jest works 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 --watchAll works 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.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

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.

I’ve also just noticed that I can get watch mode to work in the following situation:

  1. There are no changes since the last commit
  2. I run jest --watch
  3. Jest says that there are no changes since the last commit
  4. I press a to re-run all tests
  5. A full test run will be performed
  6. I change a file
  7. A test run will be triggered