Skip to content

Commit 38e29b9

Browse files
authored
feat(angular): use new test environment function from jest-preset-angular (#29169)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior <!-- This is the behavior we have today --> ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #29165
1 parent 54dbbfd commit 38e29b9

File tree

4 files changed

+67
-10
lines changed

4 files changed

+67
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { Tree } from '@nx/devkit';
2+
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
3+
import { UnitTestRunner } from '../../utils/test-runners';
4+
import { addJest } from './add-jest';
5+
import { generateTestApplication } from './testing';
6+
7+
describe('addJest', () => {
8+
let tree: Tree;
9+
10+
beforeEach(async () => {
11+
tree = createTreeWithEmptyWorkspace();
12+
await generateTestApplication(tree, {
13+
name: 'app1',
14+
directory: 'app1',
15+
unitTestRunner: UnitTestRunner.None,
16+
skipFormat: true,
17+
});
18+
});
19+
20+
it('generate the test setup file', async () => {
21+
await addJest(tree, {
22+
name: 'app1',
23+
projectRoot: 'app1',
24+
skipPackageJson: false,
25+
strict: false,
26+
});
27+
28+
expect(tree.read('app1/src/test-setup.ts', 'utf-8')).toMatchInlineSnapshot(`
29+
"import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
30+
31+
setupZoneTestEnv();
32+
"
33+
`);
34+
});
35+
36+
it('generate the test setup file with strict', async () => {
37+
await addJest(tree, {
38+
name: 'app1',
39+
projectRoot: 'app1',
40+
skipPackageJson: false,
41+
strict: true,
42+
});
43+
44+
expect(tree.read('app1/src/test-setup.ts', 'utf-8')).toMatchInlineSnapshot(`
45+
"import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
46+
47+
setupZoneTestEnv({
48+
errorOnUnknownElements: true,
49+
errorOnUnknownProperties: true
50+
});
51+
"
52+
`);
53+
});
54+
});

packages/angular/src/generators/utils/add-jest.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,13 @@ export async function addJest(
5353
const contents = tree.read(setupFile, 'utf-8');
5454
tree.write(
5555
setupFile,
56-
`// @ts-expect-error https://thymikee.github.io/jest-preset-angular/docs/getting-started/test-environment
57-
globalThis.ngJest = {
58-
testEnvironmentOptions: {
59-
errorOnUnknownElements: true,
60-
errorOnUnknownProperties: true,
61-
},
62-
};
63-
${contents}`
56+
contents.replace(
57+
'setupZoneTestEnv();',
58+
`setupZoneTestEnv({
59+
errorOnUnknownElements: true,
60+
errorOnUnknownProperties: true
61+
});`
62+
)
6463
);
6564
}
6665
}

packages/jest/src/generators/configuration/configuration.spec.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ describe('jestProject', () => {
5555
} as JestProjectSchema);
5656
expect(tree.read('libs/lib1/src/test-setup.ts', 'utf-8'))
5757
.toMatchInlineSnapshot(`
58-
"import 'jest-preset-angular/setup-jest';
58+
"import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
59+
60+
setupZoneTestEnv();
5961
"
6062
`);
6163
expect(tree.exists('libs/lib1/jest.config.ts')).toBeTruthy();
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
import 'jest-preset-angular/setup-jest';
1+
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
2+
3+
setupZoneTestEnv();

0 commit comments

Comments
 (0)