Skip to content

Commit

Permalink
fix(testing): exclude jest.config.ts in angular project tsconfigs (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed Aug 5, 2022
1 parent 5267f45 commit bd4e6ac
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 10 deletions.
10 changes: 7 additions & 3 deletions packages/angular/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ describe('app', () => {
);
expect(tsconfigApp.compilerOptions.outDir).toEqual('../../dist/out-tsc');
expect(tsconfigApp.extends).toEqual('./tsconfig.json');
expect(tsconfigApp.exclude).toEqual(['**/*.test.ts', '**/*.spec.ts']);
expect(tsconfigApp.exclude).toEqual([
'jest.config.ts',
'**/*.test.ts',
'**/*.spec.ts',
]);

const eslintrcJson = parseJson(
appTree.read('apps/my-app/.eslintrc.json', 'utf-8')
Expand Down Expand Up @@ -307,7 +311,7 @@ describe('app', () => {
{
path: 'apps/my-dir/my-app/tsconfig.app.json',
lookupFn: (json) => json.exclude,
expectedValue: ['**/*.test.ts', '**/*.spec.ts'],
expectedValue: ['jest.config.ts', '**/*.test.ts', '**/*.spec.ts'],
},
{
path: 'apps/my-dir/my-app/.eslintrc.json',
Expand Down Expand Up @@ -390,7 +394,7 @@ describe('app', () => {
{
path: 'my-dir/my-app/tsconfig.app.json',
lookupFn: (json) => json.exclude,
expectedValue: ['**/*.test.ts', '**/*.spec.ts'],
expectedValue: ['jest.config.ts', '**/*.test.ts', '**/*.spec.ts'],
},
{
path: 'my-dir/my-app/.eslintrc.json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ function updateTsConfigOptions(host: Tree, options: NormalizedSchema) {
outDir: `${offsetFromRoot(options.appProjectRoot)}dist/out-tsc`,
},
exclude: [
...new Set([...(json.exclude || []), '**/*.test.ts', '**/*.spec.ts']),
...new Set([
...(json.exclude || []),
'jest.config.ts',
'**/*.test.ts',
'**/*.spec.ts',
]),
],
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ function updateProjectConfig(host: Tree, options: NormalizedSchema) {
updateJson(host, `${options.projectRoot}/tsconfig.lib.json`, (json) => {
json.include = ['**/*.ts'];
json.exclude = [
...new Set([...(json.exclude || []), '**/*.test.ts', '**/*.spec.ts']),
...new Set([
...(json.exclude || []),
'jest.config.ts',
'**/*.test.ts',
'**/*.spec.ts',
]),
];
return json;
});
Expand Down
11 changes: 10 additions & 1 deletion packages/angular/src/generators/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ describe('lib', () => {
expect(tsconfigJson.exclude).toEqual([
'src/test-setup.ts',
'**/*.spec.ts',
'jest.config.ts',
'**/*.test.ts',
]);
});
Expand All @@ -409,6 +410,7 @@ describe('lib', () => {
expect(tsconfigJson.exclude).toEqual([
'src/test.ts',
'**/*.spec.ts',
'jest.config.ts',
'**/*.test.ts',
]);
});
Expand All @@ -421,7 +423,11 @@ describe('lib', () => {

// ASSERT
const tsconfigJson = readJson(tree, 'libs/my-lib/tsconfig.lib.json');
expect(tsconfigJson.exclude).toEqual(['**/*.test.ts', '**/*.spec.ts']);
expect(tsconfigJson.exclude).toEqual([
'jest.config.ts',
'**/*.test.ts',
'**/*.spec.ts',
]);
});
});

Expand Down Expand Up @@ -902,6 +908,7 @@ describe('lib', () => {
expect(tsConfigLibJson.exclude).toEqual([
'src/test-setup.ts',
'**/*.spec.ts',
'jest.config.ts',
'**/*.test.ts',
]);

Expand All @@ -916,6 +923,7 @@ describe('lib', () => {
expect(tsConfigLibJson2.exclude).toEqual([
'src/test-setup.ts',
'**/*.spec.ts',
'jest.config.ts',
'**/*.test.ts',
]);

Expand All @@ -933,6 +941,7 @@ describe('lib', () => {
expect(tsConfigLibJson3.exclude).toEqual([
'src/test-setup.ts',
'**/*.spec.ts',
'jest.config.ts',
'**/*.test.ts',
]);
});
Expand Down
6 changes: 6 additions & 0 deletions packages/jest/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
"cli": "nx",
"description": "Update to export default in jest config and revert jest.preset.ts to jest.preset.js",
"factory": "./src/migrations/update-14-1-5/update-exports-jest-config"
},
"exclude-jest-config-from-ts-config": {
"version": "14.5.5-beta.0",
"cli": "nx",
"description": "Exclude jest.config.ts from tsconfig where missing.",
"factory": "./src/migrations/update-14-0-0/update-jest-config-ext"
}
},
"packageJsonUpdates": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,43 @@ module.exports = {
"
`;

exports[`Jest Migration (v14.0.0) should produce the same results when running multiple times 1`] = `
Object {
"files": Array [
"*.ts",
"*.tsx",
"*.js",
"*.jsx",
],
"parserOptions": Object {
"project": Array [
"libs/my-next-proj/tsconfig.*?.json",
],
},
"rules": Object {},
}
`;

exports[`Jest Migration (v14.0.0) should produce the same results when running multiple times 2`] = `
"/* eslint-disable */
module.exports = {
displayName: 'lib-one',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
}
},
transform: {
'^.+\\\\\\\\.[tj]sx?$': 'ts-jest'
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/lib-one'
};
"
`;

exports[`Jest Migration (v14.0.0) should rename project jest.config.js to jest.config.ts 1`] = `
"/* eslint-disable */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,79 @@ describe('Jest Migration (v14.0.0)', () => {
const tsconfigSpec = readJson(tree, 'libs/lib-one/tsconfig.spec.json');
expect(tsconfigSpec.exclude).toEqual(['node_modules']);
});

it('should produce the same results when running multiple times', async () => {
await libSetUp(tree);
updateJson(tree, 'libs/lib-one/tsconfig.lib.json', (json) => {
json.exclude = ['**/*.spec.ts'];
return json;
});
updateJson(tree, 'libs/lib-one/tsconfig.spec.json', (json) => {
json.include = ['**/*.spec.ts'];
return json;
});

await setupNextProj(tree);

const esLintJson = readJson(tree, 'libs/my-next-proj/.eslintrc.json');
// make sure the parserOptions are set correctly for next
expect(esLintJson.overrides[0]).toMatchSnapshot();

await updateJestConfigExt(tree);

assertNextProj(tree);
assertLib(tree);

await updateJestConfigExt(tree);

assertNextProj(tree);
assertLib(tree);

expect(tree.read('libs/lib-one/jest.config.ts', 'utf-8')).toMatchSnapshot();
});
});

async function setupNextProj(tree: Tree) {
await libSetUp(tree, {
...setupDefaults,
libName: 'my-next-proj',
setParserOptionsProject: true,
});
const projectConfig = readProjectConfiguration(tree, 'my-next-proj');
projectConfig.targets['build'] = {
executor: '@nrwl/next:build',
options: {},
};
updateProjectConfiguration(tree, 'my-next-proj', projectConfig);
updateJson(tree, 'libs/my-next-proj/tsconfig.json', (json) => {
// simulate nextJS tsconfig;
json.exclude = ['node_modules'];
return json;
});
}

function assertNextProj(tree: Tree) {
expect(
readJson(tree, 'libs/my-next-proj/tsconfig.spec.json').exclude
).toEqual(['node_modules']);
expect(
readJson(tree, 'libs/my-next-proj/tsconfig.spec.json').include
).toEqual(expect.arrayContaining(['jest.config.ts']));
}

function assertLib(tree: Tree) {
expect(readJson(tree, 'libs/lib-one/tsconfig.json').exclude).toBeFalsy();
expect(readJson(tree, 'libs/lib-one/tsconfig.spec.json').exclude).toBeFalsy();
expect(readJson(tree, 'libs/lib-one/tsconfig.spec.json').include).toEqual([
'**/*.spec.ts',
'jest.config.ts',
]);

expect(readJson(tree, 'libs/lib-one/tsconfig.lib.json').exclude).toEqual([
'**/*.spec.ts',
'jest.config.ts',
]);
expect(readJson(tree, 'libs/lib-one/tsconfig.lib.json').include).toEqual([
'**/*.ts',
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ function updateTsConfig(tree: Tree, tsConfigPath: string) {
function addEsLintIgnoreComments(tree: Tree, filePath: string) {
if (tree.exists(filePath)) {
const contents = tree.read(filePath, 'utf-8');
tree.write(
filePath,
`/* eslint-disable */
if (!contents.startsWith('/* eslint-disable */')) {
tree.write(
filePath,
`/* eslint-disable */
${contents}`
);
);
}
}
}

Expand Down Expand Up @@ -133,6 +135,11 @@ export async function updateJestConfigExt(tree: Tree) {

if (tsConfig.references) {
for (const { path } of tsConfig.references) {
// skip as editor.json should include everything anyway.
if (path.endsWith('tsconfig.editor.json')) {
continue;
}

if (path.endsWith('tsconfig.spec.json')) {
const eslintPath = joinPathFragments(
projectConfig.root,
Expand Down

1 comment on commit bd4e6ac

@vercel
Copy link

@vercel vercel bot commented on bd4e6ac Aug 5, 2022

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-git-master-nrwl.vercel.app
nx.dev
nx-dev-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.