-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-tests.js
More file actions
79 lines (64 loc) · 2.45 KB
/
run-tests.js
File metadata and controls
79 lines (64 loc) · 2.45 KB
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
/* eslint-disable import/no-unresolved, import/no-dynamic-require, import/no-extraneous-dependencies */
if (process.argv.length !== 3) {
console.log('Usage: node run-tests.js <config>');
}
const webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const EventEmitter = require('events');
const fs = require('fs');
const path = require('path');
const Runner = require('@funboxteam/frontend-tests-runner');
const { isPortFree, findFreePort } = require('@funboxteam/free-port-finder');
const config = require(path.resolve(process.argv[2]));
const webpackConfig = require(path.resolve(__dirname, '../webpack.app.test.js'));
const project = new EventEmitter();
let resolveInitialBuild;
// Pass PhantomJS arguments through the env variable
process.env.BROWSER_ARGS = JSON.stringify(config.browserArgs);
project.build = () => {
if (config.live) {
setBaseUrl();
return isPortFree(webpackConfig.devServer.port).then(isFree => (isFree ? build() : Promise.resolve()));
}
return findFreePort(webpackConfig.devServer.port).then(port => {
console.log(`Free port found: ${port}`);
webpackConfig.devServer.port = port;
setBaseUrl();
return build();
});
};
function setBaseUrl() {
// Pass env variable BASE_URL to make it possible for E2E tests to find the app URL
process.env.BASE_URL = `http://localhost:${webpackConfig.devServer.port}`;
console.log(`BASE_URL: ${process.env.BASE_URL}`);
}
// Here we assume that @funboxteam/rebuild-in-progress-webpack-plugin is used for live mode
const rebuildInProgressFile = path.resolve(__dirname, '../../node_modules/.rebuildInProgress');
fs.watch(path.dirname(rebuildInProgressFile), (eventType, filename) => {
if (eventType === 'rename' && filename === path.basename(rebuildInProgressFile)) {
if (fs.existsSync(rebuildInProgressFile)) {
project.emit('buildStart');
} else {
if (resolveInitialBuild) {
resolveInitialBuild();
resolveInitialBuild = null;
}
project.emit('buildFinish');
}
}
});
config.project = project;
const runner = new Runner(config);
runner.start();
function build() {
return new Promise(resolve => {
// For example we use Webpack here
new WebpackDevServer(webpack(webpackConfig), webpackConfig.devServer).listen(webpackConfig.devServer.port, 'localhost', err => {
if (err) {
console.log(`Build error: ${err}`);
process.exit(1);
}
resolveInitialBuild = resolve;
});
});
}