Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(misc): replace ts-jest transformer with @swc/jest for ts solution setup #29763

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 107 additions & 5 deletions packages/detox/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,8 @@ describe('detox application generator', () => {
expect(tree.exists('my-dir/my-app-e2e/.detoxrc.json')).toBeTruthy();
expect(tree.exists('my-dir/my-app-e2e/src/app.spec.ts')).toBeTruthy();

const detoxrc = tree.read('my-dir/my-app-e2e/.detoxrc.json').toString();
// Strip trailing commas
const detoxrcJson = JSON.parse(
detoxrc.replace(/(?<=(true|false|null|["\d}\]])\s*),(?=\s*[}\]])/g, '')
);
const detoxrcJson = readJson(tree, 'my-dir/my-app-e2e/.detoxrc.json');
expect(detoxrcJson.testRunner.args.config).toEqual('./jest.config.json');
const appsDetoxrcJson = detoxrcJson['apps'];
expect(appsDetoxrcJson).toEqual({
'android.debug': {
Expand Down Expand Up @@ -288,6 +285,32 @@ describe('detox application generator', () => {
type: 'ios.app',
},
});
expect(tree.read('my-dir/my-app-e2e/jest.config.json', 'utf-8'))
.toMatchInlineSnapshot(`
"{
"preset": "../../jest.preset",
"rootDir": ".",
"testMatch": [
"<rootDir>/src/**/*.test.ts?(x)",
"<rootDir>/src/**/*.spec.ts?(x)"
],
"testTimeout": 120000,
"maxWorkers": 1,
"globalSetup": "detox/runners/jest/globalSetup",
"globalTeardown": "detox/runners/jest/globalTeardown",
"reporters": ["detox/runners/jest/reporter"],
"testEnvironment": "detox/runners/jest/testEnvironment",
"verbose": true,
"setupFilesAfterEnv": ["<rootDir>/test-setup.ts"],
"transform": {
"^.+\\\\.(ts|js|html)$": [
"ts-jest",
{ "tsconfig": "<rootDir>/tsconfig.e2e.json" }
]
}
}
"
`);
});

it('should update configuration', async () => {
Expand Down Expand Up @@ -558,5 +581,84 @@ describe('detox application generator', () => {
"
`);
});

it('should generate jest test config with @swc/jest', async () => {
writeJson(tree, 'apps/my-app/package.json', {
name: 'my-app',
});

await detoxApplicationGenerator(tree, {
e2eDirectory: 'apps/my-app-e2e',
appProject: 'my-app',
linter: Linter.None,
framework: 'react-native',
addPlugin: true,
skipFormat: true,
});

expect(tree.exists('apps/my-app-e2e/test-setup.ts')).toBeTruthy();
const detoxrc = readJson(tree, 'apps/my-app-e2e/.detoxrc.json');
expect(detoxrc.testRunner.args.config).toEqual('./jest.config.ts');
expect(tree.read('apps/my-app-e2e/jest.config.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"/* eslint-disable */
import { readFileSync } from 'fs';

// Reading the SWC compilation config for the spec files
const swcJestConfig = JSON.parse(
readFileSync(\`\${__dirname}/.spec.swcrc\`, 'utf-8')
);

// Disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves
swcJestConfig.swcrc = false;

export default {
preset: "../../jest.preset",
rootDir: ".",
testMatch: [
"<rootDir>/src/**/*.test.ts?(x)",
"<rootDir>/src/**/*.spec.ts?(x)"
],
testTimeout: 120000,
maxWorkers: 1,
globalSetup: "detox/runners/jest/globalSetup",
globalTeardown: "detox/runners/jest/globalTeardown",
reporters: ["detox/runners/jest/reporter"],
testEnvironment: "detox/runners/jest/testEnvironment",
verbose: true,
setupFilesAfterEnv: ["<rootDir>/test-setup.ts"],
transform: {
"^.+\\\\.(ts|js|html)$": ['@swc/jest', swcJestConfig]
}
};
"
`);
expect(tree.read('apps/my-app-e2e/.spec.swcrc', 'utf-8'))
.toMatchInlineSnapshot(`
"{
"jsc": {
"target": "es2017",
"parser": {
"syntax": "typescript",
"decorators": true,
"dynamicImport": true
},
"transform": {
"decoratorMetadata": true,
"legacyDecorator": true
},
"keepClassNames": true,
"externalHelpers": true,
"loose": true
},
"module": {
"type": "es6"
},
"sourceMaps": true,
"exclude": []
}
"
`);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"testRunner": {
"args": {
"$0": "jest",
"config": "./jest.config.json"
"config": "./<%= jestConfigFileName %>"
},
"jest": {
"setupTimeout": 120000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@
"testEnvironment": "detox/runners/jest/testEnvironment",
"verbose": true,
"setupFilesAfterEnv": ["<rootDir>/test-setup.ts"],
"transform": {
"transform": {
"^.+\\.(ts|js|html)$": [
"ts-jest",
{ "tsconfig": "<rootDir>/tsconfig.e2e.json" }
]
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable */
<% if (js) { %>const { readFileSync } = require('fs')<% } else { %>import { readFileSync } from 'fs';<% } %>

// Reading the SWC compilation config for the spec files
const swcJestConfig = JSON.parse(
readFileSync(`${__dirname}/.spec.swcrc`, 'utf-8')
);

// Disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves
swcJestConfig.swcrc = false;

<% if (js) { %>module.exports =<% } else { %>export default<% } %> {
preset: "<%= offsetFromRoot %>jest.preset",
rootDir: ".",
testMatch: [
"<rootDir>/src/**/*.test.ts?(x)",
"<rootDir>/src/**/*.spec.ts?(x)"
],
testTimeout: 120000,
maxWorkers: 1,
globalSetup: "detox/runners/jest/globalSetup",
globalTeardown: "detox/runners/jest/globalTeardown",
reporters: ["detox/runners/jest/reporter"],
testEnvironment: "detox/runners/jest/testEnvironment",
verbose: true,
setupFilesAfterEnv: ["<rootDir>/test-setup.ts"],
transform: {
"^.+\\.(ts|js|html)$": ['@swc/jest', swcJestConfig]
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('Create Files', () => {
expect(tree.exists('apps/my-app-e2e/.detoxrc.json')).toBeTruthy();
expect(tree.exists('apps/my-app-e2e/tsconfig.json')).toBeTruthy();
expect(tree.exists('apps/my-app-e2e/tsconfig.e2e.json')).toBeTruthy();
expect(tree.exists('apps/my-app-e2e/jest.config.json')).toBeTruthy();
expect(tree.exists('apps/my-app-e2e/test-setup.ts')).toBeTruthy();
});
});
19 changes: 17 additions & 2 deletions packages/detox/src/generators/application/lib/create-files.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import {
offsetFromRoot as _offsetFromRoot,
detectPackageManager,
generateFiles,
getPackageManagerCommand,
offsetFromRoot as _offsetFromRoot,
joinPathFragments,
toJS,
Tree,
writeJson,
joinPathFragments,
} from '@nx/devkit';
import { getRelativePathToRootTsConfig } from '@nx/js';
import { addSwcTestConfig } from '@nx/js/src/utils/swc/add-swc-config';
import { join } from 'path';
import { NormalizedSchema } from './normalize-options';

Expand All @@ -23,8 +24,22 @@ export function createFiles(host: Tree, options: NormalizedSchema) {
exec: getPackageManagerCommand(detectPackageManager(host.root)).exec,
offsetFromRoot,
rootTsConfigPath,
jestConfigFileName: options.isUsingTsSolutionConfig
? 'jest.config.ts'
: 'jest.config.json',
});
if (options.isUsingTsSolutionConfig) {
addSwcTestConfig(host, options.e2eProjectRoot, 'es6');
generateFiles(
host,
join(__dirname, '../files/ts-solution'),
options.e2eProjectRoot,
{
...options,
exec: getPackageManagerCommand(detectPackageManager(host.root)).exec,
offsetFromRoot,
}
);
writeJson(
host,
joinPathFragments(options.e2eProjectRoot, 'tsconfig.json'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('Normalize Options', () => {
appRoot: 'apps/my-app',
isUsingTsSolutionConfig: false,
linter: Linter.EsLint,
js: false,
});
});

Expand Down Expand Up @@ -68,6 +69,7 @@ describe('Normalize Options', () => {
e2eProjectRoot: 'apps/my-app-e2e',
framework: 'react-native',
isUsingTsSolutionConfig: false,
js: false,
});
});

Expand Down Expand Up @@ -97,6 +99,7 @@ describe('Normalize Options', () => {
e2eProjectName: 'directory-my-app-e2e',
framework: 'react-native',
isUsingTsSolutionConfig: false,
js: false,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ export async function normalizeOptions(
e2eProjectName,
e2eProjectRoot,
isUsingTsSolutionConfig: isUsingTsSolutionSetup(host),
js: options.js ?? false,
};
}
Loading
Loading