Skip to content

Commit fd65792

Browse files
clydinalan-agius4
authored andcommitted
test: add proper assertions to several E2E tests
The aot, build, command-scope, e2e, and run E2E tests now use assertions from the Node.js `assert` builtin module. This reduces the amount of code within each test and improves the overall readability.
1 parent 39f946a commit fd65792

File tree

5 files changed

+68
-68
lines changed

5 files changed

+68
-68
lines changed
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { expectFileToMatch } from '../../utils/fs';
1+
import assert from 'node:assert/strict';
2+
import { readFile } from 'node:fs/promises';
23
import { ng } from '../../utils/process';
34

5+
/**
6+
* AOT builds should contain generated component factories
7+
*/
48
export default async function () {
59
await ng('build', '--aot=true', '--configuration=development');
6-
await expectFileToMatch('dist/test-project/browser/main.js', 'AppComponent_Factory');
10+
const content = await readFile('dist/test-project/browser/main.js', 'utf-8');
11+
assert.match(content, /AppComponent_Factory/);
712
}
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1+
import assert from 'node:assert/strict';
2+
import { readFile } from 'node:fs/promises';
13
import { getGlobalVariable } from '../../utils/env';
2-
import { expectFileToMatch } from '../../utils/fs';
34
import { ng } from '../../utils/process';
45

