1+ import { pathsToModuleNameMapper } from 'ts-jest' ;
2+ import { readFileSync } from 'fs' ;
3+ import { fileURLToPath } from 'url' ;
4+ import { dirname } from 'path' ;
5+
6+ const __filename = fileURLToPath ( import . meta. url ) ;
7+ const __dirname = dirname ( __filename ) ;
8+
9+ // Read and parse tsconfig.json
10+ const tsconfig = JSON . parse ( readFileSync ( './tsconfig.json' , 'utf8' ) ) ;
11+ const compilerOptions = tsconfig . compilerOptions ;
12+
113export default {
2- preset : 'ts-jest' ,
14+ // Use the default ESM preset for ts-jest
15+ preset : 'ts-jest/presets/default-esm' ,
16+
17+ // Test environment
318 testEnvironment : 'node' ,
19+
20+ // Test discovery
421 roots : [ '<rootDir>/tests' ] ,
522 testMatch : [ '**/__tests__/**/*.test.ts' ] ,
23+
24+ // Coverage configuration
625 collectCoverageFrom : [
726 'src/**/*.{ts,tsx}' ,
827 '!src/**/*.d.ts' ,
928 '!src/**/*.test.{ts,tsx}' ,
1029 '!src/**/*.spec.{ts,tsx}' ,
1130 ] ,
31+
32+ // Setup files
1233 setupFilesAfterEnv : [ '<rootDir>/tests/setup.ts' ] ,
34+
35+ // Module name mapping for path aliases
1336 moduleNameMapper : {
37+ // Handle .js extensions for TypeScript files in ESM
1438 '^(\\.{1,2}/.*)\\.js$' : '$1' ,
39+ // Map TypeScript path aliases to actual file paths
40+ ...pathsToModuleNameMapper ( compilerOptions . paths , {
41+ prefix : '<rootDir>/' ,
42+ useESM : true
43+ } ) ,
44+ // Mock problematic ESM modules
45+ '^figures$' : '<rootDir>/tests/__mocks__/figures.js' ,
46+ '^is-unicode-supported$' : '<rootDir>/tests/__mocks__/is-unicode-supported.js' ,
47+ '^conf$' : '<rootDir>/tests/__mocks__/conf.js' ,
1548 } ,
49+
50+ // Transform configuration
1651 transform : {
17- '^.+\\.tsx?$' : 'ts-jest' ,
18- } ,
19- extensionsToTreatAsEsm : [ '.ts' , '.tsx' ] ,
20- globals : {
21- 'ts-jest' : {
52+ '^.+\\.tsx?$' : [ 'ts-jest' , {
2253 useESM : true ,
23- } ,
54+ tsconfig : {
55+ ...compilerOptions ,
56+ // Override some options for Jest
57+ rootDir : '.' ,
58+ module : 'ESNext' ,
59+ moduleResolution : 'Node' ,
60+ allowSyntheticDefaultImports : true ,
61+ esModuleInterop : true ,
62+ }
63+ } ] ,
2464 } ,
65+
66+ // Transform ignore patterns for node_modules
67+ transformIgnorePatterns : [
68+ 'node_modules/(?!(conf|@runloop|ink|react|ink-big-text|ink-gradient|ink-spinner|ink-text-input|ink-select-input|ink-box|ink-text|figures|is-unicode-supported)/)'
69+ ] ,
70+
71+ // Treat these extensions as ESM
72+ extensionsToTreatAsEsm : [ '.ts' , '.tsx' ] ,
73+
74+ // Test timeout
2575 testTimeout : 30000 ,
76+
77+ // Coverage thresholds
2678 coverageThreshold : {
2779 global : {
2880 branches : 80 ,
@@ -31,6 +83,16 @@ export default {
3183 statements : 80 ,
3284 } ,
3385 } ,
86+
87+
88+ // Module file extensions
89+ moduleFileExtensions : [ 'ts' , 'tsx' , 'js' , 'jsx' , 'json' ] ,
90+
91+ // Clear mocks between tests
92+ clearMocks : true ,
93+
94+ // Restore mocks between tests
95+ restoreMocks : true ,
3496} ;
3597
3698
0 commit comments