diff --git a/packages/angular/src/generators/application/lib/add-unit-test-runner.ts b/packages/angular/src/generators/application/lib/add-unit-test-runner.ts index 17f782cd04fc8..f48d1cadfe855 100644 --- a/packages/angular/src/generators/application/lib/add-unit-test-runner.ts +++ b/packages/angular/src/generators/application/lib/add-unit-test-runner.ts @@ -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'; @@ -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}` + ); + } } } diff --git a/packages/angular/src/generators/library/library.ts b/packages/angular/src/generators/library/library.ts index e246dd9b21d54..8a880a70979aa 100644 --- a/packages/angular/src/generators/library/library.ts +++ b/packages/angular/src/generators/library/library.ts @@ -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}` + ); + } } } diff --git a/packages/jest/src/generators/jest-project/__snapshots__/jest-project.spec.ts.snap b/packages/jest/src/generators/jest-project/__snapshots__/jest-project.spec.ts.snap index 1220dd07803ff..d82db950869ed 100644 --- a/packages/jest/src/generators/jest-project/__snapshots__/jest-project.spec.ts.snap +++ b/packages/jest/src/generators/jest-project/__snapshots__/jest-project.spec.ts.snap @@ -81,7 +81,7 @@ export default { " `; -exports[`jestProject should generate files 1`] = ` +exports[`jestProject should generate files 2`] = ` "/* eslint-disable */ export default { displayName: 'lib1', diff --git a/packages/jest/src/generators/jest-project/jest-project.spec.ts b/packages/jest/src/generators/jest-project/jest-project.spec.ts index 2b9d21c4eaf2d..190019142b747 100644 --- a/packages/jest/src/generators/jest-project/jest-project.spec.ts +++ b/packages/jest/src/generators/jest-project/jest-project.spec.ts @@ -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();