run-tests.js 1.36 KB
Newer Older
1 2 3
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable eslint-comments/no-unlimited-disable */
偏右's avatar
偏右 committed
4 5 6 7 8
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
9
env.TEST = true;
10 11 12
// flag to prevent multiple test
let once = false;

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

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

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

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