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

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

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/jest/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions packages/jest/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
});
Expand Down
4 changes: 3 additions & 1 deletion packages/jest/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
]
`);
});
});
Original file line number Diff line number Diff line change
@@ -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;