Skip to content

Commit

Permalink
fix: do not mutate matcher object message
Browse files Browse the repository at this point in the history
This made 3 of the assertion tests fail when re-running
as Sinon's spy formatter changed the exports of the
samsam module
  • Loading branch information
fatso83 committed Mar 26, 2023
1 parent 3ca1d9b commit 1c8a531
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/sinon/spy-formatters.js
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
22 changes: 22 additions & 0 deletions test/spy-formatters-test.js
@@ -0,0 +1,22 @@
'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')
});
});

0 comments on commit 1c8a531

Please sign in to comment.