UNPKG

2.14 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-resource-errors Suppress resource error logging
22
23
24{underline Examples}
25 $ mocha-chrome test.html --no-colors
26 $ mocha-chrome test.html --reporter dot
27 $ mocha-chrome test.html --mocha '\{"ui":"tdd"\}'
28`);
29
30if (!cli.input.length && !Object.getOwnPropertyNames(cli.flags).length) {
31 cli.showHelp();
32}
33
34const file = cli.input[0];
35const flags = cli.flags;
36let url = file,
37 code = 0;
38
39function fail (message) {
40 console.log(message);
41 process.exit(1);
42}
43
44if (!file && !file.length) {
45 fail('You must specify a file to run');
46}
47
48if (!/^(file|http(s?)):\/\//.test(file)) {
49 if (!fs.existsSync(file)) {
50 url = 'file://' + path.resolve(path.join(process.cwd(), file));
51 }
52
53 if (!fs.existsSync(file)) {
54 fail('You must specify an existing file.');
55 }
56
57 url = 'file://' + fs.realpathSync(file);
58}
59
60const mochaDefaults = {
61 reporter: flags.reporter || 'spec',
62 useColors: !flags.colors
63};
64const mocha = deepAssign(mochaDefaults, JSON.parse(flags.mocha || '{}'));
65const logLevel = flags.logLevel || 'error';
66const ignoreResourceErrors = flags.ignoreResourceErrors || false
67const options = { url, logLevel, mocha, ignoreResourceErrors };
68const runner = new MochaChrome(options);
69
70runner.on('ended', stats => {
71 code = stats.failures;
72});
73
74runner.on('failure', message => {
75 code = 1;
76});
77
78runner.on('exit', () => process.exit(code));
79
80(async function run () {
81 await runner.connect();
82 await runner.run();
83})();