Skip to content

Commit

Permalink
feat(testing): add --strict flag for jest-preset-angular test env opts (
Browse files Browse the repository at this point in the history
  • Loading branch information
barbados-clemens committed May 9, 2023
1 parent 5743f15 commit 50ad516
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Tree } from '@nx/devkit';
import { Tree, joinPathFragments } from '@nx/devkit';
import type { NormalizedSchema } from './normalized-schema';

import { jestProjectGenerator } from '@nx/jest';
Expand All @@ -15,5 +15,24 @@ export async function addUnitTestRunner(host: Tree, options: NormalizedSchema) {
skipPackageJson: options.skipPackageJson,
skipFormat: true,
});
const setupFile = joinPathFragments(
options.appProjectRoot,
'src',
'test-setup.ts'
);
if (options.strict && host.exists(setupFile)) {
const contents = host.read(setupFile, 'utf-8');
host.write(
setupFile,
`// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
globalThis.ngJest = {
testEnvironmentOptions: {
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
},
};
${contents}`
);
}
}
}
19 changes: 19 additions & 0 deletions packages/angular/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,25 @@ async function addUnitTestRunner(
skipFormat: true,
skipPackageJson: options.skipPackageJson,
});
const setupFile = joinPathFragments(
options.projectRoot,
'src',
'test-setup.ts'
);
if (options.strict && host.exists(setupFile)) {
const contents = host.read(setupFile, 'utf-8');
host.write(
setupFile,
`// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
globalThis.ngJest = {
testEnvironmentOptions: {
errorOnUnknownElements: true,
errorOnUnknownProperties: true,
},
};
${contents}`
);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default {
"
`;

exports[`jestProject should generate files 1`] = `
exports[`jestProject should generate files 2`] = `
"/* eslint-disable */
export default {
displayName: 'lib1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ describe('jestProject', () => {
project: 'lib1',
setupFile: 'angular',
} as JestProjectSchema);
expect(tree.exists('libs/lib1/src/test-setup.ts')).toBeTruthy();
expect(tree.read('libs/lib1/src/test-setup.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import 'jest-preset-angular/setup-jest';
"
`);
expect(tree.exists('libs/lib1/jest.config.ts')).toBeTruthy();
expect(tree.exists('libs/lib1/tsconfig.spec.json')).toBeTruthy();
expect(tree.read('libs/lib1/jest.config.ts', 'utf-8')).toMatchSnapshot();
Expand Down

1 comment on commit 50ad516

@vercel
Copy link

@vercel vercel bot commented on 50ad516 May 9, 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-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.