1 | #!/usr/bin/env node
|
2 |
|
3 | const fs = require('fs');
|
4 | const path = require('path');
|
5 |
|
6 | const chalk = require('chalk');
|
7 | const meow = require('meow');
|
8 |
|
9 | const MochaChrome = require('../');
|
10 |
|
11 | const cli = meow(chalk`{underline Usage}
|
12 | $ mocha-chrome <file.html> [options]
|
13 |
|
14 |
|
15 | {underline Options}
|
16 | --chrome-flags A JSON string representing an array of flags to pass to Chrome
|
17 | --chrome-launcher Chrome launcher options (see https://github.com/GoogleChrome/chrome-launcher#launchopts)
|
18 | --ignore-console Suppress console logging
|
19 | --ignore-exceptions Suppress exceptions logging
|
20 | --ignore-resource-errors Suppress resource error logging
|
21 | --log-level Specify a log level; trace, debug, info, warn, error
|
22 | --mocha A JSON string representing a config object to pass to Mocha
|
23 | --no-colors Disable colors in Mocha's output
|
24 | --reporter Specify the Mocha reporter to use
|
25 | --timeout Specify the test startup timeout to use
|
26 | --version
|
27 |
|
28 |
|
29 | {underline Examples}
|
30 | $ mocha-chrome test.html --no-colors
|
31 | $ mocha-chrome test.html --reporter dot
|
32 | $ mocha-chrome test.html --mocha '\{"ui":"tdd"\}'
|
33 | $ mocha-chrome test.html --chrome-flags '["--some-flag", "--and-another-one"]'
|
34 | `);
|
35 |
|
36 | if (
|
37 | !cli.input.length &&
|
38 | (!Object.getOwnPropertyNames(cli.flags).length || cli.flags.oldAndBusted)
|
39 | ) {
|
40 | cli.showHelp();
|
41 | }
|
42 |
|
43 | const [file] = cli.input;
|
44 | const { flags } = cli;
|
45 | const { log } = console;
|
46 |
|
47 | let url = file;
|
48 | let code = 0;
|
49 |
|
50 | function fail(message) {
|
51 | log(message);
|
52 | process.exit(1);
|
53 | }
|
54 |
|
55 | if (!file && !file.length) {
|
56 | fail('You must specify a file to run');
|
57 | }
|
58 |
|
59 | if (!/^(file|http(s?)):\/\//.test(file)) {
|
60 | if (!fs.existsSync(file)) {
|
61 | url = `file://${path.resolve(path.join(process.cwd(), file))}`;
|
62 | }
|
63 |
|
64 | if (!fs.existsSync(file)) {
|
65 | fail('You must specify an existing file.');
|
66 | }
|
67 |
|
68 | url = `file://${fs.realpathSync(file)}`;
|
69 | }
|
70 |
|
71 | const useColors = !!flags.colors;
|
72 | const reporter = flags.reporter || 'spec';
|
73 | const mocha = Object.assign(JSON.parse(flags.mocha || '{}'), { useColors, reporter });
|
74 | const chromeFlags = JSON.parse(flags.chromeFlags || '[]');
|
75 | const {
|
76 | chromeLauncher = {},
|
77 | ignoreExceptions,
|
78 | ignoreConsole,
|
79 | ignoreResourceErrors,
|
80 | logLevel
|
81 | } = flags;
|
82 | const loadTimeout = flags.timeout;
|
83 | const options = {
|
84 | chromeFlags,
|
85 | chromeLauncher,
|
86 | ignoreExceptions,
|
87 | ignoreConsole,
|
88 | ignoreResourceErrors,
|
89 | logLevel,
|
90 | mocha,
|
91 | url,
|
92 | loadTimeout
|
93 | };
|
94 | const runner = new MochaChrome(options);
|
95 |
|
96 | runner.on('ended', (stats) => {
|
97 | code = stats.failures;
|
98 | });
|
99 |
|
100 | runner.on('failure', () => {
|
101 | code = 1;
|
102 | });
|
103 |
|
104 | runner.on('exit', () => process.exit(code));
|
105 |
|
106 | (async function run() {
|
107 | await runner.connect();
|
108 | await runner.run();
|
109 | })();
|