Skip to content

Commit 20367f4

Browse files
committed
fix e2e webpack test
1 parent e16a42e commit 20367f4

File tree

6 files changed

+54
-14
lines changed

6 files changed

+54
-14
lines changed

packages/webpack/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'reflect-metadata';
2+
13
export * from './codegen'
24
export * from './plugin'
35
export {ngcLoader as default} from './loader'
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {node, ng, npm} from '../utils/process';
2+
3+
4+
export default function() {
5+
return Promise.resolve()
6+
.then(() => console.log('Environment:'))
7+
.then(() => {
8+
Object.keys(process.env).forEach(envName => {
9+
console.log(` ${envName}: ${process.env[envName].replace(/[\n\r]+/g, '\n ')}`);
10+
});
11+
})
12+
.then(() => node('--version'))
13+
.then(() => npm('--version'))
14+
.then(() => ng('version'));
15+
}

tests/e2e/setup/500-create-project.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ import {gitClean, gitCommit} from '../utils/git';
99
export default function(argv: any) {
1010
let createProject = null;
1111

12-
// If we're set to reuse an existing project, just chdir to it and clean it.
12+
// This is a dangerous flag, but is useful for testing packages only.
1313
if (argv.noproject) {
1414
return Promise.resolve();
1515
} else if (argv.reuse) {
16+
// If we're set to reuse an existing project, just chdir to it and clean it.
1617
createProject = Promise.resolve()
1718
.then(() => process.chdir(argv.reuse))
1819
.then(() => gitClean());

tests/e2e/tests/packages/webpack/test.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {copyAssets} from '../../../utils/assets';
2-
import {npm, exec} from '../../../utils/process';
2+
import {exec, silentNpm} from '../../../utils/process';
33
import {updateJsonFile} from '../../../utils/project';
44
import {join} from 'path';
5+
import {expectFileSizeToBeUnder} from '../../../utils/fs';
56

67

78
export default function(argv: any, skipCleaning: () => void) {
@@ -14,9 +15,10 @@ export default function(argv: any, skipCleaning: () => void) {
1415
const dist = '../../../../../dist/';
1516
json['dependencies']['@ngtools/webpack'] = join(__dirname, dist, 'webpack');
1617
}))
17-
.then(() => npm('install'))
18-
.then(() => exec('node_modules/.bin/webpack', '-p', 'webpack.config.js'))
19-
.then(() => console.log(process.cwd()))
18+
.then(() => silentNpm('install'))
19+
.then(() => exec('node_modules/.bin/webpack', '-p'))
20+
.then(() => expectFileSizeToBeUnder('dist/app.main.js', 400000))
21+
.then(() => expectFileSizeToBeUnder('dist/0.app.main.js', 40000))
2022
.then(() => process.chdir(currentDir))
2123
.then(() => skipCleaning());
2224
}

tests/e2e/utils/fs.ts

+9
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,12 @@ export function expectFileToMatch(fileName: string, regEx: RegExp | string) {
119119
}
120120
});
121121
}
122+
123+
export function expectFileSizeToBeUnder(fileName: string, sizeInBytes: number) {
124+
return readFile(fileName)
125+
.then(content => {
126+
if (content.length > sizeInBytes) {
127+
throw new Error(`File "${fileName}" exceeded file size of "${sizeInBytes}".`);
128+
}
129+
});
130+
}

tests/e2e/utils/process.ts

+20-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<strin
2020
));
2121

2222
args = args.filter(x => x !== undefined);
23-
24-
console.log(blue(` Running \`${cmd} ${args.map(x => `"${x}"`).join(' ')}\`...`));
23+
const flags = [
24+
options.silent && 'silent',
25+
options.waitForMatch && `matching(${options.waitForMatch})`
26+
]
27+
.filter(x => !!x) // Remove false and undefined.
28+
.join(', ')
29+
.replace(/^(.+)$/, ' [$1]'); // Proper formatting.
30+
31+
console.log(blue(` Running \`${cmd} ${args.map(x => `"${x}"`).join(' ')}\`${flags}...`));
2532
console.log(blue(` CWD: ${cwd}`));
2633
const spawnOptions: any = {cwd};
2734

@@ -31,8 +38,8 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<strin
3138
spawnOptions['stdio'] = 'pipe';
3239
}
3340

34-
const npmProcess = child_process.spawn(cmd, args, spawnOptions);
35-
npmProcess.stdout.on('data', (data: Buffer) => {
41+
const childProcess = child_process.spawn(cmd, args, spawnOptions);
42+
childProcess.stdout.on('data', (data: Buffer) => {
3643
stdout += data.toString('utf-8');
3744
if (options.silent) {
3845
return;
@@ -42,21 +49,21 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<strin
4249
.filter(line => line !== '')
4350
.forEach(line => console.log(' ' + line));
4451
});
45-
npmProcess.stderr.on('data', (data: Buffer) => {
52+
childProcess.stderr.on('data', (data: Buffer) => {
4653
stderr += data.toString('utf-8');
4754
data.toString('utf-8')
4855
.split(/[\n\r]+/)
4956
.filter(line => line !== '')
5057
.forEach(line => console.error(yellow(' ' + line)));
5158
});
5259

53-
_processes.push(npmProcess);
60+
_processes.push(childProcess);
5461

5562
// Create the error here so the stack shows who called this function.
5663
const err = new Error(`Running "${cmd} ${args.join(' ')}" returned error code `);
5764
return new Promise((resolve, reject) => {
58-
npmProcess.on('exit', (error: any) => {
59-
_processes = _processes.filter(p => p !== npmProcess);
65+
childProcess.on('exit', (error: any) => {
66+
_processes = _processes.filter(p => p !== childProcess);
6067

6168
if (!error) {
6269
resolve(stdout);
@@ -67,7 +74,7 @@ function _exec(options: ExecOptions, cmd: string, args: string[]): Promise<strin
6774
});
6875

6976
if (options.waitForMatch) {
70-
npmProcess.stdout.on('data', (data: Buffer) => {
77+
childProcess.stdout.on('data', (data: Buffer) => {
7178
if (data.toString().match(options.waitForMatch)) {
7279
resolve(stdout);
7380
}
@@ -113,6 +120,10 @@ export function npm(...args: string[]) {
113120
return _exec({}, 'npm', args);
114121
}
115122

123+
export function node(...args: string[]) {
124+
return _exec({}, 'node', args);
125+
}
126+
116127
export function git(...args: string[]) {
117128
return _exec({}, 'git', args);
118129
}

0 commit comments

Comments
 (0)