Last active
November 30, 2017 19:03
-
-
Save dkarmalita/bc5028eeeffd4b24db6ba36b3c842247 to your computer and use it in GitHub Desktop.
react-intl: redundant console output suppressor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this filter suppress react-intl's redundant console output while not in production | |
class ErrorConsoleFilter { | |
constructor(disabledValue) { | |
this.disabledValue = disabledValue; | |
this.originalConsoleError = console.error; // eslint-disable-line no-console | |
this.outputHandler = this.outputHandler.bind(this); | |
console.error = this.outputHandler; // eslint-disable-line no-console | |
} | |
outputHandler(...args) { | |
if (args[0].indexOf(this.disabledValue) === 0) return; | |
this.originalConsoleError.call(console, ...args); | |
} | |
} | |
export const filterRedundandMessages = (on) => { | |
if (on) return new ErrorConsoleFilter('[React Intl] Missing message:'); | |
return null; | |
}; | |
export const getProductionState = () => process.env.NODE_ENV === 'production'; | |
filterRedundandMessages(!getProductionState); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { expect } from 'chai'; | |
import { filterRedundandMessages, getProductionState } from './filter'; | |
describe('Unit: i18n.config', () => { | |
it('getProductionState', () => { | |
expect(getProductionState()).to.be.eql(false); | |
}); | |
describe('Redundant message filter', () => { | |
const consoleFilter = filterRedundandMessages(true); | |
let consoleOutput = ''; | |
let consoleOrigin = null; | |
const consoleMock = (...args) => { consoleOutput = args; }; | |
before(() => { | |
consoleOrigin = consoleFilter.originalConsoleError; | |
consoleFilter.originalConsoleError = consoleMock; | |
}); | |
after(() => { | |
consoleFilter.originalConsoleError = consoleOrigin; | |
}); | |
beforeEach(() => { | |
consoleOutput = ''; | |
}); | |
it('disabled message', () => { | |
const disabledMessage = '[React Intl] Missing message: disabled message'; | |
console.error(disabledMessage); // eslint-disable-line no-console | |
expect(consoleOutput).to.be.eql(''); | |
}); | |
it('enabled message', () => { | |
const enabledMessage = 'enabled message'; | |
console.error(enabledMessage); // eslint-disable-line no-console | |
expect(consoleOutput).to.be.eql([enabledMessage]); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment