Skip to content

Commit

Permalink
add more tests for utils.deprecate (#3628)
Browse files Browse the repository at this point in the history
* ensure coverage data not cached between execution calls to npm test

* wallaby: disable debug mode in and ignore lib/browser/ [ci skip]

* refactor utils.deprecate and add tests to get coverage up

- `utils.deprecate()` needs a non-empty message
- it no longer shims `process.emitWarning`, since we dropped Node v4 support
- added tests because coverage
  • Loading branch information
boneskull authored Dec 30, 2018
1 parent 273f479 commit 471b7f4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 21 deletions.
6 changes: 3 additions & 3 deletions .wallaby.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = () => {
},
'package.json',
'test/opts/mocha.opts',
'mocharc.yml'
'mocharc.yml',
'!lib/browser/**/*.js',
],
filesWithNoCoverageCalculated: [
'test/**/*.fixture.js',
Expand Down Expand Up @@ -52,7 +53,6 @@ module.exports = () => {
return execFile.apply(this, arguments);
};
require('./test/setup');
},
debug: true
}
};
};
26 changes: 9 additions & 17 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

/**
* @summary
Expand Down
2 changes: 1 addition & 1 deletion package-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports = {
},
node: {
default: {
script: `nps ${[
script: `rimraf .nyc_output && nps ${[
'build',
'test.node.bdd',
'test.node.tdd',
Expand Down
51 changes: 51 additions & 0 deletions test/unit/utils.spec.js
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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');
});
});
});

0 comments on commit 471b7f4

Please sign in to comment.