☕️ Classic, reliable, trusted test framework for Node.js and the browser
add more tests for utils.deprecate (#3628)
Christopher Hiller471b7f45e522
diff --git a/.wallaby.js b/.wallaby.jsindex 88620c3e..e23c2a8a 100644--- a/.wallaby.js+++ b/.wallaby.js@@ -17,7 +17,8 @@ module.exports = () => {},'package.json','test/opts/mocha.opts',- 'mocharc.yml'+ 'mocharc.yml',+ '!lib/browser/**/*.js',],filesWithNoCoverageCalculated: ['test/**/*.fixture.js',@@ -52,7 +53,6 @@ module.exports = () => {return execFile.apply(this, arguments);};require('./test/setup');- },- debug: true+ }};};diff --git a/lib/utils.js b/lib/utils.jsindex 71aa7804..43d02faa 100644--- a/lib/utils.js+++ b/lib/utils.js@@ -588,28 +588,20 @@ exports.getError = function(err) {** @param {string} msg*/-exports.deprecate = function(msg) {+exports.deprecate = function deprecate(msg) {msg = String(msg);- if (seenDeprecationMsg.hasOwnProperty(msg)) {- return;- }- seenDeprecationMsg[msg] = true;- doDeprecationWarning(msg);-};--var seenDeprecationMsg = {};--var doDeprecationWarning = process.emitWarning- ? function(msg) {- // Node.js v6++ if (msg && !deprecate.cache[msg]) {+ deprecate.cache[msg] = true;+ if (process.emitWarning) {process.emitWarning(msg, 'DeprecationWarning');- }- : function(msg) {- // Everything else+ } else {process.nextTick(function() {console.warn(msg);});- };+ }+ }+};+exports.deprecate.cache = {};/*** @summarydiff --git a/package-scripts.js b/package-scripts.jsindex c4081449..0c2a9c50 100644--- a/package-scripts.js+++ b/package-scripts.js@@ -69,7 +69,7 @@ module.exports = {},node: {default: {- script: `nps ${[+ script: `rimraf .nyc_output && nps ${['build','test.node.bdd','test.node.tdd',diff --git a/test/unit/utils.spec.js b/test/unit/utils.spec.jsindex aaed6709..7eeeda3a 100644--- a/test/unit/utils.spec.js+++ b/test/unit/utils.spec.js@@ -1,8 +1,19 @@'use strict';var utils = require('../../lib/utils');+var sinon = require('sinon');describe('lib/utils', function() {+ var sandbox;++ beforeEach(function() {+ sandbox = sinon.createSandbox();+ });++ afterEach(function() {+ sandbox.restore();+ });+describe('clean', function() {it('should remove the wrapping function declaration', function() {expect(@@ -618,4 +629,44 @@ describe('lib/utils', function() {expect(utils.escape('💩'), 'to be', '💩');});});++ describe('deprecate', function() {+ var emitWarning;++ beforeEach(function() {+ if (process.emitWarning) {+ emitWarning = process.emitWarning;+ sandbox.stub(process, 'emitWarning');+ } else {+ process.emitWarning = sandbox.spy();+ }+ utils.deprecate.cache = {};+ });++ afterEach(function() {+ // if this is not set, then we created it, so we should remove it.+ if (!emitWarning) {+ delete process.emitWarning;+ }+ });++ it('should coerce its parameter to a string', function() {+ utils.deprecate(1);+ expect(process.emitWarning, 'to have a call satisfying', [+ '1',+ 'DeprecationWarning'+ ]);+ });++ it('should cache the message', function() {+ utils.deprecate('foo');+ utils.deprecate('foo');+ expect(process.emitWarning, 'was called times', 1);+ });++ it('should ignore falsy messages', function() {+ utils.deprecate('');+ expect(process.emitWarning, 'was not called');+ });+ });});