Skip to content

Commit

Permalink
Use deepEqual to compare rejected promise value
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Nov 6, 2020
1 parent b001fed commit 4b21019
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/assertions/rejects.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
var samsam = require("@sinonjs/samsam");
var createAsyncAssertion = require("../create-async-assertion");

var assertMessage = "${actual} is not identical to ${expected}";
var refuteMessage = "${actual} is identical to ${expected}";
var assertMessage = "${actual} is not equal to ${expected}";
var refuteMessage = "${actual} is equal to ${expected}";

module.exports = function(referee) {
function thenCallback() {
this.reject("${0} did not reject, it resolved instead");
}
referee.add("rejects", {
assert: createAsyncAssertion(thenCallback, function(actual, expected) {
if (!samsam.identical(actual, expected)) {
if (!samsam.deepEqual(actual, expected)) {
this.reject(assertMessage);
return;
}
this.resolve();
}),
refute: createAsyncAssertion(thenCallback, function(actual, expected) {
if (samsam.identical(actual, expected)) {
if (samsam.deepEqual(actual, expected)) {
this.reject(refuteMessage);
return;
}
Expand Down
108 changes: 102 additions & 6 deletions lib/assertions/rejects.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var assert = require("assert");
var sinon = require("sinon");
var samsam = require("@sinonjs/samsam");

var factory = require("./rejects");

Expand All @@ -14,8 +15,15 @@ describe("rejects factory", function() {
factory(this.fakeReferee);

this.options = this.fakeReferee.add.args[0][1];
this.options.fail = function(message) {
throw new Error(message);
};
});

function unexpectedThen() {
throw new Error("Unexpected then");
}

it("calls referee.add with 'rejects' as name", function() {
assert(this.fakeReferee.add.calledWith("rejects"));
});
Expand All @@ -33,16 +41,55 @@ describe("rejects factory", function() {
it("should resolve the returned promise", function() {
return this.options.assert(Promise.reject("test"), "test");
});

it("should pass for equal object", function() {
return this.options.assert(Promise.reject({ foo: 1 }), {
foo: 1
});
});

it("should pass for matching matcher", function() {
return this.options.assert(
Promise.reject({ foo: 1 }),
samsam.match.object
);
});
});

context(
"when promise argument does not reject to value argument",
function() {
it("should reject the returned promise", function() {
return this.options
var options = this.options;
return options
.assert(Promise.reject("test"), "test2")
.then(unexpectedThen)
.catch(function(e) {
assert(e instanceof Error);
assert.equal(e.message, options.assertMessage);
});
});

it("should fail for different object", function() {
var options = this.options;
return options
.assert(Promise.reject({ foo: 1 }), { foo: 2 })
.then(unexpectedThen)
.catch(function(e) {
assert(e instanceof Error);
assert.equal(e.message, options.assertMessage);
});
});

it("should fail for non-matching matcher", function() {
var promise = Promise.reject({ foo: 1 });
var options = this.options;
return options
.assert(promise, samsam.match.array)
.then(unexpectedThen)
.catch(function(e) {
assert(e instanceof Error);
assert.equal(e.message, options.assertMessage);
});
});
}
Expand All @@ -52,6 +99,7 @@ describe("rejects factory", function() {
it("should reject the returned promise", function() {
return this.options.assert({}, "test").catch(function(e) {
assert(e instanceof Error);
assert.equal(e.message, "promise.then is not a function");
});
});
});
Expand All @@ -62,6 +110,10 @@ describe("rejects factory", function() {
.assert(Promise.resolve(), "test")
.catch(function(e) {
assert(e instanceof Error);
assert.equal(
e.message,
"${0} did not reject, it resolved instead"
);
});
});
});
Expand All @@ -82,15 +134,54 @@ describe("rejects factory", function() {
it("should resolve the returned promise", function() {
return this.options.refute(Promise.reject("test"), "test2");
});

it("should pass for different object", function() {
return this.options.refute(Promise.reject({ foo: 1 }), {
foo: 2
});
});

it("should pass for non-matching matcher", function() {
return this.options.refute(
Promise.reject({ foo: 1 }),
samsam.match.array
);
});
}
);

context("when promise argument rejects to value argument", function() {
it("should reject the returned promise", function() {
return this.options
var options = this.options;
return options
.refute(Promise.reject("test"), "test")
.then(unexpectedThen)
.catch(function(e) {
assert(e instanceof Error);
assert.equal(e.message, options.refuteMessage);
});
});

it("should fail for equal object", function() {
var options = this.options;
return options
.refute(Promise.reject({ foo: 1 }), { foo: 1 })
.then(unexpectedThen)
.catch(function(e) {
assert(e instanceof Error);
assert.equal(e.message, options.refuteMessage);
});
});

it("should fail for matching matcher", function() {
var promise = Promise.reject({ foo: 1 });
var options = this.options;
return options
.refute(promise, samsam.match.object)
.then(unexpectedThen)
.catch(function(e) {
assert(e instanceof Error);
assert.equal(e.message, options.refuteMessage);
});
});
});
Expand All @@ -99,6 +190,7 @@ describe("rejects factory", function() {
it("should reject the returned promise", function() {
return this.options.refute({}, "test").catch(function(e) {
assert(e instanceof Error);
assert.equal(e.message, "promise.then is not a function");
});
});
});
Expand All @@ -109,25 +201,29 @@ describe("rejects factory", function() {
.refute(Promise.resolve(), "test")
.catch(function(e) {
assert(e instanceof Error);
assert.equal(
e.message,
"${0} did not reject, it resolved instead"
);
});
});
});
});

describe(".assertMessage", function() {
it("is '${actual} is not identical to ${expected}'", function() {
it("is '${actual} is not equal to ${expected}'", function() {
assert.equal(
this.options.assertMessage,
"${actual} is not identical to ${expected}"
"${actual} is not equal to ${expected}"
);
});
});

describe(".refuteMessage", function() {
it("is '${actual} is identical to ${expected}'", function() {
it("is '${actual} is equal to ${expected}'", function() {
assert.equal(
this.options.refuteMessage,
"${actual} is identical to ${expected}"
"${actual} is equal to ${expected}"
);
});
});
Expand Down

0 comments on commit 4b21019

Please sign in to comment.