Skip to content

Commit

Permalink
Use deepEqual to compare resolved promise value
Browse files Browse the repository at this point in the history
  • Loading branch information
mantoni committed Nov 6, 2020
1 parent f027bf6 commit b001fed
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/assertions/resolves.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 catchCallback() {
this.reject("${0} did not resolve, it rejected instead");
}
referee.add("resolves", {
assert: createAsyncAssertion(function(actual, expected) {
if (!samsam.identical(actual, expected)) {
if (!samsam.deepEqual(actual, expected)) {
this.reject(assertMessage);
return;
}
this.resolve();
}, catchCallback),
refute: createAsyncAssertion(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/resolves.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("./resolves");

Expand All @@ -14,8 +15,15 @@ describe("resolves 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 'resolves' as name", function() {
assert(this.fakeReferee.add.calledWith("resolves"));
});
Expand All @@ -36,16 +44,55 @@ describe("resolves factory", function() {
it("should resolve the returned promise", function() {
return this.options.assert(Promise.resolve("test"), "test");
});

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

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

context(
"when promise argument does not resolves to value argument",
function() {
it("should reject the returned promise", function() {
return this.options
var options = this.options;
return options
.assert(Promise.resolve("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.resolve({ 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.resolve({ 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 @@ -55,6 +102,7 @@ describe("resolves 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 @@ -65,6 +113,10 @@ describe("resolves factory", function() {
.assert(Promise.reject(), "test")
.catch(function(e) {
assert(e instanceof Error);
assert.equal(
e.message,
"${0} did not resolve, it rejected instead"
);
});
});
});
Expand All @@ -88,15 +140,54 @@ describe("resolves factory", function() {
"test2"
);
});

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

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

context("when promise argument resolves to value argument", function() {
it("rejects the returned promise", function() {
return this.options
var options = this.options;
return options
.refute(Promise.resolve("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.resolve({ 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.resolve({ 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 @@ -105,6 +196,7 @@ describe("resolves 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 @@ -115,25 +207,29 @@ describe("resolves factory", function() {
.refute(Promise.reject(), "test")
.catch(function(e) {
assert(e instanceof Error);
assert.equal(
e.message,
"${0} did not resolve, it rejected 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 b001fed

Please sign in to comment.