diff --git a/packages/angular/src/generators/utils/add-jest.spec.ts b/packages/angular/src/generators/utils/add-jest.spec.ts new file mode 100644 index 0000000000000..6066909d6f2ac --- /dev/null +++ b/packages/angular/src/generators/utils/add-jest.spec.ts @@ -0,0 +1,54 @@ +import type { Tree } from '@nx/devkit'; +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import { UnitTestRunner } from '../../utils/test-runners'; +import { addJest } from './add-jest'; +import { generateTestApplication } from './testing'; + +describe('addJest', () => { + let tree: Tree; + + beforeEach(async () => { + tree = createTreeWithEmptyWorkspace(); + await generateTestApplication(tree, { + name: 'app1', + directory: 'app1', + unitTestRunner: UnitTestRunner.None, + skipFormat: true, + }); + }); + + it('generate the test setup file', async () => { + await addJest(tree, { + name: 'app1', + projectRoot: 'app1', + skipPackageJson: false, + strict: false, + }); + + expect(tree.read('app1/src/test-setup.ts', 'utf-8')).toMatchInlineSnapshot(` + "import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; + + setupZoneTestEnv(); + " + `); + }); + + it('generate the test setup file with strict', async () => { + await addJest(tree, { + name: 'app1', + projectRoot: 'app1', + skipPackageJson: false, + strict: true, + }); + + expect(tree.read('app1/src/test-setup.ts', 'utf-8')).toMatchInlineSnapshot(` + "import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; + + setupZoneTestEnv({ + errorOnUnknownElements: true, + errorOnUnknownProperties: true + }); + " + `); + }); +}); diff --git a/packages/angular/src/generators/utils/add-jest.ts b/packages/angular/src/generators/utils/add-jest.ts index b9fe5e713083c..1365ced1d5265 100644 --- a/packages/angular/src/generators/utils/add-jest.ts +++ b/packages/angular/src/generators/utils/add-jest.ts @@ -53,14 +53,13 @@ export async function addJest( const contents = tree.read(setupFile, 'utf-8'); tree.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}` + contents.replace( + 'setupZoneTestEnv();', + `setupZoneTestEnv({ + errorOnUnknownElements: true, + errorOnUnknownProperties: true +});` + ) ); } } diff --git a/packages/jest/src/generators/configuration/configuration.spec.ts b/packages/jest/src/generators/configuration/configuration.spec.ts index 333a4d3c7a587..449cbc7d42dd7 100644 --- a/packages/jest/src/generators/configuration/configuration.spec.ts +++ b/packages/jest/src/generators/configuration/configuration.spec.ts @@ -55,7 +55,9 @@ describe('jestProject', () => { } as JestProjectSchema); expect(tree.read('libs/lib1/src/test-setup.ts', 'utf-8')) .toMatchInlineSnapshot(` - "import 'jest-preset-angular/setup-jest'; + "import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; + + setupZoneTestEnv(); " `); expect(tree.exists('libs/lib1/jest.config.ts')).toBeTruthy(); diff --git a/packages/jest/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ b/packages/jest/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ index 1100b3e8a6ed0..58c511e08265c 100644 --- a/packages/jest/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ +++ b/packages/jest/src/generators/configuration/files-angular/src/test-setup.ts__tmpl__ @@ -1 +1,3 @@ -import 'jest-preset-angular/setup-jest'; +import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone'; + +setupZoneTestEnv();