Skip to content

Instantly share code, notes, and snippets.

@rhysburnie
Last active September 19, 2017 01:06
Show Gist options
  • Save rhysburnie/9c44896eab9a100436112089a4385889 to your computer and use it in GitHub Desktop.
Save rhysburnie/9c44896eab9a100436112089a4385889 to your computer and use it in GitHub Desktop.
Suppress certain console.errors
/* eslint-disable no-console */
const originalConsoleError = console.error;
export function restoreConsoleError() {
console.error = originalConsoleError;
}
export function createSuppressedConsoleError(ignoreThese = []) {
if (console.error === originalConsoleError) {
console.error = (...args) => {
let supress = false;
ignoreThese.forEach(ignore => {
if (args[0].indexOf(ignore) === 0) {
supress = true;
}
});
if (supress) return;
originalConsoleError.call(console, ...args);
};
}
}
// creates a sinon.spy instance
// wrapping console.error and
// adds additional method to it
// (spy.errorCallContainsOneOf)
// NOTE: requires you pass it sinon
export function createConsoleErrorSpy(sinon) {
if (!sinon) {
throw new Error('createConsoleErrorSpy requires you pass sinon to it');
}
const spy = sinon.spy(console, 'error');
spy.errorCallContainsOneOf = (messages = [], callIndex) => {
if (messages.length === 0 || typeof callIndex !== 'number') {
return 'errorsContainsOneOf requires an array of messages and an callIndex number';
}
if (!console.error.getCall) {
// the spy must have been `.restore()`d.
return 'the spy has either had no calls or has been restored';
}
const log = console.error.getCall(callIndex).args[0];
let bool = false;
messages.forEach(msg => {
if (log.indexOf(msg) === 0) {
bool = true;
}
});
return bool;
};
return spy;
}
@rhysburnie
Copy link
Author

rhysburnie commented Sep 19, 2017

IMPORTANT

Turns out when using AVA you can't setup suppression for each test file due to it parallel nature.

So instead in AVA just sett all suppression up in a setup file

Example:

import './setup-browser-env';
import {
  createSuppressedConsoleError,
  restoreConsoleError,
} from './console-utilities';

restoreConsoleError(); // in case previous is kept on --watch (not sure if it does)
createSuppressedConsoleError([
  'Warning: Failed child context type',
  'Warning: Failed context type',
  'Warning: Failed prop type',
]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment