Skip to content

Commit

Permalink
Avoid tampering with globals and other modules' exports in tests (#2504)
Browse files Browse the repository at this point in the history
* fix: do not modify the Function prototype when running tests

Made a single test fail as the Function prototype had been mutated by a later test in the test suite.

refs #2502

* fix: do not mutate matcher object message

This made 3 of the assertion tests fail when re-running as Sinon's spy formatter changed the exports of the samsam module

* Use latest version of samsam to get immutable messages

Ensure we cannot do the same mistake again
  • Loading branch information
fatso83 authored Mar 26, 2023
1 parent 477064b commit b775f1b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lib/sinon/spy-formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ var slice = arrayProto.slice;

function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
var calledArgumentMessage = calledArgMessage;
var matcherMessage = matcher.message;
if (!matcher.test(calledArg)) {
matcher.message = color.red(matcher.message);
matcherMessage = color.red(matcher.message);
if (calledArgumentMessage) {
calledArgumentMessage = color.green(calledArgumentMessage);
}
}
return `${calledArgumentMessage} ${matcher.message}`;
return `${calledArgumentMessage} ${matcherMessage}`;
}

function colorDiffText(diff) {
Expand Down
32 changes: 27 additions & 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"dependencies": {
"@sinonjs/commons": "^3.0.0",
"@sinonjs/fake-timers": "^10.0.2",
"@sinonjs/samsam": "^7.0.1",
"@sinonjs/samsam": "^8.0.0",
"diff": "^5.1.0",
"nise": "^5.1.4",
"supports-color": "^7.2.0"
Expand Down
21 changes: 21 additions & 0 deletions test/spy-formatters-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";
const { D } = require("./../lib/sinon/spy-formatters");
const sinon = require("../lib/sinon");
const { assert } = require("@sinonjs/referee");

describe('formatter specifier "D"', function () {
it("should not mutate matchers passed as arguments", function () {
const matcher = sinon.match(function test() {
return false;
}, "something");
assert.equals(matcher.message, "something");

const stub = sinon.stub();

stub(1, 2, 3);
/* eslint-disable new-cap */
D(stub, [matcher]);

assert.equals(matcher.message, "something");
});
});
15 changes: 8 additions & 7 deletions test/stub-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ describe("stub", function () {
test: func,
};
func.aProp = 42;

createStub(object, "test");

assert.equals(object.test.aProp, 42);
Expand Down Expand Up @@ -1535,16 +1536,16 @@ describe("stub", function () {
});

it("stubs methods of function", function () {
var func = function () {
return;
};
class FunctionType extends Function {
func2() {
return 42;
}
}

var func = new FunctionType();
func.func1 = function () {
return;
};
// eslint-disable-next-line no-proto
func.__proto__.func2 = function () {
return;
};

createStub(func);

Expand Down

0 comments on commit b775f1b

Please sign in to comment.