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