runner.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* eslint-disable no-undef, no-process-exit */
  2. const fs = require('fs');
  3. const path = require('path');
  4. const mkdirp = require('mkdirp');
  5. const puppeteer = require('puppeteer');
  6. const webpack = require('webpack');
  7. const config = require('../../webpack.config');
  8. const middleware = require('webpack-dev-middleware');
  9. const express = require('express');
  10. const devRoutes = require('../../server/bin/test');
  11. const app = express();
  12. const wpm = middleware(webpack(config(null, { mode: 'development' })), {
  13. logLevel: 'silent'
  14. });
  15. app.use(wpm);
  16. devRoutes(app, { middleware: wpm });
  17. // eslint-disable-next-line no-unused-vars
  18. function onConsole(msg) {
  19. // uncomment to debug
  20. // console.error(msg.text());
  21. }
  22. const server = app.listen(async function() {
  23. let exitCode = -1;
  24. const browser = await puppeteer.launch({
  25. args: [
  26. // puppeteer >= 1.10.0 crashes on Circle CI without this flag set
  27. '--no-sandbox'
  28. ]
  29. });
  30. try {
  31. const page = await browser.newPage();
  32. page.on('console', onConsole);
  33. page.on('pageerror', console.log.bind(console));
  34. await page.goto(`http://127.0.0.1:${server.address().port}/test`);
  35. await page.waitFor(() => typeof runner.testResults !== 'undefined', {
  36. polling: 1000,
  37. timeout: 15000
  38. });
  39. const results = await page.evaluate(() => runner.testResults);
  40. const coverage = await page.evaluate(() => __coverage__);
  41. if (coverage) {
  42. const dir = path.resolve(__dirname, '../../.nyc_output');
  43. mkdirp.sync(dir);
  44. fs.writeFileSync(
  45. path.resolve(dir, 'frontend.json'),
  46. JSON.stringify(coverage)
  47. );
  48. }
  49. const stats = results.stats;
  50. exitCode = stats.failures;
  51. console.log(`${stats.passes} passing (${stats.duration}ms)\n`);
  52. if (stats.failures) {
  53. console.log('Failures:\n');
  54. for (const f of results.failures) {
  55. console.log(`${f.fullTitle}`);
  56. console.log(` ${f.err.stack}\n`);
  57. }
  58. }
  59. } catch (e) {
  60. console.log(e);
  61. } finally {
  62. browser.close();
  63. server.close(() => {
  64. process.exit(exitCode);
  65. });
  66. }
  67. });