Skip to content

Commit

Permalink
chore: suppress node.js warnings to have clean logs
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Dec 14, 2018
1 parent f23df06 commit c47bba2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/build/bin/run-mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ function run(argv, options) {
!utils.mochaOptsFileProjectExists();

// Add default options
// Keep it backward compatible as dryRun
if (typeof options === 'boolean') options = {dryRun: options};
options = options || {};
if (setMochaOpts) {
const mochaOptsFile = utils.getConfigFile('mocha.opts');
mochaOpts.unshift('--opts', mochaOptsFile);
Expand All @@ -36,6 +39,7 @@ function run(argv, options) {
if (allowConsoleLogsIx === -1) {
// Fail any tests that are printing to console.
mochaOpts.unshift(
'--no-warnings', // Disable node.js warnings
'--require',
require.resolve('../src/fail-on-console-logs'),
);
Expand Down
7 changes: 7 additions & 0 deletions packages/build/bin/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function resolveCLI(cli) {
* @param {string} cli Path of the cli command
* @param {string[]} args The arguments
* @param {object} options Options to control dryRun and spawn
* - nodeArgs An array of args for `node`
* - dryRun Controls if the cli will be executed or not. If set
* to true, the command itself will be returned without running it
*/
Expand All @@ -125,6 +126,12 @@ function runCLI(cli, args, options) {
if (options.dryRun) {
return util.format('%s %s', process.execPath, args.join(' '));
}
if (options.nodeArgs) {
debug('node args: %s', options.nodeArgs.join(' '));
// For example, [--no-warnings]
args = options.nodeArgs.concat(args);
}
debug('Spawn %s %s', process.execPath, args.join(' '));
var child = spawn(
process.execPath, // Typically '/usr/local/bin/node'
args,
Expand Down
10 changes: 10 additions & 0 deletions packages/build/src/fail-on-console-logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ console.warn = recordForbiddenCall('warn');
console.error = recordForbiddenCall('error');

const problems = [];
const warnings = [];

function recordForbiddenCall(methodName) {
return function recordForbiddenConsoleUsage(...args) {
Expand All @@ -40,7 +41,16 @@ function recordForbiddenCall(methodName) {
};
}

process.on('warning', warning => {
warnings.push(warning);
});

process.on('exit', code => {
if (!warnings.length) {
for (w of warnings) {
originalConsole.warn(w);
}
}
if (!problems.length) return;
const log = originalConsole.log;

Expand Down

0 comments on commit c47bba2

Please sign in to comment.