Skip to content

Commit

Permalink
fix(js): info about jest global setup/teardown + swc (#16681)
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens authored May 9, 2023
1 parent ad77b91 commit 5743f15
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
43 changes: 38 additions & 5 deletions docs/generated/packages/jest/documents/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,52 @@ cleanupRegisteredPaths();
{% callout type="note" title="@swc/jest & global scripts" %}
When using @swc/jest and a global setup/teardown file,
You'll have to set the global setup/teardown file to be transformed with ts-jest.
For example, if your files are named `global-setup.ts` and `global-teardown.ts`,
then you would need to add to your _project level `jest.config.ts`_ a new entry in the transformers object
You have to set the `noInterop: false` and use dynmamic imports within the setup function
```typescript {% fileName="apps/<your-project>/jest.config.ts" %}
/* eslint-disable */
import { readFileSync } from 'fs';
// Reading the SWC compilation config and remove the "exclude"
// for the test files to be compiled by SWC
const { exclude: _, ...swcJestConfig } = JSON.parse(
readFileSync(`${__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;
}
// jest needs EsModule Interop to find the default exported function
swcJestConfig.module.noInterop = false;
export default {
globalSetup: '<rootDir>/src/global-setup-swc.ts',
transform: {
'global-(setup|teardown).ts': 'ts-jest',
// resest of the transformers
'^.+\\.[tj]s$': ['@swc/jest', swcJestConfig],
},
// other settings
};
```
```typescript {% fileName="global-setup-swc.ts" %}
import { registerTsProject } from '@nx/js/src/internal';
const cleanupRegisteredPaths = registerTsProject('.', 'tsconfig.base.json');
export default async function () {
// swc will hoist all imports, and we need to make sure the register happens first
// so we import all nx project alias within the setup function first.
const { yourFancyFunction } = await import('@some-org/my-util-library');
yourFancyFunction();
// make sure to run the clean up!
cleanupRegisteredPaths();
}
```
{% /callout %}
## More Documentation
Expand Down
43 changes: 38 additions & 5 deletions docs/shared/packages/jest/jest-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,52 @@ cleanupRegisteredPaths();
{% callout type="note" title="@swc/jest & global scripts" %}
When using @swc/jest and a global setup/teardown file,
You'll have to set the global setup/teardown file to be transformed with ts-jest.
For example, if your files are named `global-setup.ts` and `global-teardown.ts`,
then you would need to add to your _project level `jest.config.ts`_ a new entry in the transformers object
You have to set the `noInterop: false` and use dynmamic imports within the setup function
```typescript {% fileName="apps/<your-project>/jest.config.ts" %}
/* eslint-disable */
import { readFileSync } from 'fs';
// Reading the SWC compilation config and remove the "exclude"
// for the test files to be compiled by SWC
const { exclude: _, ...swcJestConfig } = JSON.parse(
readFileSync(`${__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;
}
// jest needs EsModule Interop to find the default exported function
swcJestConfig.module.noInterop = false;
export default {
globalSetup: '<rootDir>/src/global-setup-swc.ts',
transform: {
'global-(setup|teardown).ts': 'ts-jest',
// resest of the transformers
'^.+\\.[tj]s$': ['@swc/jest', swcJestConfig],
},
// other settings
};
```
```typescript {% fileName="global-setup-swc.ts" %}
import { registerTsProject } from '@nx/js/src/internal';
const cleanupRegisteredPaths = registerTsProject('.', 'tsconfig.base.json');
export default async function () {
// swc will hoist all imports, and we need to make sure the register happens first
// so we import all nx project alias within the setup function first.
const { yourFancyFunction } = await import('@some-org/my-util-library');
yourFancyFunction();
// make sure to run the clean up!
cleanupRegisteredPaths();
}
```
{% /callout %}
## More Documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ 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;
module.exports = {
displayName: 'my-lib',
preset: '../../jest.preset.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ 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;

<% if(js) {%>module.exports =<% } else { %>export default<% } %> {
displayName: '<%= project %>',
preset: '<%= offsetFromRoot %>jest.preset.js',
Expand Down

1 comment on commit 5743f15

@vercel
Copy link

@vercel vercel bot commented on 5743f15 May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.