Skip to content

Commit

Permalink
feat(testing): add test-setup.ts to ignored prod inputs (#17918)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav authored Jul 4, 2023
1 parent 749bc5b commit 8f0ec5c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 1 deletion.
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;

1 comment on commit 8f0ec5c

@vercel
Copy link

@vercel vercel bot commented on 8f0ec5c Jul 4, 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-dev-git-master-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.