-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
39f946a
commit fd65792
Showing
5 changed files
with
68 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/); | ||
} | ||
} |