UNPKG

1.78 kBJavaScriptView Raw
1#!/usr/bin/env node
2// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
3// Node module: @loopback/build
4// This file is licensed under the MIT License.
5// License text available at https://opensource.org/licenses/MIT
6
7/*
8========
9
10Usage:
11 node ./bin/run-mocha
12
13========
14*/
15
16'use strict';
17
18function run(argv, options) {
19 const utils = require('./utils');
20
21 const mochaOpts = argv.slice(2);
22
23 const setMochaOpts =
24 !utils.isOptionSet(
25 mochaOpts,
26 '--config', // mocha 6.x
27 '--package', // mocha 6.x
28 '--no-config', // mocha 6.x
29 ) && !utils.mochaConfiguredForProject();
30
31 // Add default options
32 // Keep it backward compatible as dryRun
33 if (typeof options === 'boolean') options = {dryRun: options};
34 options = options || {};
35 if (setMochaOpts) {
36 // Use the default `.mocharc.json` from `@loopback/build`
37 const mochaOptsFile = utils.getConfigFile('.mocharc.json');
38 mochaOpts.unshift('--config', mochaOptsFile);
39 }
40
41 const allowConsoleLogsIx = mochaOpts.indexOf('--allow-console-logs');
42 if (allowConsoleLogsIx === -1) {
43 // Fail any tests that are printing to console.
44 mochaOpts.unshift(
45 '--no-warnings', // Disable node.js warnings
46 '--require',
47 require.resolve('../src/fail-on-console-logs'),
48 );
49 } else {
50 // Allow tests to print to console, remove --allow-console-logs argument
51 mochaOpts.splice(allowConsoleLogsIx, 1);
52 }
53
54 // Allow `--lang en_US.UTF-8`
55 const lang = mochaOpts.indexOf('--lang');
56 if (lang !== -1) {
57 process.env.LANG = mochaOpts[lang + 1];
58 mochaOpts.splice(lang, 2);
59 }
60
61 const args = [...mochaOpts];
62
63 return utils.runCLI('mocha/bin/mocha', args, options);
64}
65
66module.exports = run;
67if (require.main === module) run(process.argv);