6+
const OUTPUT_INDEX_PATH = 'dist/test-project/browser/index.html';
7+
58
export default async function () {
69
// Development build
7-
const { stdout: stdout1 } = await ng('build', '--configuration=development');
8-
await expectFileToMatch('dist/test-project/browser/index.html', 'main.js');
10+
const { stdout: stdoutDev } = await ng('build', '--configuration=development');
11+
// Console output should not contain estimated transfer size information
12+
assert.doesNotMatch(stdoutDev, /Estimated transfer size/);
13+
// Output index HTML file should reference main JS file
14+
const devIndexContent = await readFile(OUTPUT_INDEX_PATH, 'utf-8');
15+
assert.match(devIndexContent, /main\.js/);
916

10-
if (stdout1.includes('Estimated transfer size')) {
11-
throw new Error(
12-
`Expected stdout not to contain 'Estimated transfer size' but it did.\n${stdout1}`,
13-
);
14-
}
17+
const usingApplicationBuilder = getGlobalVariable('argv')['esbuild'];
1518

1619
// Production build
17-
const { stdout: stdout2 } = await ng('build');
18-
if (getGlobalVariable('argv')['esbuild']) {
19-
// esbuild uses an 8 character hash and a dash as separator
20-
await expectFileToMatch('dist/test-project/browser/index.html', /main-[a-zA-Z0-9]{8}\.js/);
21-
} else {
22-
await expectFileToMatch('dist/test-project/browser/index.html', /main\.[a-zA-Z0-9]{16}\.js/);
23-
}
20+
const { stdout: stdoutProd } = await ng('build');
21+
// Console output should contain estimated transfer size information
22+
assert.match(stdoutProd, /Estimated transfer size/);
23+
// Output index HTML file should reference main JS file with hashing
24+
const prodIndexContent = await readFile(OUTPUT_INDEX_PATH, 'utf-8');
2425

25-
if (!stdout2.includes('Estimated transfer size')) {
26-
throw new Error(
27-
`Expected stdout to contain 'Estimated transfer size' but it did not.\n${stdout2}`,
28-
);
26+
if (usingApplicationBuilder) {
27+
// application builder uses an 8 character hash and a dash as a separator
28+
assert.match(prodIndexContent, /main-[a-zA-Z0-9]{8}\.js/);
29+
} else {
30+
// browser builder uses a 16 character hash and a period as a separator
31+
assert.match(prodIndexContent, /main\.[a-zA-Z0-9]{16}\.js/);
2932
}
3033
}
Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,31 @@
1-
import { homedir } from 'os';
1+
import assert from 'node:assert/strict';
2+
import { homedir } from 'node:os';
23
import { silentNg } from '../../utils/process';
3-
import { expectToFail } from '../../utils/utils';
44

55
export default async function () {
6-
const originalCwd = process.cwd();
6+
// Run inside workspace
7+
await silentNg('generate', 'component', 'foo', '--dry-run');
78

8-
try {
9-
// Run inside workspace
10-
await silentNg('generate', 'component', 'foo', '--dry-run');
9+
// The version command can be run in and outside of a workspace.
10+
await silentNg('version');
1111

12-
// The version command can be run in and outside of a workspace.
13-
await silentNg('version');
12+
assert.rejects(
13+
silentNg('new', 'proj-name', '--dry-run'),
14+
/This command is not available when running the Angular CLI inside a workspace\./,
15+
);
1416

15-
const { message: ngNewFailure } = await expectToFail(() =>
16-
silentNg('new', 'proj-name', '--dry-run'),
17-
);
18-
if (
19-
!ngNewFailure.includes(
20-
'This command is not available when running the Angular CLI inside a workspace.',
21-
)
22-
) {
23-
throw new Error('ng new should have failed when ran inside a workspace.');
24-
}
17+
// Change CWD to run outside a workspace.
18+
process.chdir(homedir());
2519

26-
// Chnage CWD to run outside a workspace.
27-
process.chdir(homedir());
20+
// ng generate can only be ran inside.
21+
assert.rejects(
22+
silentNg('generate', 'component', 'foo', '--dry-run'),
23+
/This command is not available when running the Angular CLI outside a workspace\./,
24+
);
2825

29-
// ng generate can only be ran inside.
30-
const { message: ngGenerateFailure } = await expectToFail(() =>
31-
silentNg('generate', 'component', 'foo', '--dry-run'),
32-
);
33-
if (
34-
!ngGenerateFailure.includes(
35-
'This command is not available when running the Angular CLI outside a workspace.',
36-
)
37-
) {
38-
throw new Error('ng generate should have failed when ran outside a workspace.');
39-
}
26+
// ng new can only be ran outside of a workspace
27+
await silentNg('new', 'proj-name', '--dry-run');
4028

41-
// ng new can only be ran outside of a workspace
42-
await silentNg('new', 'proj-name', '--dry-run');
43-
44-
// The version command can be run in and outside of a workspace.
45-
await silentNg('version');
46-
} finally {
47-
process.chdir(originalCwd);
48-
}
29+
// The version command can be run in and outside of a workspace.
30+
await silentNg('version');
4931
}

tests/legacy-cli/e2e/tests/basic/e2e.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import assert from 'node:assert/strict';
12
import { setTimeout } from 'node:timers/promises';
23
import { silentNg } from '../../utils/process';
3-
import { expectToFail } from '../../utils/utils';
44

55
export default async function () {
6-
await expectToFail(() => silentNg('e2e', 'test-project', '--dev-server-target='));
6+
assert.rejects(silentNg('e2e', 'test-project', '--dev-server-target='));
77

88
// These should work.
99
await silentNg('e2e', 'test-project');
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1+
import assert from 'node:assert/strict';
2+
import { readFile } from 'node:fs/promises';
13
import { getGlobalVariable } from '../../utils/env';
2-
import { expectFileToMatch } from '../../utils/fs';
34
import { silentNg } from '../../utils/process';
45

6+
const OUTPUT_INDEX_PATH = 'dist/test-project/browser/index.html';
7+
58
export default async function () {
69
// Development build
710
await silentNg('run', 'test-project:build:development');
8-
await expectFileToMatch('dist/test-project/browser/index.html', 'main.js');
11+
// Output index HTML file should reference main JS file
12+
const devIndexContent = await readFile(OUTPUT_INDEX_PATH, 'utf-8');
13+
assert.match(devIndexContent, /main\.js/);
14+
15+
const usingApplicationBuilder = getGlobalVariable('argv')['esbuild'];
916

1017
// Production build
1118
await silentNg('run', 'test-project:build');
12-
if (getGlobalVariable('argv')['esbuild']) {
13-
// esbuild uses an 8 character hash and a dash as separator
14-
await expectFileToMatch('dist/test-project/browser/index.html', /main-[a-zA-Z0-9]{8}\.js/);
19+
// Output index HTML file should reference main JS file with hashing
20+
const prodIndexContent = await readFile(OUTPUT_INDEX_PATH, 'utf-8');
21+
if (usingApplicationBuilder) {
22+
// application builder uses an 8 character hash and a dash as a separator
23+
assert.match(prodIndexContent, /main-[a-zA-Z0-9]{8}\.js/);
1524
} else {
16-
await expectFileToMatch('dist/test-project/browser/index.html', /main\.[a-zA-Z0-9]{16}\.js/);
25+
// browser builder uses a 16 character hash and a period as a separator
26+
assert.match(prodIndexContent, /main\.[a-zA-Z0-9]{16}\.js/);
1727
}
1828
}

0 commit comments

Comments
 (0)