feat(testing): add test-setup.ts to ignored prod inputs (#17918)

This commit is contained in:
Miroslav Jonaš 2023-07-04 16:20:54 +02:00 committed by GitHub
parent 749bc5bee4
commit 8f0ec5cb39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 89 additions and 1 deletions

View File

@ -95,6 +95,12 @@
"version": "16.0.0-beta.1",
"description": "Replace @nrwl/jest with @nx/jest",
"implementation": "./src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages"
},
"add-test-setup-to-inputs-ignore": {
"cli": "nx",
"version": "16.5.0-beta.2",
"description": "Add test-setup.ts to ignored files in production input",
"implementation": "./src/migrations/update-16-5-0/add-test-setup-to-inputs-ignore"
}
},
"packageJsonUpdates": {

View File

@ -86,6 +86,7 @@ export default {
);
expect(productionFileSet).toContain('!{projectRoot}/tsconfig.spec.json');
expect(productionFileSet).toContain('!{projectRoot}/jest.config.[jt]s');
expect(productionFileSet).toContain('!{projectRoot}/src/test-setup.[jt]s');
expect(testDefaults).toEqual({
inputs: ['default', '^production', '{workspaceRoot}/jest.preset.js'],
});

View File

@ -124,7 +124,9 @@ function addTestInputs(tree: Tree) {
// Remove tsconfig.spec.json
'!{projectRoot}/tsconfig.spec.json',
// Remove jest.config.js/ts
'!{projectRoot}/jest.config.[jt]s'
'!{projectRoot}/jest.config.[jt]s',
// Remove test-setup.js/ts
'!{projectRoot}/src/test-setup.[jt]s'
);
// Dedupe and set
nxJson.namedInputs.production = Array.from(new Set(productionFileSet));

View File

@ -0,0 +1,51 @@
import { Tree, readNxJson, updateNxJson } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import addTestSetupToIgnoredInputs from './add-test-setup-to-inputs-ignore';
describe('Jest Migration - jest 29 mocked usage in tests', () => {
let tree: Tree;
beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
});
it('should add inputs configuration for test-setup if missing', async () => {
updateNxJson(tree, {
namedInputs: {
default: ['{projectRoot}/**/*', 'sharedGlobals'],
sharedGlobals: [],
production: ['default'],
},
});
await addTestSetupToIgnoredInputs(tree);
const updated = readNxJson(tree);
expect(updated.namedInputs.production).toMatchInlineSnapshot(`
[
"default",
"!{projectRoot}/src/test-setup.[jt]s",
]
`);
});
it('should not add inputs configuration for test-setup if existing', async () => {
updateNxJson(tree, {
namedInputs: {
default: ['{projectRoot}/**/*', 'sharedGlobals'],
sharedGlobals: [],
production: ['!{projectRoot}/src/test-setup.[jt]s', 'default'],
},
});
await addTestSetupToIgnoredInputs(tree);
const updated = readNxJson(tree);
expect(updated.namedInputs.production).toMatchInlineSnapshot(`
[
"!{projectRoot}/src/test-setup.[jt]s",
"default",
]
`);
});
});

View File

@ -0,0 +1,28 @@
import {
NxJsonConfiguration,
Tree,
formatFiles,
readNxJson,
updateNxJson,
} from '@nx/devkit';
export async function addTestSetupToIgnoredInputs(tree: Tree) {
const nxJson: NxJsonConfiguration = readNxJson(tree);
if (!nxJson) {
return;
}
if (
nxJson.namedInputs?.production &&
!nxJson.namedInputs.production.includes(
'!{projectRoot}/src/test-setup.[jt]s'
)
) {
nxJson.namedInputs.production.push('!{projectRoot}/src/test-setup.[jt]s');
updateNxJson(tree, nxJson);
}
await formatFiles(tree);
}
export default addTestSetupToIgnoredInputs;