run-tests.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* eslint-disable no-console */
  2. const { spawn } = require('child_process');
  3. const { kill } = require('cross-port-killer');
  4. const env = Object.create(process.env);
  5. env.BROWSER = 'none';
  6. env.TEST = true;
  7. // flag to prevent multiple test
  8. let once = false;
  9. const startServer = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['start'], {
  10. env,
  11. });
  12. startServer.stderr.on('data', data => {
  13. // eslint-disable-next-line
  14. console.log(data.toString());
  15. });
  16. startServer.on('exit', () => {
  17. kill(process.env.PORT || 8000);
  18. });
  19. console.log('Starting development server for e2e tests...');
  20. startServer.stdout.on('data', data => {
  21. console.log(data.toString());
  22. // hack code , wait umi
  23. if (
  24. (!once && data.toString().indexOf('Compiled successfully') >= 0) ||
  25. data.toString().indexOf('Theme generated successfully') >= 0
  26. ) {
  27. // eslint-disable-next-line
  28. once = true;
  29. console.log('Development server is started, ready to run tests.');
  30. const testCmd = spawn(
  31. /^win/.test(process.platform) ? 'npm.cmd' : 'npm',
  32. ['test', '--', '--maxWorkers=1', '--runInBand'],
  33. {
  34. stdio: 'inherit',
  35. }
  36. );
  37. testCmd.on('exit', code => {
  38. startServer.kill();
  39. process.exit(code);
  40. });
  41. }
  42. });