Skip to content

Commit

Permalink
fix(js): generate correct jest config for --compiler=swc --js
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed May 11, 2022
1 parent c63364f commit 40c1b88
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`lib --unit-test-runner jest should generate test configuration with swc and js 1`] = `
"/* eslint-disable */
const { readFileSync } = require('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}/.lib.swcrc\`, 'utf-8')
);
module.exports = {
displayName: 'my-lib',
preset: '../../jest.preset.js',
transform: {
'^.+\\\\\\\\.[tj]s$': ['@swc/jest', swcJestConfig],
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory: '../../coverage/libs/my-lib'
};
"
`;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
import { readFileSync } from 'fs';
<% if(js) {%>const { readFileSync } = require('fs')<% } else { %>import { readFileSync } from 'fs';<% } %>

// Reading the SWC compilation config and remove the "exclude"
// for the test files to be compiled by SWC
Expand Down
24 changes: 24 additions & 0 deletions packages/js/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,30 @@ describe('lib', () => {
expect(readme).toContain('nx test my-lib');
});

it('should generate test configuration with swc and js', async () => {
await libraryGenerator(tree, {
...defaultOptions,
name: 'myLib',
unitTestRunner: 'jest',
compiler: 'swc',
js: true,
});

expect(tree.exists('libs/my-lib/tsconfig.spec.json')).toBeTruthy();
expect(tree.exists('libs/my-lib/jest.config.js')).toBeTruthy();
expect(tree.exists('libs/my-lib/src/lib/my-lib.spec.js')).toBeTruthy();

const projectConfig = readProjectConfiguration(tree, 'my-lib');
expect(projectConfig.targets.test).toBeDefined();

expect(tree.exists(`libs/my-lib/jest.config.js`)).toBeTruthy();
expect(
tree.read(`libs/my-lib/jest.config.js`, 'utf-8')
).toMatchSnapshot();
const readme = tree.read('libs/my-lib/README.md', 'utf-8');
expect(readme).toContain('nx test my-lib');
});

describe('--buildable', () => {
it('should generate the build target', async () => {
await libraryGenerator(tree, {
Expand Down
11 changes: 10 additions & 1 deletion packages/js/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,18 @@ function replaceJestConfig(
options: NormalizedSchema,
filesDir: string
) {
// the existing config has to be deleted otherwise the new config won't overwrite it
const existingJestConfig = joinPathFragments(
filesDir,
`jest.config.${options.js ? 'js' : 'ts'}`
);
if (tree.exists(existingJestConfig)) {
tree.delete(existingJestConfig);
}

// replace with JS:SWC specific jest config
generateFiles(tree, filesDir, options.projectRoot, {
tmpl: '',
ext: options.js ? 'js' : 'ts',
js: !!options.js,
project: options.name,
offsetFromRoot: offsetFromRoot(options.projectRoot),
Expand Down

0 comments on commit 40c1b88

Please sign in to comment.