Skip to content

Commit 86b23d7

Browse files
Alan Agiusalexeagle
Alan Agius
authored andcommitted
test: add size checks to prod build e2e tests
1 parent a8b0337 commit 86b23d7

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

tests/legacy-cli/e2e/tests/build/prod-build.ts

+36-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1-
import { readdirSync } from 'fs';
1+
import { statSync } from 'fs';
22
import { join } from 'path';
33
import { getGlobalVariable } from '../../utils/env';
4-
import { expectFileToExist, expectFileToMatch } from '../../utils/fs';
4+
import { expectFileToExist, expectFileToMatch, readFile } from '../../utils/fs';
55
import { expectGitToBeClean } from '../../utils/git';
66
import { ng } from '../../utils/process';
77

88

9+
function verifySize(bundle: string, baselineBytes: number) {
10+
const size = statSync(`dist/test-project/${bundle}`).size;
11+
const percentageBaseline = baselineBytes * 10 / 100;
12+
const maxSize = baselineBytes + percentageBaseline;
13+
const minSize = baselineBytes - percentageBaseline;
14+
15+
if (size >= maxSize) {
16+
throw new Error(
17+
`Expected ${bundle} size to be less than ${maxSize / 1024}Kb but it was ${size / 1024}Kb.`,
18+
);
19+
}
20+
21+
if (size <= minSize) {
22+
throw new Error(
23+
`Expected ${bundle} size to be greater than ${minSize / 1024}Kb but it was ${size / 1024}Kb.`,
24+
);
25+
}
26+
}
27+
928
export default async function () {
1029
// TODO(architect): Delete this test. It is now in devkit/build-angular.
1130

@@ -25,12 +44,22 @@ export default async function () {
2544
await expectFileToMatch('dist/test-project/index.html', /styles\.[0-9a-f]{20}\.css/);
2645
await expectFileToMatch('dist/test-project/3rdpartylicenses.txt', /MIT/);
2746

28-
const dirContents = readdirSync('./dist/test-project');
29-
const mainES5 = dirContents.find(name => /main-es5.[a-z0-9]+\.js/.test(name));
30-
await expectFileToMatch(`dist/test-project/${mainES5}`, bootstrapRegExp);
47+
const indexContent = await readFile('dist/test-project/index.html');
48+
const mainES5Path = indexContent.match(/src="(main-es5\.[a-z0-9]{0,32}\.js)"/)[1];
49+
const mainES2015Path = indexContent.match(/src="(main-es2015\.[a-z0-9]{0,32}\.js)"/)[1];
50+
51+
// Content checks
52+
await expectFileToMatch(`dist/test-project/${mainES5Path}`, bootstrapRegExp);
53+
await expectFileToMatch(`dist/test-project/${mainES2015Path}`, bootstrapRegExp);
3154

32-
const mainES2015 = dirContents.find(name => /main-es2015.[a-z0-9]+\.js/.test(name));
33-
await expectFileToMatch(`dist/test-project/${mainES2015}`, bootstrapRegExp);
55+
// Size checks in bytes
56+
if (ivyProject) {
57+
verifySize(mainES5Path, 147789);
58+
verifySize(mainES2015Path, 130153);
59+
} else {
60+
verifySize(mainES5Path, 155523);
61+
verifySize(mainES2015Path, 135394);
62+
}
3463

3564
// Check that the process didn't change local files.
3665
await expectGitToBeClean();

0 commit comments

Comments
 (0)