From 86b23d7d9ceb92567e793fc528622797361b651e Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 16 Apr 2019 10:55:42 +0200 Subject: [PATCH] test: add size checks to prod build e2e tests --- .../legacy-cli/e2e/tests/build/prod-build.ts | 43 ++++++++++++++++--- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/tests/legacy-cli/e2e/tests/build/prod-build.ts b/tests/legacy-cli/e2e/tests/build/prod-build.ts index 24bc1f39dcff..e4839192591b 100644 --- a/tests/legacy-cli/e2e/tests/build/prod-build.ts +++ b/tests/legacy-cli/e2e/tests/build/prod-build.ts @@ -1,11 +1,30 @@ -import { readdirSync } from 'fs'; +import { statSync } from 'fs'; import { join } from 'path'; import { getGlobalVariable } from '../../utils/env'; -import { expectFileToExist, expectFileToMatch } from '../../utils/fs'; +import { expectFileToExist, expectFileToMatch, readFile } from '../../utils/fs'; import { expectGitToBeClean } from '../../utils/git'; import { ng } from '../../utils/process'; +function verifySize(bundle: string, baselineBytes: number) { + const size = statSync(`dist/test-project/${bundle}`).size; + const percentageBaseline = baselineBytes * 10 / 100; + const maxSize = baselineBytes + percentageBaseline; + const minSize = baselineBytes - percentageBaseline; + + if (size >= maxSize) { + throw new Error( + `Expected ${bundle} size to be less than ${maxSize / 1024}Kb but it was ${size / 1024}Kb.`, + ); + } + + if (size <= minSize) { + throw new Error( + `Expected ${bundle} size to be greater than ${minSize / 1024}Kb but it was ${size / 1024}Kb.`, + ); + } +} + export default async function () { // TODO(architect): Delete this test. It is now in devkit/build-angular. @@ -25,12 +44,22 @@ export default async function () { await expectFileToMatch('dist/test-project/index.html', /styles\.[0-9a-f]{20}\.css/); await expectFileToMatch('dist/test-project/3rdpartylicenses.txt', /MIT/); - const dirContents = readdirSync('./dist/test-project'); - const mainES5 = dirContents.find(name => /main-es5.[a-z0-9]+\.js/.test(name)); - await expectFileToMatch(`dist/test-project/${mainES5}`, bootstrapRegExp); + const indexContent = await readFile('dist/test-project/index.html'); + const mainES5Path = indexContent.match(/src="(main-es5\.[a-z0-9]{0,32}\.js)"/)[1]; + const mainES2015Path = indexContent.match(/src="(main-es2015\.[a-z0-9]{0,32}\.js)"/)[1]; + + // Content checks + await expectFileToMatch(`dist/test-project/${mainES5Path}`, bootstrapRegExp); + await expectFileToMatch(`dist/test-project/${mainES2015Path}`, bootstrapRegExp); - const mainES2015 = dirContents.find(name => /main-es2015.[a-z0-9]+\.js/.test(name)); - await expectFileToMatch(`dist/test-project/${mainES2015}`, bootstrapRegExp); + // Size checks in bytes + if (ivyProject) { + verifySize(mainES5Path, 147789); + verifySize(mainES2015Path, 130153); + } else { + verifySize(mainES5Path, 155523); + verifySize(mainES2015Path, 135394); + } // Check that the process didn't change local files. await expectGitToBeClean();