Skip to content

Commit

Permalink
Introduce isUndefined, deprecate defined (#93)
Browse files Browse the repository at this point in the history
* feat(isUndefined): add new assetion isUndefined

check if an actual value is undefined

See: #89 #87

* chore: update commons package

to use deprecated wrapper

* deprecate(defined): add warning wrapper
  • Loading branch information
mgred committed Mar 6, 2019
1 parent 0f36c31 commit c8f8951
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 5 deletions.
9 changes: 8 additions & 1 deletion lib/assertions/defined.js
@@ -1,11 +1,18 @@
"use strict";

var actualAndTypeOfMessageValues = require("../actual-and-type-of-message-values");
var deprecated = require("@sinonjs/commons").deprecated;

module.exports = function(referee) {
function definedAssert(actual) {
return typeof actual !== "undefined";
}
referee.add("defined", {
assert: function(actual) {
return typeof actual !== "undefined";
return deprecated.wrap(
definedAssert,
"defined is deprecated and will be removed in the future. Please use isUndefined instead."
)(actual);
},
assertMessage: "${customMessage}Expected to be defined",
refuteMessage:
Expand Down
17 changes: 17 additions & 0 deletions lib/assertions/is-undefined.js
@@ -0,0 +1,17 @@
"use strict";

var actualAndTypeOfMessageValues = require("../actual-and-type-of-message-values");

module.exports = function(referee) {
referee.add("isUndefined", {
assert: function(actual) {
return typeof actual === "undefined";
},
assertMessage:
"${customMessage}Expected ${actual} (${actualType}) to be undefined",
refuteMessage:
"${customMessage}Expected ${actual} (${actualType}) not to be undefined",
expectation: "toBeUndefined",
values: actualAndTypeOfMessageValues
});
};
38 changes: 38 additions & 0 deletions lib/assertions/is-undefined.test.js
@@ -0,0 +1,38 @@
"use strict";

var testHelper = require("../test-helper");

testHelper.assertionTests("assert", "isUndefined", function(
pass,
fail,
msg,
error
) {
fail("for array", {});
fail("for boolean", true);
fail("for function", function() {});
fail("for null", null);
fail("for number", 42);
fail("for object", {});
fail("for string", "Test");
pass("for undefined", undefined);
msg(
"fail with descriptive message",
"[assert.isUndefined] Expected null (object) to be undefined",
null
);
msg(
"fail with custom message",
"[assert.isUndefined] fails: Expected null (object) to be undefined",
null,
"fails"
);
error(
"for null",
{
code: "ERR_ASSERTION",
operator: "assert.isUndefined"
},
null
);
});
6 changes: 6 additions & 0 deletions lib/expect.test.js
@@ -1,5 +1,6 @@
"use strict";

var commons = require("@sinonjs/commons");
var sinon = require("sinon");
var referee = require("./referee");
var expect = referee.expect;
Expand All @@ -11,6 +12,9 @@ describe("expect", function() {
referee.on("pass", this.okListener);
this.failListener = sinon.spy();
referee.on("failure", this.failListener);
sinon.replace(commons.deprecated, "wrap", function(f) {
return f;
});
});

afterEach(function() {
Expand Down Expand Up @@ -96,6 +100,8 @@ describe("expect", function() {
expect({}).not.toBeDate();
expect(null).toBeDefined();
expect(undefined).not.toBeDefined();
expect(undefined).toBeUndefined();
expect(null).not.toBeUndefined();
expect(null).toBeNull();
expect(42).not.toBeNull();
expect(obj).toMatch({ id: 42 });
Expand Down
1 change: 1 addition & 0 deletions lib/referee.js
Expand Up @@ -59,6 +59,7 @@ require("./assertions/is-symbol")(referee);
require("./assertions/is-syntax-error")(referee);
require("./assertions/is-true")(referee);
require("./assertions/is-type-error")(referee);
require("./assertions/is-undefined")(referee);
require("./assertions/is-uri-error")(referee);
require("./assertions/is-u-int-16-array")(referee);
require("./assertions/is-u-int-32-array")(referee);
Expand Down
4 changes: 4 additions & 0 deletions lib/test-helper/index.js
@@ -1,5 +1,6 @@
"use strict";

var commons = require("@sinonjs/commons");
var referee = require("../referee");
var sinon = require("sinon");
var slice = require("@sinonjs/commons").prototypes.array.slice;
Expand All @@ -12,6 +13,9 @@ var testHelper = {
referee.on("pass", testHelper.okListener);
testHelper.failListener = sinon.spy();
referee.on("failure", testHelper.failListener);
sinon.replace(commons.deprecated, "wrap", function(f) {
return f;
});
},

tearDown: function() {
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -25,7 +25,7 @@
"*.js": "eslint"
},
"dependencies": {
"@sinonjs/commons": "^1.3.0",
"@sinonjs/commons": "^1.4.0",
"@sinonjs/formatio": "^3.1.0",
"@sinonjs/samsam": "^3.0.0",
"array-from": "2.1.1",
Expand Down

0 comments on commit c8f8951

Please sign in to comment.