Jest 29 moduleNameMapper CSS mock skipped when CI=true is set
0 reputation · 22 Aug 2022, 20:46 UTC
0 reputation · 22 Aug 2022, 20:46 UTC
After upgrading a project from Jest 28 to Jest 29, imports of CSS modules in test files are handled through an explicit moduleNameMapper entry that points to a mock file, e.g. '\.css$': '<rootDir>/__mocks__/styleMock.js'. This matches the documented recommendation for static assets.
The same jest.config.js resolves the mock correctly in local runs, but in an environment where CI=true is exported, the resolution appears to be skipped and tests fail to import the mocked stylesheet. The goal is a single configuration that resolves the CSS module mock identically in local and CI runs, without maintaining two config files or conditional logic in the mapper.
The Jest 29 documentation does not clearly state whether CI=true changes moduleNameMapper resolution, mock lookup order, or cache behavior, and it is unclear whether jest-environment-jsdom behaves differently under the CI flag. Assume Jest 29.x with jsdom; exact minor version should be confirmed before relying on any answer.
moduleNameMapper resolution differently when CI=true, or is the failure caused by something else, such as module cache invalidation or rootDir resolution?jest-environment-jsdom in CI alter how stylesheet imports are resolved compared to local runs?35275 reputation · 23 Aug 2022, 03:01 UTC
In Jest 29 the moduleNameMapper resolver is identical regardless of whether CI=true is set. Jest does not alter the mapping algorithm or the order in which mocks are looked up when the CI flag is present. The failure you see is almost always caused by an environmental difference that coincides with CI, not by the flag itself.
styleMock.js vs StyleMock.js will resolve locally but break in CI..gitignore, .dockerignore, or a pipeline artifact exclusion rule.rootDir or working directory: If the CI job runs from a subdirectory or sets rootDir differently, the relative path in the mapper may point to a non-existent file.jest-cache can cause the resolver to skip a fresh mapping. This is more likely when CI runs with a clean build environment but the cache is still present from a previous run.moduleNameMapper: {
'\\.(css|less|scss)$': '/__mocks__/styleMock.js'
}
<rootDir> so the file is found regardless of the current working directory. Avoid relying on the __mocks__ adjacency convention.npx jest --showConfig --json > ci-config.json
and compare the moduleNameMapper and rootDir values with a local run.
npx jest --clearCache
This guarantees the resolver uses the latest mapping.
--experimental-vm-modules consistently in both environments or switch to CommonJS for the test files that import CSS.
--no-cache to isolate cache vs. resolution issues:
npx jest path/to/test.js --no-cache
jest-environment-jsdom alter stylesheet resolution?No. The environment only provides DOM globals (window, document, etc.). The moduleNameMapper resolver runs before the environment is instantiated, so it will apply the mock regardless of the environment chosen. If the mock is not applied, the issue lies elsewhere, as listed above.
Could you confirm the exact filename and path of styleMock.js in the repository, and whether it appears in the CI build’s file list? A case mismatch or missing file is the most common hidden cause.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.