Skip to content

Commit bcba41f

Browse files
dgp1130angular-robot[bot]
authored andcommitted
refactor: add initial Jest e2e test
This test generates an `ng new` project, updates it to use Jest for test executions, and then runs `ng test` and checks for the experimental notice.
1 parent 210b613 commit bcba41f

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { applyJestBuilder } from '../../utils/jest';
2+
import { ng } from '../../utils/process';
3+
4+
export default async function (): Promise<void> {
5+
await applyJestBuilder();
6+
7+
const { stderr } = await ng('test');
8+
9+
if (!stderr.includes('Jest builder is currently EXPERIMENTAL')) {
10+
throw new Error(`No experimental notice in stderr.\nSTDERR:\n\n${stderr}`);
11+
}
12+
}

tests/legacy-cli/e2e/utils/jest.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { silentNpm } from './process';
2+
import { updateJsonFile } from './project';
3+
4+
/** Updates the `test` builder in the current workspace to use Jest with the given options. */
5+
export async function applyJestBuilder(
6+
options: {} = {
7+
tsConfig: 'tsconfig.spec.json',
8+
polyfills: ['zone.js', 'zone.js/testing'],
9+
},
10+
): Promise<void> {
11+
await silentNpm('install', 'jest@29.5.0', 'jest-environment-jsdom@29.5.0', '--save-dev');
12+
13+
await updateJsonFile('angular.json', (json) => {
14+
const projects = Object.values(json['projects']);
15+
if (projects.length !== 1) {
16+
throw new Error(
17+
`Expected exactly one project but found ${projects.length} projects named ${Object.keys(
18+
json['projects'],
19+
).join(', ')}`,
20+
);
21+
}
22+
const project = projects[0]! as any;
23+
24+
// Update to Jest builder.
25+
const test = project['architect']['test'];
26+
test['builder'] = '@angular-devkit/build-angular:jest';
27+
test['options'] = options;
28+
});
29+
}

0 commit comments

Comments
 (0)