1 | #!/usr/bin/env node
|
2 | 'use strict';
|
3 |
|
4 | const chalk = require('chalk');
|
5 | const deepAssign = require('deep-assign');
|
6 | const fs = require('fs');
|
7 | const meow = require('meow');
|
8 | const path = require('path');
|
9 |
|
10 | const MochaChrome = require('../');
|
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 | --ignore-console Suppress console logging
|
18 | --ignore-exceptions Suppress exceptions logging
|
19 | --ignore-resource-errors Suppress resource error logging
|
20 | --log-level Specify a log level; trace, debug, info, warn, error
|
21 | --mocha A JSON string representing a config object to pass to Mocha
|
22 | --no-colors Disable colors in Mocha's output
|
23 | --old-and-busted Take pity upon users of old-node. This option will run moche-chrome
|
24 | under Node < 8 using babel-register. Use at your own risk, and
|
25 | without support.
|
26 | --reporter Specify the Mocha reporter to use
|
27 | --timeout Specify the test startup timeout to use
|
28 | --version
|
29 |
|
30 |
|
31 | {underline Examples}
|
32 | $ mocha-chrome test.html --no-colors
|
33 | $ mocha-chrome test.html --reporter dot
|
34 | $ mocha-chrome test.html --mocha '\{"ui":"tdd"\}'
|
35 | $ mocha-chrome test.html --chrome-flags '["--some-flag", "--and-another-one"]'
|
36 | `);
|
37 |
|
38 | if (!cli.input.length && (!Object.getOwnPropertyNames(cli.flags).length || cli.flags.oldAndBusted)) {
|
39 | cli.showHelp();
|
40 | }
|
41 |
|
42 | const file = cli.input[0];
|
43 | const flags = cli.flags;
|
44 | let url = file,
|
45 | code = 0;
|
46 |
|
47 | function fail (message) {
|
48 | console.log(message);
|
49 | process.exit(1);
|
50 | }
|
51 |
|
52 | if (!file && !file.length) {
|
53 | fail('You must specify a file to run');
|
54 | }
|
55 |
|
56 | if (!/^(file|http(s?)):\/\//.test(file)) {
|
57 | if (!fs.existsSync(file)) {
|
58 | url = 'file://' + path.resolve(path.join(process.cwd(), file));
|
59 | }
|
60 |
|
61 | if (!fs.existsSync(file)) {
|
62 | fail('You must specify an existing file.');
|
63 | }
|
64 |
|
65 | url = 'file://' + fs.realpathSync(file);
|
66 | }
|
67 |
|
68 | const useColors = !flags.colors;
|
69 | const reporter = flags.reporter || 'spec';
|
70 | const mocha = Object.assign(JSON.parse(flags.mocha || '{}'), { useColors, reporter });
|
71 | const chromeFlags = JSON.parse(flags.chromeFlags || '[]');
|
72 | const { logLevel, ignoreExceptions, ignoreConsole, ignoreResourceErrors } = flags;
|
73 | const options = {
|
74 | chromeFlags,
|
75 | ignoreExceptions,
|
76 | ignoreConsole,
|
77 | ignoreResourceErrors,
|
78 | logLevel,
|
79 | mocha,
|
80 | url
|
81 | };
|
82 | const runner = new MochaChrome(options);
|
83 |
|
84 | runner.on('ended', stats => {
|
85 | code = stats.failures;
|
86 | });
|
87 |
|
88 | runner.on('failure', message => {
|
89 | code = 1;
|
90 | });
|
91 |
|
92 | runner.on('exit', () => process.exit(code));
|
93 |
|
94 | (async function run () {
|
95 | await runner.connect();
|
96 | await runner.run();
|
97 | })();
|