Skip to content

Commit 626c514

Browse files
authored
feat(misc): replace ts-jest transformer with @swc/jest for ts solution setup (#29763)
## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes #
1 parent 5127c15 commit 626c514

File tree

14 files changed

+479
-42
lines changed

14 files changed

+479
-42
lines changed

packages/detox/src/generators/application/application.spec.ts

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,8 @@ describe('detox application generator', () => {
252252
expect(tree.exists('my-dir/my-app-e2e/.detoxrc.json')).toBeTruthy();
253253
expect(tree.exists('my-dir/my-app-e2e/src/app.spec.ts')).toBeTruthy();
254254

255-
const detoxrc = tree.read('my-dir/my-app-e2e/.detoxrc.json').toString();
256-
// Strip trailing commas
257-
const detoxrcJson = JSON.parse(
258-
detoxrc.replace(/(?<=(true|false|null|["\d}\]])\s*),(?=\s*[}\]])/g, '')
259-
);
255+
const detoxrcJson = readJson(tree, 'my-dir/my-app-e2e/.detoxrc.json');
256+
expect(detoxrcJson.testRunner.args.config).toEqual('./jest.config.json');
260257
const appsDetoxrcJson = detoxrcJson['apps'];
261258
expect(appsDetoxrcJson).toEqual({
262259
'android.debug': {
@@ -288,6 +285,32 @@ describe('detox application generator', () => {
288285
type: 'ios.app',
289286
},
290287
});
288+
expect(tree.read('my-dir/my-app-e2e/jest.config.json', 'utf-8'))
289+
.toMatchInlineSnapshot(`
290+
"{
291+
"preset": "../../jest.preset",
292+
"rootDir": ".",
293+
"testMatch": [
294+
"<rootDir>/src/**/*.test.ts?(x)",
295+
"<rootDir>/src/**/*.spec.ts?(x)"
296+
],
297+
"testTimeout": 120000,
298+
"maxWorkers": 1,
299+
"globalSetup": "detox/runners/jest/globalSetup",
300+
"globalTeardown": "detox/runners/jest/globalTeardown",
301+
"reporters": ["detox/runners/jest/reporter"],
302+
"testEnvironment": "detox/runners/jest/testEnvironment",
303+
"verbose": true,
304+
"setupFilesAfterEnv": ["<rootDir>/test-setup.ts"],
305+
"transform": {
306+
"^.+\\\\.(ts|js|html)$": [
307+
"ts-jest",
308+
{ "tsconfig": "<rootDir>/tsconfig.e2e.json" }
309+
]
310+
}
311+
}
312+
"
313+
`);
291314
});
292315

293316
it('should update configuration', async () => {
@@ -558,5 +581,84 @@ describe('detox application generator', () => {
558581
"
559582
`);
560583
});
584+
585+
it('should generate jest test config with @swc/jest', async () => {
586+
writeJson(tree, 'apps/my-app/package.json', {
587+
name: 'my-app',
588+
});
589+
590+
await detoxApplicationGenerator(tree, {
591+
e2eDirectory: 'apps/my-app-e2e',
592+
appProject: 'my-app',
593+
linter: Linter.None,
594+
framework: 'react-native',
595+
addPlugin: true,
596+
skipFormat: true,
597+
});
598+
599+
expect(tree.exists('apps/my-app-e2e/test-setup.ts')).toBeTruthy();
600+
const detoxrc = readJson(tree, 'apps/my-app-e2e/.detoxrc.json');
601+
expect(detoxrc.testRunner.args.config).toEqual('./jest.config.ts');
602+
expect(tree.read('apps/my-app-e2e/jest.config.ts', 'utf-8'))
603+
.toMatchInlineSnapshot(`
604+
"/* eslint-disable */
605+
import { readFileSync } from 'fs';
606+
607+
// Reading the SWC compilation config for the spec files
608+
const swcJestConfig = JSON.parse(
609+
readFileSync(\`\${__dirname}/.spec.swcrc\`, 'utf-8')
610+
);
611+
612+
// Disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves
613+
swcJestConfig.swcrc = false;
614+
615+
export default {
616+
preset: "../../jest.preset",
617+
rootDir: ".",
618+
testMatch: [
619+
"<rootDir>/src/**/*.test.ts?(x)",
620+
"<rootDir>/src/**/*.spec.ts?(x)"
621+
],
622+
testTimeout: 120000,
623+
maxWorkers: 1,
624+
globalSetup: "detox/runners/jest/globalSetup",
625+
globalTeardown: "detox/runners/jest/globalTeardown",
626+
reporters: ["detox/runners/jest/reporter"],
627+
testEnvironment: "detox/runners/jest/testEnvironment",
628+
verbose: true,
629+
setupFilesAfterEnv: ["<rootDir>/test-setup.ts"],
630+
transform: {
631+
"^.+\\\\.(ts|js|html)$": ['@swc/jest', swcJestConfig]
632+
}
633+
};
634+
"
635+
`);
636+
expect(tree.read('apps/my-app-e2e/.spec.swcrc', 'utf-8'))
637+
.toMatchInlineSnapshot(`
638+
"{
639+
"jsc": {
640+
"target": "es2017",
641+
"parser": {
642+
"syntax": "typescript",
643+
"decorators": true,
644+
"dynamicImport": true
645+
},
646+
"transform": {
647+
"decoratorMetadata": true,
648+
"legacyDecorator": true
649+
},
650+
"keepClassNames": true,
651+
"externalHelpers": true,
652+
"loose": true
653+
},
654+
"module": {
655+
"type": "es6"
656+
},
657+
"sourceMaps": true,
658+
"exclude": []
659+
}
660+
"
661+
`);
662+
});
561663
});
562664
});

packages/detox/src/generators/application/files/app/.detoxrc.json.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"testRunner": {
33
"args": {
44
"$0": "jest",
5-
"config": "./jest.config.json"
5+
"config": "./<%= jestConfigFileName %>"
66
},
77
"jest": {
88
"setupTimeout": 120000

packages/detox/src/generators/application/files/app/jest.config.json.template renamed to packages/detox/src/generators/application/files/non-ts-solution/jest.config.json.template

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@
1313
"testEnvironment": "detox/runners/jest/testEnvironment",
1414
"verbose": true,
1515
"setupFilesAfterEnv": ["<rootDir>/test-setup.ts"],
16-
"transform": {
16+
"transform": {
1717
"^.+\\.(ts|js|html)$": [
1818
"ts-jest",
1919
{ "tsconfig": "<rootDir>/tsconfig.e2e.json" }
2020
]
2121
}
22-
2322
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/* eslint-disable */
2+
<% if (js) { %>const { readFileSync } = require('fs')<% } else { %>import { readFileSync } from 'fs';<% } %>
3+
4+
// Reading the SWC compilation config for the spec files
5+
const swcJestConfig = JSON.parse(
6+
readFileSync(`${__dirname}/.spec.swcrc`, 'utf-8')
7+
);
8+
9+
// Disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves
10+
swcJestConfig.swcrc = false;
11+
12+
<% if (js) { %>module.exports =<% } else { %>export default<% } %> {
13+
preset: "<%= offsetFromRoot %>jest.preset",
14+
rootDir: ".",
15+
testMatch: [
16+
"<rootDir>/src/**/*.test.ts?(x)",
17+
"<rootDir>/src/**/*.spec.ts?(x)"
18+
],
19+
testTimeout: 120000,
20+
maxWorkers: 1,
21+
globalSetup: "detox/runners/jest/globalSetup",
22+
globalTeardown: "detox/runners/jest/globalTeardown",
23+
reporters: ["detox/runners/jest/reporter"],
24+
testEnvironment: "detox/runners/jest/testEnvironment",
25+
verbose: true,
26+
setupFilesAfterEnv: ["<rootDir>/test-setup.ts"],
27+
transform: {
28+
"^.+\\.(ts|js|html)$": ['@swc/jest', swcJestConfig]
29+
}
30+
};

packages/detox/src/generators/application/lib/create-files.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ describe('Create Files', () => {
2828
expect(tree.exists('apps/my-app-e2e/.detoxrc.json')).toBeTruthy();
2929
expect(tree.exists('apps/my-app-e2e/tsconfig.json')).toBeTruthy();
3030
expect(tree.exists('apps/my-app-e2e/tsconfig.e2e.json')).toBeTruthy();
31+
expect(tree.exists('apps/my-app-e2e/jest.config.json')).toBeTruthy();
3132
expect(tree.exists('apps/my-app-e2e/test-setup.ts')).toBeTruthy();
3233
});
3334
});

packages/detox/src/generators/application/lib/create-files.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import {
2+
offsetFromRoot as _offsetFromRoot,
23
detectPackageManager,
34
generateFiles,
45
getPackageManagerCommand,
5-
offsetFromRoot as _offsetFromRoot,
6+
joinPathFragments,
67
toJS,
78
Tree,
89
writeJson,
9-
joinPathFragments,
1010
} from '@nx/devkit';
1111
import { getRelativePathToRootTsConfig } from '@nx/js';
12+
import { addSwcTestConfig } from '@nx/js/src/utils/swc/add-swc-config';
1213
import { join } from 'path';
1314
import { NormalizedSchema } from './normalize-options';
1415

@@ -23,8 +24,22 @@ export function createFiles(host: Tree, options: NormalizedSchema) {
2324
exec: getPackageManagerCommand(detectPackageManager(host.root)).exec,
2425
offsetFromRoot,
2526
rootTsConfigPath,
27+
jestConfigFileName: options.isUsingTsSolutionConfig
28+
? 'jest.config.ts'
29+
: 'jest.config.json',
2630
});
2731
if (options.isUsingTsSolutionConfig) {
32+
addSwcTestConfig(host, options.e2eProjectRoot, 'es6');
33+
generateFiles(
34+
host,
35+
join(__dirname, '../files/ts-solution'),
36+
options.e2eProjectRoot,
37+
{
38+
...options,
39+
exec: getPackageManagerCommand(detectPackageManager(host.root)).exec,
40+
offsetFromRoot,
41+
}
42+
);
2843
writeJson(
2944
host,
3045
joinPathFragments(options.e2eProjectRoot, 'tsconfig.json'),

packages/detox/src/generators/application/lib/normalize-options.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('Normalize Options', () => {
3939
appRoot: 'apps/my-app',
4040
isUsingTsSolutionConfig: false,
4141
linter: Linter.EsLint,
42+
js: false,
4243
});
4344
});
4445

@@ -68,6 +69,7 @@ describe('Normalize Options', () => {
6869
e2eProjectRoot: 'apps/my-app-e2e',
6970
framework: 'react-native',
7071
isUsingTsSolutionConfig: false,
72+
js: false,
7173
});
7274
});
7375

@@ -97,6 +99,7 @@ describe('Normalize Options', () => {
9799
e2eProjectName: 'directory-my-app-e2e',
98100
framework: 'react-native',
99101
isUsingTsSolutionConfig: false,
102+
js: false,
100103
});
101104
});
102105
});

packages/detox/src/generators/application/lib/normalize-options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ export async function normalizeOptions(
4848
e2eProjectName,
4949
e2eProjectRoot,
5050
isUsingTsSolutionConfig: isUsingTsSolutionSetup(host),
51+
js: options.js ?? false,
5152
};
5253
}

0 commit comments

Comments
 (0)