Skip to content

Commit 2726e51

Browse files
committed
fix(test): Stabilize Jest ESM configuration for CI
Resolves persistent `ReferenceError: exports is not defined` errors encountered during unit tests (`unit-test (20.x)` job) in the GitHub Actions CI environment. These errors occurred specifically when importing `cli-testing-library` in the global Jest setup file (`test/jest-setup.ts`), despite tests passing locally with the same Node.js version (v20.19.0). After iterative testing, the following Jest configuration combination was identified as necessary to ensure consistent ESM handling and test success in both local and CI environments: - Set preset to `ts-jest/presets/default-esm` for stricter ESM rules. - Configured `transformIgnorePatterns` to ensure Jest transforms specific ESM dependencies (`cli-testing-library`, `@clack`, `cleye`) within `node_modules`. - Expanded the `transform` pattern (`^.+\\.(ts|tsx|js|jsx|mjs)$`) to explicitly cover various script types handled by `ts-jest`. - Included explicit `tsconfig` overrides (`module: 'ESNext'`, `target: 'ES2022'`) within the `ts-jest` transform options to resolve potential environment discrepancies. - Retained `moduleNameMapper` for `.js` imports for reliable module resolution. - Ensured `cli-testing-library` imports remain in the global setup (`test/jest-setup.ts`). - Removed test cache clearing from the `test:unit` script in `package.json`. This configuration now passes reliably across environments.
1 parent da2742e commit 2726e51

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ uncaughtExceptions.log
1111
src/*.json
1212
.idea
1313
test.ts
14-
notes.md
14+
notes.md
15+
.nvmrc

jest.config.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,32 @@ const config: Config = {
99
testTimeout: 100_000,
1010
coverageProvider: 'v8',
1111
moduleDirectories: ['node_modules', 'src'],
12-
preset: 'ts-jest/presets/js-with-ts-esm',
12+
preset: 'ts-jest/presets/default-esm',
1313
setupFilesAfterEnv: ['<rootDir>/test/jest-setup.ts'],
1414
testEnvironment: 'node',
1515
testRegex: ['.*\\.test\\.ts$'],
16-
transformIgnorePatterns: ['node_modules/(?!cli-testing-library)'],
16+
transformIgnorePatterns: [
17+
'node_modules/(?!(cli-testing-library|@clack|cleye)/.*)'
18+
],
1719
transform: {
18-
'^.+\\.(ts|tsx)$': [
20+
'^.+\\.(ts|tsx|js|jsx|mjs)$': [
1921
'ts-jest',
2022
{
2123
diagnostics: false,
22-
useESM: true
24+
useESM: true,
25+
tsconfig: {
26+
module: 'ESNext',
27+
target: 'ES2022'
28+
}
2329
}
2430
]
31+
},
32+
// Fix Haste module naming collision
33+
modulePathIgnorePatterns: [
34+
'<rootDir>/test/e2e/prompt-module/data/'
35+
],
36+
moduleNameMapper: {
37+
'^(\\.{1,2}/.*)\\.js$': '$1'
2538
}
2639
};
2740

test/jest-setup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import 'cli-testing-library/extend-expect'
2-
import { configure } from 'cli-testing-library'
31
import { jest } from '@jest/globals';
2+
import 'cli-testing-library/extend-expect';
3+
import { configure } from 'cli-testing-library';
44

55
global.jest = jest;
66

77
/**
88
* Adjusted the wait time for waitFor/findByText to 2000ms, because the default 1000ms makes the test results flaky
99
*/
10-
configure({ asyncUtilTimeout: 2000 })
10+
configure({ asyncUtilTimeout: 2000 });

0 commit comments

Comments
 (0)