Skip to content

Commit

Permalink
test: add proper assertions to several E2E tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
clydin authored and alan-agius4 committed Jun 23, 2024
1 parent 39f946a commit fd65792
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 68 deletions.
9 changes: 7 additions & 2 deletions tests/legacy-cli/e2e/tests/basic/aot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { expectFileToMatch } from '../../utils/fs';
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { ng } from '../../utils/process';

/**
* AOT builds should contain generated component factories
*/
export default async function () {
await ng('build', '--aot=true', '--configuration=development');
await expectFileToMatch('dist/test-project/browser/main.js', 'AppComponent_Factory');
const content = await readFile('dist/test-project/browser/main.js', 'utf-8');
assert.match(content, /AppComponent_Factory/);
}
41 changes: 22 additions & 19 deletions tests/legacy-cli/e2e/tests/basic/build.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { getGlobalVariable } from '../../utils/env';
import { expectFileToMatch } from '../../utils/fs';
import { ng } from '../../utils/process';

const OUTPUT_INDEX_PATH = 'dist/test-project/browser/index.html';

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

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

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

if (!stdout2.includes('Estimated transfer size')) {
throw new Error(
`Expected stdout to contain 'Estimated transfer size' but it did not.\n${stdout2}`,
);
if (usingApplicationBuilder) {
// application builder uses an 8 character hash and a dash as a separator
assert.match(prodIndexContent, /main-[a-zA-Z0-9]{8}\.js/);
} else {
// browser builder uses a 16 character hash and a period as a separator
assert.match(prodIndexContent, /main\.[a-zA-Z0-9]{16}\.js/);
}
}
60 changes: 21 additions & 39 deletions tests/legacy-cli/e2e/tests/basic/command-scope.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,31 @@
import { homedir } from 'os';
import assert from 'node:assert/strict';
import { homedir } from 'node:os';
import { silentNg } from '../../utils/process';
import { expectToFail } from '../../utils/utils';

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

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

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

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

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

// ng generate can only be ran inside.
const { message: ngGenerateFailure } = await expectToFail(() =>
silentNg('generate', 'component', 'foo', '--dry-run'),
);
if (
!ngGenerateFailure.includes(
'This command is not available when running the Angular CLI outside a workspace.',
)
) {
throw new Error('ng generate should have failed when ran outside a workspace.');
}
// ng new can only be ran outside of a workspace
await silentNg('new', 'proj-name', '--dry-run');

// ng new can only be ran outside of a workspace
await silentNg('new', 'proj-name', '--dry-run');

// The version command can be run in and outside of a workspace.
await silentNg('version');
} finally {
process.chdir(originalCwd);
}
// The version command can be run in and outside of a workspace.
await silentNg('version');
}
4 changes: 2 additions & 2 deletions tests/legacy-cli/e2e/tests/basic/e2e.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import assert from 'node:assert/strict';
import { setTimeout } from 'node:timers/promises';
import { silentNg } from '../../utils/process';
import { expectToFail } from '../../utils/utils';

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

// These should work.
await silentNg('e2e', 'test-project');
Expand Down
22 changes: 16 additions & 6 deletions tests/legacy-cli/e2e/tests/basic/run.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import { getGlobalVariable } from '../../utils/env';
import { expectFileToMatch } from '../../utils/fs';
import { silentNg } from '../../utils/process';

const OUTPUT_INDEX_PATH = 'dist/test-project/browser/index.html';

export default async function () {
// Development build
await silentNg('run', 'test-project:build:development');
await expectFileToMatch('dist/test-project/browser/index.html', 'main.js');
// Output index HTML file should reference main JS file
const devIndexContent = await readFile(OUTPUT_INDEX_PATH, 'utf-8');
assert.match(devIndexContent, /main\.js/);

const usingApplicationBuilder = getGlobalVariable('argv')['esbuild'];

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

0 comments on commit fd65792

Please sign in to comment.