UNPKG

2.48 kBPlain TextView 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 --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
35if (!cli.input.length && !Object.getOwnPropertyNames(cli.flags).length) {
36 cli.showHelp();
37}
38
39const file = cli.input[0];
40const flags = cli.flags;
41let url = file,
42 code = 0;
43
44function fail (message) {
45 console.log(message);
46 process.exit(1);
47}
48
49if (!file && !file.length) {
50 fail('You must specify a file to run');
51}
52
53if (!/^(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
65const useColors = !flags.colors;
66const mocha = Object.assign(JSON.parse(flags.mocha || '{}'), { useColors });
67const chromeFlags = JSON.parse(flags.chromeFlags || '[]');
68const { logLevel, ignoreExceptions, ignoreConsole, ignoreResourceErrors } = flags;
69const options = {
70 chromeFlags,
71 ignoreExceptions,
72 ignoreConsole,
73 ignoreResourceErrors,
74 logLevel,
75 mocha,
76 url
77};
78const runner = new MochaChrome(options);
79
80runner.on('ended', stats => {
81 code = stats.failures;
82});
83
84runner.on('failure', message => {
85 code = 1;
86});
87
88runner.on('exit', () => process.exit(code));
89
90(async function run () {
91 await runner.connect();
92 await runner.run();
93})();