run-tests.js 1.5 KB

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