@@ -7,14 +7,17 @@ const {
7
7
readDefaultTsConfig,
8
8
tsCompilerOptionsToSwcConfig,
9
9
} = require ( '@swc-node/register/read-default-tsconfig' )
10
- const { ifAnyDep, hasFile, fromRoot, hasDevDep} = require ( '../utils' )
10
+
11
+ const { ifAnyDep, hasFile, fromRoot, hasDevDep, getDebug} = require ( '../utils' )
11
12
12
13
const {
13
14
testMatch,
14
15
testMatchGlob,
15
16
testMatchExtensions,
16
17
} = require ( './helpers/test-match' )
17
18
19
+ const debug = getDebug ( 'jest' )
20
+
18
21
const ignores = [
19
22
'/node_modules/' ,
20
23
'/__fixtures__/' ,
@@ -24,12 +27,48 @@ const ignores = [
24
27
'__mocks__' ,
25
28
]
26
29
30
+ /**
31
+ * @type {SwcNodeOptions }
32
+ */
33
+ const DEFAULT_SWC_OPTIONS = {
34
+ esModuleInterop : true ,
35
+ module : 'commonjs' ,
36
+ react : { runtime : 'automatic' } ,
37
+ swc : {
38
+ jsc : {
39
+ target : 'es2020' ,
40
+ experimental : {
41
+ plugins : [ [ require . resolve ( 'swc_mut_cjs_exports' ) , { } ] ] ,
42
+ } ,
43
+ parser : {
44
+ syntax : 'typescript' ,
45
+ tsx : true ,
46
+ decorators : false ,
47
+ dynamicimport : true ,
48
+ } ,
49
+ loose : true ,
50
+ externalHelpers : false ,
51
+ transform : {
52
+ react : {
53
+ runtime : 'automatic' ,
54
+ } ,
55
+ } ,
56
+ } ,
57
+ } ,
58
+ }
59
+
60
+ const tsConfig = readDefaultTsConfig ( )
61
+ const swcConfig = merge (
62
+ tsCompilerOptionsToSwcConfig ( tsConfig , '' ) ,
63
+ DEFAULT_SWC_OPTIONS ,
64
+ )
65
+
66
+ debug . prefix ( 'tsconfig:paths' ) ( tsConfig . paths )
67
+
27
68
/**
28
69
* Get the path at which `@hover/javascript/jest` is installed in a dependent
29
70
* project in order to resolve the Jest preset as sometimes package managers
30
71
* nest the preset installation within the `@hover/javascript` installation.
31
- *
32
- * @returns
33
72
*/
34
73
const getResolvePaths = ( ) => {
35
74
try {
@@ -41,6 +80,54 @@ const getResolvePaths = () => {
41
80
}
42
81
}
43
82
83
+ /**
84
+ * The default transform is now SWC, however, `ts-jest` will
85
+ * still be used if it is installed in the host project
86
+ *
87
+ * @returns {JestConfig['transform'] }
88
+ */
89
+ const getTransform = ( ) => {
90
+ const log = debug . prefix ( 'transform' )
91
+
92
+ if ( hasDevDep ( 'ts-jest' ) ) {
93
+ log ( 'Detected `ts-jest` package, using ts-jest transform' )
94
+
95
+ const transform = Object . fromEntries (
96
+ // Ensure we can resolve the preset even when
97
+ // it's in a nested `node_modules` installation
98
+ Object . entries ( require ( 'ts-jest/presets' ) . jsWithTs . transform ) . map (
99
+ ( [ glob , [ transformer , options ] ] ) => [
100
+ glob ,
101
+ [
102
+ require . resolve ( transformer ) ,
103
+ {
104
+ ...options ,
105
+ diagnostics : false ,
106
+ } ,
107
+ ] ,
108
+ ] ,
109
+ ) ,
110
+ )
111
+
112
+ log ( transform )
113
+
114
+ return transform
115
+ }
116
+
117
+ log ( 'No `ts-jest` package detected, using default SWC transform' )
118
+
119
+ const transform = {
120
+ '^.+\\.(t|j|mj)sx?$' : [
121
+ require . resolve ( '@swc-node/jest' , getResolvePaths ( ) ) ,
122
+ swcConfig ,
123
+ ] ,
124
+ }
125
+
126
+ log ( transform )
127
+
128
+ return transform
129
+ }
130
+
44
131
/** @type JestConfig */
45
132
const jestConfig = {
46
133
roots : [ fromRoot ( '.' ) ] ,
@@ -55,52 +142,7 @@ const jestConfig = {
55
142
testMatch,
56
143
testPathIgnorePatterns : [ ...ignores , '<rootDir>/dist' ] ,
57
144
testLocationInResults : true ,
58
- // The default transform is now SWC, however, `ts-jest` will
59
- // still be used if it is installed in the host project
60
- transform : hasDevDep ( 'ts-jest' )
61
- ? Object . fromEntries (
62
- // Ensure we can resolve the preset even when
63
- // it's in a nested `node_modules` installation
64
- Object . entries ( require ( 'ts-jest/presets' ) . jsWithTs . transform ) . map (
65
- ( [ glob , [ transformer , options ] ] ) => [
66
- glob ,
67
- [
68
- require . resolve ( transformer ) ,
69
- {
70
- ...options ,
71
- diagnostics : false ,
72
- } ,
73
- ] ,
74
- ] ,
75
- ) ,
76
- )
77
- : {
78
- '^.+\\.(t|j)sx?$' : [
79
- require . resolve ( '@swc-node/jest' , getResolvePaths ( ) ) ,
80
- /** @type {SwcNodeOptions } */ (
81
- merge ( tsCompilerOptionsToSwcConfig ( readDefaultTsConfig ( ) , '' ) , {
82
- esModuleInterop : true ,
83
- module : 'commonjs' ,
84
- swc : {
85
- jsc : {
86
- target : 'es2020' ,
87
- experimental : {
88
- plugins : [ [ require . resolve ( 'swc_mut_cjs_exports' ) , { } ] ] ,
89
- } ,
90
- parser : {
91
- syntax : 'typescript' ,
92
- tsx : true ,
93
- decorators : false ,
94
- dynamicimport : true ,
95
- } ,
96
- loose : true ,
97
- externalHelpers : false ,
98
- } ,
99
- } ,
100
- } )
101
- ) ,
102
- ] ,
103
- } ,
145
+ transform : getTransform ( ) ,
104
146
coveragePathIgnorePatterns : [
105
147
...ignores ,
106
148
'src/(umd|cjs|esm)-entry.js$' ,
0 commit comments