run-tests.js 1.28 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* eslint-disable @typescript-eslint/no-var-requires */
const { spawn } = require('child_process');
const { kill } = require('cross-port-killer');

const env = Object.create(process.env);
env.BROWSER = 'none';
env.TEST = true;
env.UMI_UI = 'none';
env.PROGRESS = 'none';
// flag to prevent multiple test
let once = false;

const startServer = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['run', 'serve'], {
  env,
});

startServer.stderr.on('data', (data) => {
  // eslint-disable-next-line
TZW's avatar
TZW committed
19
  //console.log(data.toString());
wuhao's avatar
wuhao committed
20 21 22 23 24 25
});

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

TZW's avatar
TZW committed
26
//console.log('Starting development server for e2e tests...');
wuhao's avatar
wuhao committed
27
startServer.stdout.on('data', (data) => {
TZW's avatar
TZW committed
28
  //console.log(data.toString());
wuhao's avatar
wuhao committed
29 30 31 32
  // hack code , wait umi
  if (!once && data.toString().indexOf('Serving your umi project!') >= 0) {
    // eslint-disable-next-line
    once = true;
TZW's avatar
TZW committed
33
    //console.log('Development server is started, ready to run tests.');
wuhao's avatar
wuhao committed
34 35 36 37 38 39 40 41
    const testCmd = spawn(
      /^win/.test(process.platform) ? 'npm.cmd' : 'npm',
      ['run', 'playwright'],
      {
        stdio: 'inherit',
      },
    );
    testCmd.on('exit', (code) => {
TZW's avatar
TZW committed
42
      //console.log('服务已经退出,退出码:', code);
wuhao's avatar
wuhao committed
43 44 45 46 47
      startServer.kill();
      process.exit(code);
    });
  }
});