UNPKG

2.75 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3const fs = require('fs');
4const path = require('path');
5
6const chalk = require('chalk');
7const meow = require('meow');
8
9const MochaChrome = require('../');
10
11const 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
36if (
37 !cli.input.length &&
38 (!Object.getOwnPropertyNames(cli.flags).length || cli.flags.oldAndBusted)
39) {
40 cli.showHelp();
41}
42
43const [file] = cli.input;
44const { flags } = cli;
45const { log } = console;
46
47let url = file;
48let code = 0;
49
50function fail(message) {
51 log(message);
52 process.exit(1);
53}
54
55if (!file && !file.length) {
56 fail('You must specify a file to run');
57}
58
59if (!/^(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
71const useColors = !!flags.colors;
72const reporter = flags.reporter || 'spec';
73const mocha = Object.assign(JSON.parse(flags.mocha || '{}'), { useColors, reporter });
74const chromeFlags = JSON.parse(flags.chromeFlags || '[]');
75const {
76 chromeLauncher = {},
77 ignoreExceptions,
78 ignoreConsole,
79 ignoreResourceErrors,
80 logLevel
81} = flags;
82const loadTimeout = flags.timeout;
83const options = {
84 chromeFlags,
85 chromeLauncher,
86 ignoreExceptions,
87 ignoreConsole,
88 ignoreResourceErrors,
89 logLevel,
90 mocha,
91 url,
92 loadTimeout
93};
94const runner = new MochaChrome(options);
95
96runner.on('ended', (stats) => {
97 code = stats.failures;
98});
99
100runner.on('failure', (/* message */) => {
101 code = 1;
102});
103
104runner.on('exit', () => process.exit(code));
105
106(async function run() {
107 await runner.connect();
108 await runner.run();
109})();