Skip to content

Commit b775f1b

Browse files
authored
Avoid tampering with globals and other modules' exports in tests (#2504)
* 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
1 parent 477064b commit b775f1b

File tree

5 files changed

+60
-15
lines changed

5 files changed

+60
-15
lines changed

lib/sinon/spy-formatters.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ var slice = arrayProto.slice;
1414

1515
function colorSinonMatchText(matcher, calledArg, calledArgMessage) {
1616
var calledArgumentMessage = calledArgMessage;
17+
var matcherMessage = matcher.message;
1718
if (!matcher.test(calledArg)) {
18-
matcher.message = color.red(matcher.message);
19+
matcherMessage = color.red(matcher.message);
1920
if (calledArgumentMessage) {
2021
calledArgumentMessage = color.green(calledArgumentMessage);
2122
}
2223
}
23-
return `${calledArgumentMessage} ${matcher.message}`;
24+
return `${calledArgumentMessage} ${matcherMessage}`;
2425
}
2526

2627
function colorDiffText(diff) {

package-lock.json

+27-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"dependencies": {
7676
"@sinonjs/commons": "^3.0.0",
7777
"@sinonjs/fake-timers": "^10.0.2",
78-
"@sinonjs/samsam": "^7.0.1",
78+
"@sinonjs/samsam": "^8.0.0",
7979
"diff": "^5.1.0",
8080
"nise": "^5.1.4",
8181
"supports-color": "^7.2.0"

test/spy-formatters-test.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use strict";
2+
const { D } = require("./../lib/sinon/spy-formatters");
3+
const sinon = require("../lib/sinon");
4+
const { assert } = require("@sinonjs/referee");
5+
6+
describe('formatter specifier "D"', function () {
7+
it("should not mutate matchers passed as arguments", function () {
8+
const matcher = sinon.match(function test() {
9+
return false;
10+
}, "something");
11+
assert.equals(matcher.message, "something");
12+
13+
const stub = sinon.stub();
14+
15+
stub(1, 2, 3);
16+
/* eslint-disable new-cap */
17+
D(stub, [matcher]);
18+
19+
assert.equals(matcher.message, "something");
20+
});
21+
});

test/stub-test.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ describe("stub", function () {
173173
test: func,
174174
};
175175
func.aProp = 42;
176+
176177
createStub(object, "test");
177178

178179
assert.equals(object.test.aProp, 42);
@@ -1535,16 +1536,16 @@ describe("stub", function () {
15351536
});
15361537

15371538
it("stubs methods of function", function () {
1538-
var func = function () {
1539-
return;
1540-
};
1539+
class FunctionType extends Function {
1540+
func2() {
1541+
return 42;
1542+
}
1543+
}
1544+
1545+
var func = new FunctionType();
15411546
func.func1 = function () {
15421547
return;
15431548
};
1544-
// eslint-disable-next-line no-proto
1545-
func.__proto__.func2 = function () {
1546-
return;
1547-
};
15481549

15491550
createStub(func);
15501551

0 commit comments

Comments
 (0)