Skip to content

Commit

Permalink
feat(workspace-plugin): update react-library generator jest config to…
Browse files Browse the repository at this point in the history
… use swc (#32122)
  • Loading branch information
Hotell authored Jul 31, 2024
1 parent 1b56d04 commit b5c9a8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
// @ts-check
/* eslint-disable */

const { readFileSync } = require('node:fs');
const { join } = require('node:path');

// Reading the SWC compilation config and remove the "exclude"
// for the test files to be compiled by SWC
const { exclude: _, ...swcJestConfig } = JSON.parse(readFileSync(join(__dirname, '.swcrc'), 'utf-8'));

// disable .swcrc look-up by SWC core because we're passing in swcJestConfig ourselves.
// If we do not disable this, SWC Core will read .swcrc and won't transform our test files due to "exclude"
if (swcJestConfig.swcrc === undefined) {
swcJestConfig.swcrc = false;
}

// Uncomment if using global setup/teardown files being transformed via swc
// https://nx.dev/packages/jest/documents/overview#global-setup/teardown-with-nx-libraries
// jest needs EsModule Interop to find the default exported setup/teardown functions
// swcJestConfig.module.noInterop = false;

/**
* @type {import('@jest/types').Config.InitialOptions}
Expand All @@ -7,13 +26,7 @@ module.exports = {
displayName: '<%= projectName %>',
preset: '../../../jest.preset.js',
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig.spec.json',
isolatedModules: true,
},
],
'^.+\\.tsx?$': ['@swc/jest', swcJestConfig],
},
coverageDirectory: './coverage',
setupFilesAfterEnv: ['./config/tests.js'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ describe('react-library generator', () => {
expect(readJson(tree, `${library.root}/.babelrc.json`)).toEqual(
expect.objectContaining({ extends: '../../../../.babelrc-v9.json' }),
);
expect(tree.read(`${library.root}/jest.config.js`, 'utf-8')).toEqual(
expect.stringContaining(`displayName: 'react-one-preview',`),
);

const jestConfig = tree.read(`${library.root}/jest.config.js`, 'utf-8');
expect(jestConfig).toEqual(expect.stringContaining(`displayName: 'react-one-preview',`));
expect(jestConfig).toEqual(expect.stringContaining(`'^.+\\\\.tsx?$': ['@swc/jest', swcJestConfig],`));
expect(tree.read(`${library.root}/README.md`, 'utf-8')).toEqual(
expect.stringContaining(stripIndents`
# @proj/react-one-preview
Expand Down

0 comments on commit b5c9a8c

Please sign in to comment.