run-tests.js 1.07 KB
Newer Older
偏右's avatar
偏右 committed
1 2 3 4 5
const { spawn } = require('child_process');
const { kill } = require('cross-port-killer');

const env = Object.create(process.env);
env.BROWSER = 'none';
sorrycc's avatar
sorrycc committed
6
env.TEST = true;
7 8 9
// flag to prevent multiple test
let once = false;

偏右's avatar
偏右 committed
10
const startServer = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['start'], {
偏右's avatar
偏右 committed
11 12 13
  env,
});

jim's avatar
jim committed
14
startServer.stderr.on('data', data => {
偏右's avatar
偏右 committed
15
  // eslint-disable-next-line
afc163's avatar
afc163 committed
16
  console.log(data.toString());
偏右's avatar
偏右 committed
17 18 19 20 21 22 23 24
});

startServer.on('exit', () => {
  kill(process.env.PORT || 8000);
});

// eslint-disable-next-line
console.log('Starting development server for e2e tests...');
jim's avatar
jim committed
25
startServer.stdout.on('data', data => {
afc163's avatar
afc163 committed
26 27
  // eslint-disable-next-line
  console.log(data.toString());
28
  if (!once && data.toString().indexOf('App running at') >= 0) {
偏右's avatar
偏右 committed
29
    // eslint-disable-next-line
30
    once = true;
偏右's avatar
偏右 committed
31
    console.log('Development server is started, ready to run tests.');
偏右's avatar
偏右 committed
32
    const testCmd = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['test'], {
偏右's avatar
偏右 committed
33 34
      stdio: 'inherit',
    });
jim's avatar
jim committed
35
    testCmd.on('exit', code => {
偏右's avatar
偏右 committed
36
      startServer.kill();
jim's avatar
jim committed
37
      process.exit(code);
偏右's avatar
偏右 committed
38 39 40
    });
  }
});