From 2726e51c2a95ebc5ed30b82800b38fa5bd75de2f Mon Sep 17 00:00:00 2001 From: Jethro Yu Date: Tue, 15 Apr 2025 14:54:40 +0800 Subject: [PATCH] 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. --- .gitignore | 3 ++- jest.config.ts | 21 +++++++++++++++++---- test/jest-setup.ts | 6 +++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index e20df5e..1e5999b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ uncaughtExceptions.log src/*.json .idea test.ts -notes.md \ No newline at end of file +notes.md +.nvmrc \ No newline at end of file diff --git a/jest.config.ts b/jest.config.ts index ee0d397..e100760 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -9,19 +9,32 @@ const config: Config = { testTimeout: 100_000, coverageProvider: 'v8', moduleDirectories: ['node_modules', 'src'], - preset: 'ts-jest/presets/js-with-ts-esm', + preset: 'ts-jest/presets/default-esm', setupFilesAfterEnv: ['/test/jest-setup.ts'], testEnvironment: 'node', testRegex: ['.*\\.test\\.ts$'], - transformIgnorePatterns: ['node_modules/(?!cli-testing-library)'], + transformIgnorePatterns: [ + 'node_modules/(?!(cli-testing-library|@clack|cleye)/.*)' + ], transform: { - '^.+\\.(ts|tsx)$': [ + '^.+\\.(ts|tsx|js|jsx|mjs)$': [ 'ts-jest', { diagnostics: false, - useESM: true + useESM: true, + tsconfig: { + module: 'ESNext', + target: 'ES2022' + } } ] + }, + // Fix Haste module naming collision + modulePathIgnorePatterns: [ + '/test/e2e/prompt-module/data/' + ], + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1' } }; diff --git a/test/jest-setup.ts b/test/jest-setup.ts index 29280f7..cd7b699 100644 --- a/test/jest-setup.ts +++ b/test/jest-setup.ts @@ -1,10 +1,10 @@ -import 'cli-testing-library/extend-expect' -import { configure } from 'cli-testing-library' import { jest } from '@jest/globals'; +import 'cli-testing-library/extend-expect'; +import { configure } from 'cli-testing-library'; global.jest = jest; /** * Adjusted the wait time for waitFor/findByText to 2000ms, because the default 1000ms makes the test results flaky */ -configure({ asyncUtilTimeout: 2000 }) +configure({ asyncUtilTimeout: 2000 });