Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testing): use new test environment function from jest-preset-angular #29054

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions packages/angular/src/generators/utils/add-jest.spec.ts
Original file line number Diff line number Diff line change
@@ -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
});
"
`);
});
});
15 changes: 7 additions & 8 deletions packages/angular/src/generators/utils/add-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});`
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import 'jest-preset-angular/setup-jest';
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';

setupZoneTestEnv();
Loading