1
1
import path from 'node:path' ;
2
2
import url from 'node:url' ;
3
- import tsconfigJSON from './tsconfig.json' assert { type : " json " } ;
3
+ import tsconfigJSON from './tsconfig.json' assert { type : ' json ' } ;
4
4
5
5
const projectPath = path . dirname ( url . fileURLToPath ( import . meta. url ) ) ;
6
6
7
- // Global variables that are shared across the jest worker pool
8
- // These variables must be static and serializable
9
7
const globals = {
10
- // Absolute directory to the project root
11
8
projectDir : projectPath ,
12
- // Absolute directory to the test root
13
9
testDir : path . join ( projectPath , 'tests' ) ,
14
- // Default asynchronous test timeout
15
10
defaultTimeout : 20000 ,
16
- // Timeouts rely on setTimeout which takes 32 bit numbers
17
11
maxTimeout : Math . pow ( 2 , 31 ) - 1 ,
18
12
} ;
19
13
20
- // The `globalSetup` and `globalTeardown` cannot access the `globals`
21
- // They run in their own process context
22
- // They can however receive the process environment
23
- // Use `process.env` to set variables
24
-
25
14
const config = {
26
15
testEnvironment : 'node' ,
27
16
verbose : true ,
@@ -30,55 +19,61 @@ const config = {
30
19
coverageDirectory : '<rootDir>/tmp/coverage' ,
31
20
roots : [ '<rootDir>/tests' ] ,
32
21
testMatch : [ '**/?(*.)+(spec|test|unit.test).+(ts|tsx|js|jsx)' ] ,
22
+
23
+ // Use SWC to transpile TS/JS/TSX
33
24
transform : {
34
- " ^.+\\.(t|j)sx?$" : [
35
- " @swc/jest" ,
25
+ ' ^.+\\.(t|j)sx?$' : [
26
+ ' @swc/jest' ,
36
27
{
37
28
jsc : {
38
29
parser : {
39
- syntax : " typescript" ,
30
+ syntax : ' typescript' ,
40
31
tsx : true ,
41
32
decorators : tsconfigJSON . compilerOptions . experimentalDecorators ,
42
33
dynamicImport : true ,
43
34
} ,
44
35
target : tsconfigJSON . compilerOptions . target . toLowerCase ( ) ,
45
36
keepClassNames : true ,
46
37
} ,
47
- }
38
+ module : {
39
+ type : 'es6' , // Crucial for ESM-style imports
40
+ } ,
41
+ } ,
48
42
] ,
49
43
} ,
44
+
45
+ // Required to treat TS/TSX as ESM in Jest
46
+ extensionsToTreatAsEsm : [ '.ts' , '.tsx' , '.mts' ] ,
47
+
48
+ moduleNameMapper : {
49
+ // Remove `.js` extension from relative imports in source like `./foo.js` → `./foo`
50
+ '^(\\.{1,2}/.*)\\.js$' : '$1' ,
51
+ } ,
52
+
50
53
reporters : [
51
54
'default' ,
52
- [ 'jest-junit' , {
53
- outputDirectory : '<rootDir>/tmp/junit' ,
54
- classNameTemplate : '{classname}' ,
55
- ancestorSeparator : ' > ' ,
56
- titleTemplate : '{title}' ,
57
- addFileAttribute : 'true' ,
58
- reportTestSuiteErrors : 'true' ,
59
- } ] ,
55
+ [
56
+ 'jest-junit' ,
57
+ {
58
+ outputDirectory : '<rootDir>/tmp/junit' ,
59
+ classNameTemplate : '{classname}' ,
60
+ ancestorSeparator : ' > ' ,
61
+ titleTemplate : '{title}' ,
62
+ addFileAttribute : 'true' ,
63
+ reportTestSuiteErrors : 'true' ,
64
+ } ,
65
+ ] ,
60
66
] ,
67
+
61
68
collectCoverageFrom : [ 'src/**/*.{ts,tsx,js,jsx}' , '!src/**/*.d.ts' ] ,
62
69
coverageReporters : [ 'text' , 'cobertura' ] ,
70
+
63
71
globals,
64
- // Global setup script executed once before all test files
72
+
65
73
globalSetup : '<rootDir>/tests/globalSetup.ts' ,
66
- // Global teardown script executed once after all test files
67
74
globalTeardown : '<rootDir>/tests/globalTeardown.ts' ,
68
- // Setup files are executed before each test file
69
- // Can access globals
70
75
setupFiles : [ '<rootDir>/tests/setup.ts' ] ,
71
- // Setup files after env are executed before each test file
72
- // after the jest test environment is installed
73
- // Can access globals
74
- setupFilesAfterEnv : [
75
- 'jest-extended/all' ,
76
- '<rootDir>/tests/setupAfterEnv.ts'
77
- ] ,
78
- moduleNameMapper : {
79
- "^(\\.{1,2}/.*)\\.js$" : "$1" ,
80
- } ,
81
- extensionsToTreatAsEsm : [ '.ts' , '.tsx' , '.mts' ] ,
76
+ setupFilesAfterEnv : [ 'jest-extended/all' , '<rootDir>/tests/setupAfterEnv.ts' ] ,
82
77
} ;
83
78
84
79
export default config ;
0 commit comments