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

fix(js): ignore .test. and test tsx files with buildable swc libraries #9540

Merged
merged 1 commit into from Apr 11, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/js/migrations.json
Expand Up @@ -11,6 +11,12 @@
"version": "13.8.5-beta.1",
"description": "Adjust .swcrc to .lib.swcrc",
"factory": "./src/migrations/update-13-8-5/update-swcrc"
},
"update-swcrc-exclude": {
"cli": "nx",
"version": "13.10.1-beta.1",
"description": "Update .lib.swcrc to exclude missing test files",
"factory": "./src/migrations/update-13-10-1/update-lib-swcrc-exclude"
}
},
"packageJsonUpdates": {}
Expand Down
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Update .lib.swcrc exclude should update the exclude pattern 1`] = `
"{
\\"jsc\\": {
\\"target\\": \\"es2017\\",
\\"parser\\": {
\\"syntax\\": \\"typescript\\",
\\"decorators\\": true,
\\"dynamicImport\\": true
},
\\"transform\\": {
\\"decoratorMetadata\\": true,
\\"legacyDecorator\\": true
},
\\"keepClassNames\\": true,
\\"externalHelpers\\": true,
\\"loose\\": true
},
\\"module\\": {
\\"type\\": \\"commonjs\\",
\\"strict\\": true,
\\"noInterop\\": true
},
\\"sourceMaps\\": true,
\\"exclude\\": [
\\".*.spec.tsx?$\\",
\\".*.test.tsx?$\\",
\\"./src/jest-setup.ts$\\",
\\"./**/jest-setup.ts$\\",
\\".*.js$\\"
]
}
"
`;
@@ -0,0 +1,90 @@
import {
addProjectConfiguration,
ProjectConfiguration,
readJson,
Tree,
updateJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import updateSwcRcExclude from './update-lib-swcrc-exclude';

const projectConfig: ProjectConfiguration = {
root: 'libs/swc-lib',
sourceRoot: 'libs/swc-lib/src',
targets: {
build: {
executor: '@nrwl/js:swc',
outputs: ['{options.outputPath}'],
options: {
outputPath: 'dist/libs/swc-lib',
main: 'libs/swc-lib/src/index.ts',
tsConfig: 'libs/swc-lib/tsconfig.lib.json',
assets: ['libs/swc-lib/*.md'],
},
},
},
};
const oldSwcRc = {
jsc: {
target: 'es2017',
parser: {
syntax: 'typescript',
decorators: true,
dynamicImport: true,
},
transform: {
decoratorMetadata: true,
legacyDecorator: true,
},
keepClassNames: true,
externalHelpers: true,
loose: true,
},
module: {
type: 'commonjs',
strict: true,
noInterop: true,
},
sourceMaps: true,
exclude: [
'./src/**/.*.spec.ts$',
'./**/.*.spec.ts$',
'./src/**/jest-setup.ts$',
'./**/jest-setup.ts$',
'./**/.*.js$',
],
};
describe('Update .lib.swcrc exclude', () => {
let tree: Tree;
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
addProjectConfiguration(tree, 'swc-lib', projectConfig);

tree.write('libs/swc-lib/.lib.swcrc', JSON.stringify(oldSwcRc));
});

it('should update the exclude pattern', () => {
updateSwcRcExclude(tree);
expect(tree.read('libs/swc-lib/.lib.swcrc', 'utf-8')).toMatchSnapshot();
});

it('should NOT update the exclude pattern if not present', () => {
updateJson(tree, 'libs/swc-lib/.lib.swcrc', (json) => {
delete json.exclude;
return json;
});

const before = readJson(tree, 'libs/swc-lib/.lib.swcrc');
updateSwcRcExclude(tree);
const after = readJson(tree, 'libs/swc-lib/.lib.swcrc');

expect(after.exclude).toBeFalsy();
expect(after).toEqual(before);
});

it('should do nothing if .lib.swcrc doest not exist', () => {
tree.delete('libs/swc-lib/.lib-swcrc');

expect(() => updateSwcRcExclude(tree)).not.toThrowError();
});
});
@@ -0,0 +1,41 @@
import { readProjectConfiguration, Tree, updateJson } from '@nrwl/devkit';
import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils';
import { join } from 'path';
import { SwcExecutorOptions } from '../../utils/schema';
import { defaultExclude } from '../../utils/swc/add-swc-config';

export default function updateSwcRcExclude(tree: Tree) {
forEachExecutorOptions(
tree,
'@nrwl/js:swc',
(config: SwcExecutorOptions, projectName) => {
const projectConfig = readProjectConfiguration(tree, projectName);
const libSwcPath = join(projectConfig.root, '.lib.swcrc');

if (!tree.exists(libSwcPath)) return;

updateJson(
tree,
libSwcPath,
(json) => {
if (json.exclude) {
const excludePatterns = new Set([
...defaultExclude,
...json.exclude,
]);
// remove old patterns that are duplicate for new patterns
// defined in defaultExclude
excludePatterns.delete('./**/.*.spec.ts$');
excludePatterns.delete('./src/**/.*.spec.ts$');
excludePatterns.delete('./**/.*.js$');
excludePatterns.delete('./src/**/jest-setup.ts$');

json.exclude = [...excludePatterns];
}
return json;
},
{ expectComments: true }
);
}
);
}
8 changes: 4 additions & 4 deletions packages/js/src/utils/swc/add-swc-config.ts
Expand Up @@ -4,11 +4,11 @@ import { Tree } from '@nrwl/devkit';
import { join } from 'path';

export const defaultExclude = [
'./src/**/.*.spec.ts$',
'./**/.*.spec.ts$',
'./src/**/jest-setup.ts$',
'.*.spec.tsx?$',
'.*.test.tsx?$',
'./src/jest-setup.ts$',
'./**/jest-setup.ts$',
'./**/.*.js$',
'.*.js$',
];

const swcOptionsString = () => `{
Expand Down