Commit 62b99843 authored by Robert Knight's avatar Robert Knight

Add tests for `warnOnce` utility

parent e5824a34
'use strict';
const warnOnce = require('../warn-once');
describe('warnOnce', () => {
beforeEach(() => {
sinon.stub(console, 'warn');
warnOnce.reset();
});
afterEach(() => {
console.warn.restore();
});
it('outputs a warning only the first time a given string is passed', () => {
warnOnce('something is fishy');
assert.calledWith(console.warn, 'something is fishy');
console.warn.reset();
warnOnce('something is fishy');
assert.notCalled(console.warn);
warnOnce('something else is wrong');
assert.calledWith(console.warn, 'something else is wrong');
});
});
'use strict'; 'use strict';
const shownWarnings = {}; let shownWarnings = {};
/** /**
* Log a warning if it has not already been reported. * Log a warning if it has not already been reported.
* *
* This is useful to avoid spamming the console if a warning is emitted in a
* context that may be called frequently.
*
* @param {string} warning * @param {string} warning
*/ */
function warnOnce(warning) { function warnOnce(warning) {
...@@ -15,4 +18,8 @@ function warnOnce(warning) { ...@@ -15,4 +18,8 @@ function warnOnce(warning) {
shownWarnings[warning] = true; shownWarnings[warning] = true;
} }
warnOnce.reset = () => {
shownWarnings = {};
};
module.exports = warnOnce; module.exports = warnOnce;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment