@@ -7,14 +7,17 @@ const {
77 readDefaultTsConfig,
88 tsCompilerOptionsToSwcConfig,
99} = 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' )
1112
1213const {
1314 testMatch,
1415 testMatchGlob,
1516 testMatchExtensions,
1617} = require ( './helpers/test-match' )
1718
19+ const debug = getDebug ( 'jest' )
20+
1821const ignores = [
1922 '/node_modules/' ,
2023 '/__fixtures__/' ,
@@ -24,12 +27,48 @@ const ignores = [
2427 '__mocks__' ,
2528]
2629
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+
2768/**
2869 * Get the path at which `@hover/javascript/jest` is installed in a dependent
2970 * project in order to resolve the Jest preset as sometimes package managers
3071 * nest the preset installation within the `@hover/javascript` installation.
31- *
32- * @returns
3372 */
3473const getResolvePaths = ( ) => {
3574 try {
@@ -41,6 +80,54 @@ const getResolvePaths = () => {
4180 }
4281}
4382
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+
44131/** @type JestConfig */
45132const jestConfig = {
46133 roots : [ fromRoot ( '.' ) ] ,
@@ -55,52 +142,7 @@ const jestConfig = {
55142 testMatch,
56143 testPathIgnorePatterns : [ ...ignores , '<rootDir>/dist' ] ,
57144 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 ( ) ,
104146 coveragePathIgnorePatterns : [
105147 ...ignores ,
106148 'src/(umd|cjs|esm)-entry.js$' ,
0 commit comments