Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sandboxes each use their own assert object #2302

Merged
merged 2 commits into from Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/sinon/assert.js
Expand Up @@ -15,6 +15,7 @@ var forEach = arrayProto.forEach;
var join = arrayProto.join;
var splice = arrayProto.splice;

function createAssertObject() {
var assert;

function verifyIsStub() {
Expand Down Expand Up @@ -97,7 +98,9 @@ function mirrorPropAsAssertion(name, method, message) {
}

function exposedName(prefix, prop) {
return !prefix || /^fail/.test(prop) ? prop : prefix + stringSlice(prop, 0, 1).toUpperCase() + stringSlice(prop, 1);
return !prefix || /^fail/.test(prop)
? prop
: prefix + stringSlice(prop, 0, 1).toUpperCase() + stringSlice(prop, 1);
}

assert = {
Expand Down Expand Up @@ -214,4 +217,8 @@ mirrorPropAsAssertion("neverCalledWithMatch", "expected %n to never be called wi
mirrorPropAsAssertion("threw", "%n did not throw exception%C");
mirrorPropAsAssertion("alwaysThrew", "%n did not always throw exception%C");

module.exports = assert;
return assert;
}

module.exports = createAssertObject();
module.exports.createAssertObject = createAssertObject;
3 changes: 2 additions & 1 deletion lib/sinon/sandbox.js
Expand Up @@ -37,6 +37,8 @@ function Sandbox() {
var fakeRestorers = [];
var promiseLib;

sandbox.assert = sinonAssert.createAssertObject();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


sandbox.serverPrototype = fakeServer;

// this is for testing only
Expand Down Expand Up @@ -410,7 +412,6 @@ function Sandbox() {
};
}

Sandbox.prototype.assert = sinonAssert;
Sandbox.prototype.match = match;

module.exports = Sandbox;
34 changes: 27 additions & 7 deletions test/sandbox-test.js
Expand Up @@ -15,7 +15,6 @@ var sinonFake = require("../lib/sinon/fake");
var sinonSpy = require("../lib/sinon/spy");
var sinonStub = require("../lib/sinon/stub");
var sinonConfig = require("../lib/sinon/util/core/get-config");
var sinonAssert = require("../lib/sinon/assert");
var sinonClock = require("../lib/sinon/util/fake-timers");

var supportsAjax = typeof XMLHttpRequest !== "undefined" || typeof ActiveXObject !== "undefined";
Expand Down Expand Up @@ -47,12 +46,6 @@ describe("Sandbox", function() {
assert.same(sandbox.match, match);
});

it("exposes assert", function() {
var sandbox = new Sandbox();

assert.same(sandbox.assert, sinonAssert);
});

it("can be reset without failing when pre-configured to use a fake server", function() {
var sandbox = createSandbox({ useFakeServer: true });
refute.exception(function() {
Expand Down Expand Up @@ -2077,4 +2070,31 @@ describe("Sandbox", function() {
assert.equals(object.prop, "bla");
});
});

describe(".assert", function() {
it("allows rebinding of .fail on a per-sandbox level", function() {
var sandboxA = createSandbox();
var sandboxB = createSandbox();

sandboxA.assert.failException = "CustomErrorA";
sandboxB.assert.failException = "CustomErrorB";

assert.exception(
function() {
sandboxA.assert.fail("Some message");
},
{ name: "CustomErrorA" }
);

assert.exception(
function() {
sandboxB.assert.fail("Some message");
},
{ name: "CustomErrorB" }
);

sandboxA.restore();
fatso83 marked this conversation as resolved.
Show resolved Hide resolved
sandboxB.restore();
fatso83 marked this conversation as resolved.
Show resolved Hide resolved
});
});
});