run-tests.js 1.22 KB
Newer Older
1
/* eslint-disable no-console */
偏右's avatar
偏右 committed
2 3 4 5 6
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
7
env.TEST = true;
8 9 10
// flag to prevent multiple test
let once = false;

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

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

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

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
  console.log(data.toString());
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
27 28 29 30 31
  // hack code , wait umi
  if (
    (!once && data.toString().indexOf('Compiled successfully') >= 0) ||
    data.toString().indexOf('Theme generated successfully') >= 0
  ) {
偏右's avatar
偏右 committed
32
    // eslint-disable-next-line
33
    once = true;
偏右's avatar
偏右 committed
34
    console.log('Development server is started, ready to run tests.');
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35 36
    const testCmd = spawn(
      /^win/.test(process.platform) ? 'npm.cmd' : 'npm',
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
      ['test', '--', '--maxWorkers=1', '--runInBand'],
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
38 39 40 41
      {
        stdio: 'inherit',
      }
    );
jim's avatar
jim committed
42
    testCmd.on('exit', code => {
偏右's avatar
偏右 committed
43
      startServer.kill();
jim's avatar
jim committed
44
      process.exit(code);
偏右's avatar
偏右 committed
45 46 47
    });
  }
});