|
1 | | -/* eslint global-require: 0, no-console: 0, import/no-unresolved: 0, import/no-extraneous-dependencies: 0, import/no-dynamic-require: 0, max-len: 0 */ |
2 | | -const path = require('path'); |
3 | | -const fs = require('fs'); |
4 | | -const globby = require('globby'); |
5 | | -const Mocha = require('mocha'); |
6 | | -const options = require('./options'); |
| 1 | +/* eslint global-require: 0, import/no-dynamic-require: 0, object-curly-newline: 0 */ |
| 2 | +const chromeFinder = require('chrome-launcher/dist/chrome-finder'); |
| 3 | +const { getPlatform } = require('chrome-launcher/dist/utils'); |
| 4 | +const { Runner, configure } = require('@after-work.js/node/src/'); |
| 5 | +const nodeOptions = require('@after-work.js/node/src/options'); |
| 6 | +const utils = require('@after-work.js/utils'); |
| 7 | +const puppetOptions = require('./options'); |
7 | 8 |
|
8 | | -process.on('unhandledRejection', (err) => { |
9 | | - console.error(`Promise Rejection:${err}`); |
10 | | -}); |
| 9 | +const options = Object.assign({}, nodeOptions, puppetOptions); |
11 | 10 |
|
12 | | -function runTests(files, mochaOpts) { |
13 | | - return (resolve) => { |
14 | | - const mocha = new Mocha(mochaOpts); |
15 | | - files.forEach(f => mocha.addFile(f)); |
16 | | - mocha.run((failures) => { |
17 | | - resolve(failures); |
18 | | - }); |
19 | | - }; |
20 | | -} |
21 | | -function run(files, mochaOpts) { |
22 | | - return new Promise(runTests(files, mochaOpts)); |
23 | | -} |
| 11 | +const getChromeExecutablePath = async () => { |
| 12 | + const installations = await chromeFinder[getPlatform()](); |
| 13 | + if (installations.length === 0) { |
| 14 | + throw new Error('Chrome not installed'); |
| 15 | + } |
| 16 | + return installations[0]; |
| 17 | +}; |
24 | 18 |
|
25 | 19 | const puppet = { |
26 | 20 | command: ['puppeteer', 'puppet'], |
27 | 21 | desc: 'Run tests with puppeteer', |
28 | 22 | builder(yargs) { |
29 | 23 | return yargs |
30 | 24 | .options(options) |
31 | | - .config('config', (configPath) => { |
32 | | - if (configPath === null) { |
33 | | - return {}; |
34 | | - } |
35 | | - if (!fs.existsSync(configPath)) { |
36 | | - throw new Error(`Config ${configPath} not found`); |
| 25 | + .config('config', configure) |
| 26 | + .coerce('babel', utils.coerceBabel) |
| 27 | + .coerce('nyc', (opt) => { |
| 28 | + if (opt.babel) { |
| 29 | + // opt.require.push('babel-register'); |
| 30 | + opt.sourceMap = false; // eslint-disable-line no-param-reassign |
| 31 | + opt.instrumenter = './lib/instrumenters/noop'; // eslint-disable-line no-param-reassign |
37 | 32 | } |
38 | | - let config = {}; |
39 | | - const foundConfig = require(configPath); |
40 | | - if (typeof foundConfig === 'function') { |
41 | | - config = Object.assign({}, foundConfig()); |
42 | | - } else { |
43 | | - config = Object.assign({}, foundConfig); |
44 | | - } |
45 | | - return config; |
| 33 | + return opt; |
46 | 34 | }); |
47 | 35 | }, |
48 | 36 | handler(argv) { |
49 | | - let puppeteer; |
50 | | - try { |
51 | | - puppeteer = require('puppeteer'); |
52 | | - } catch (_) { |
53 | | - console.log('Could not load puppeteer'); |
54 | | - const p = `${path.resolve(process.cwd())}/node_modules/puppeteer`; |
55 | | - console.log(`Trying: ${p}`); |
56 | | - try { |
57 | | - puppeteer = require(p); |
58 | | - } catch (__) { |
59 | | - console.log('Puppeteer could not be found by after-work.js! Please verify that it has been added as a devDependencies in your package.json'); |
60 | | - process.exit(1); |
61 | | - } |
62 | | - } |
63 | | - const files = globby.sync(argv.glob); |
64 | | - if (!files.length) { |
65 | | - console.log('No test files found for:', argv.glob); |
66 | | - process.exit(1); |
67 | | - } |
68 | 37 | (async function launchAndRun() { |
| 38 | + const puppeteer = require('puppeteer-core'); |
| 39 | + if (argv.presetEnv) { |
| 40 | + require(argv.presetEnv); |
| 41 | + } |
| 42 | + const runner = new Runner(argv); |
| 43 | + if (!argv.chrome.executablePath) { |
| 44 | + argv.chrome.executablePath = await getChromeExecutablePath(); //eslint-disable-line |
| 45 | + } |
69 | 46 | const browser = await puppeteer.launch(argv.chrome); |
70 | 47 | global.browser = browser; |
71 | 48 | global.page = await browser.newPage(); |
72 | | - const failures = await run(files, argv.mocha); |
73 | | - await browser.close(); |
74 | | - process.exit(failures); |
| 49 | + const closeBrowser = () => { |
| 50 | + (async function close() { |
| 51 | + await browser.close(); |
| 52 | + }()); |
| 53 | + }; |
| 54 | + runner.on('onFinished', failures => runner.exit(failures)); |
| 55 | + runner.on('forceExit', closeBrowser); |
| 56 | + runner.exit = (code) => { |
| 57 | + if (argv.watch) { |
| 58 | + return; |
| 59 | + } |
| 60 | + process.exitCode = code; |
| 61 | + closeBrowser(); |
| 62 | + }; |
| 63 | + runner |
| 64 | + .addToMatchSnapshot() |
| 65 | + .autoDetectDebug() |
| 66 | + .setupKeyPress() |
| 67 | + .setTestFiles() |
| 68 | + .setSrcFiles() |
| 69 | + .require() |
| 70 | + .run(); |
75 | 71 | }()); |
76 | 72 | }, |
77 | 73 | }; |
|
0 commit comments