UNPKG

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