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
#16467)

(cherry picked from commit 50ad516)
  • Loading branch information
barbados-clemens authored and FrozenPandaz committed May 9, 2023
1 parent f6b05e4 commit 0d90bc8
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

0 comments on commit 0d90bc8

Please sign in to comment.