forked from aurelia/new
-
Notifications
You must be signed in to change notification settings - Fork 4
/
e2e-test.js
199 lines (175 loc) · 6.03 KB
/
e2e-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// This test is not required by "makes" at all.
// It's to ensure all the skeletons here do (kind of) work.
// The whole test suite takes long long time to finish,
// it's not automatically triggered by "npm version patch".
// Have to run "npm run test:e2e" manually before a release.
const spawn = require('cross-spawn');
const fs = require('fs');
const path = require('path');
const del = require('del');
const test = require('ava');
const puppeteer = require('puppeteer');
const kill = require('tree-kill');
const {possibleFeatureSelections} = require('makes');
const questions = require('./questions');
const allSkeletons = possibleFeatureSelections(questions);
const isWin32 = process.platform === 'win32';
const dir = __dirname;
const folder = path.join(dir, 'test-skeletons');
console.log('-- cleanup ' + folder);
del.sync(folder);
fs.mkdirSync(folder);
// Somehow taskkill on windows would not send SIGTERM signal to proc,
// The proc killed by taskkill got null signal.
const win32Killed = new Set();
function killProc(proc) {
if (isWin32) {
win32Killed.add(proc.pid);
}
proc.stdin.pause();
kill(proc.pid);
}
function run(command, dataCB, errorCB) {
const [cmd, ...args] = command.split(' ');
return new Promise((resolve, reject) => {
const env = Object.create(process.env);
// use CI to turn off automatic browser opening in tasks/run.js
env.CI = 'true';
// need to reset NODE_ENV back to development because this whole
// test is running in NODE_ENV=test which will affect gulp build
env.NODE_ENV = 'development';
const proc = spawn(cmd, args, {env});
proc.on('exit', (code, signal) => {
if (code && signal !== 'SIGTERM' && !win32Killed.has(proc.pid)) {
if (isWin32 && args[1] === 'test:e2e' && code === 3221226356) {
// There is random cypress ELIFECYCLE (3221226356) issue on Windows.
// Probably related to https://github.com/cypress-io/cypress/pull/2011
resolve();
return;
}
reject(new Error(cmd + ' ' + args.join(' ') + ' process exit code: ' + code + ' signal: ' + signal));
} else {
resolve();
}
});
proc.on('error', reject);
proc.stdout.on('data', data => {
process.stdout.write(data);
if (dataCB) {
dataCB(data, () => {
console.log(`-- kill "${command}"`);
killProc(proc);
});
}
});
proc.stderr.on('data', data => {
process.stderr.write(data);
if (errorCB) {
errorCB(data, () => {
console.log(`-- kill "${command}"`);
// process.stderr.write(data);
killProc(proc);
});
}
})
});
}
async function takeScreenshot(url, filePath) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
await new Promise(r => setTimeout(r, 6000));
await page.screenshot({path: filePath});
await browser.close();
}
const targetFeatures = (process.env.TARGET_FEATURES || '').toLowerCase().split(',').filter(p => p);
if (!targetFeatures.includes('cypress')) {
targetFeatures.push('cypress');
}
if (!targetFeatures.includes('app-min')) {
// Skipped app-with-router for now
targetFeatures.push('app-min');
}
if (targetFeatures.length) {
console.log('Target features: ', targetFeatures);
}
function getServerRegex(features) {
if (features.includes('webpack')) return /Project is running at (\S+)/;
if (features.includes('parcel')) return /Server running at (\S+)/;
if (features.includes('fuse-box')) return /Development server running (\S+)/;
return /Dev server is started at: (\S+)/;
}
function getStartCommand(features) {
// don't open browser for parcel
if (features.includes('parcel')) return 'npx parcel index.html -p 9000 --no-autoinstall';
return 'npm start';
}
const skeletons = allSkeletons.filter(features =>
targetFeatures.length === 0 || targetFeatures.every(f => features.includes(f))
);
skeletons.forEach((features, i) => {
const appName = features.join('-');
const appFolder = path.join(folder, appName);
const title = `App: ${i + 1}/${skeletons.length} ${appName}`;
const serverRegex = getServerRegex(features);
const startCommand = getStartCommand(features);
test.serial(title, async t => {
console.log(title);
process.chdir(folder);
const makeCmd = `npx makes ${dir} ${appName} -s ${features.join(',')}`;
console.log('-- ' + makeCmd);
await run(makeCmd);
t.pass('made skeleton');
process.chdir(appFolder);
console.log('-- yarn');
await run('yarn');
t.pass('installed deps');
if (!features.includes('no-unit-tests')) {
console.log('-- npm test');
await run('npm test');
t.pass('finished unit tests');
}
console.log('-- npm run build');
await run('npm run build', null,
(data, kill) => {
t.fail('build failed: ' + data.toString());
}
);
t.pass('made dev build');
const distPath = path.join(appFolder, 'dist');
const compiledFiles = fs.readdirSync(distPath);
t.truthy(compiledFiles.length);
console.log('-- ' + startCommand);
await run(startCommand,
async (data, kill) => {
const m = data.toString().match(serverRegex);
if (!m) return;
const url = m[1];
t.pass(m[0]);
try {
if (!process.env.GITHUB_ACTIONS) {
console.log('-- take screenshot');
await takeScreenshot(url, path.join(folder, appName + '.png'));
}
if (isWin32 && features.includes('cypress')) {
// Have to by pass start-server-and-test on win32
// due to cypress issue (3221226356, search above)
console.log('-- npm run cypress');
await run(`npm run cypress`);
}
kill();
} catch (e) {
t.fail(e.message);
kill();
}
}
);
if (!isWin32 && features.includes('cypress')) {
console.log('-- npm run test:e2e');
await run(`npm run test:e2e`);
}
console.log('-- remove folder ' + appName);
process.chdir(folder);
await del(appFolder);
});
});