Modified error message

This commit is contained in:
Eradev
2025-01-18 17:55:00 -05:00
parent 289afbf0a6
commit 888d3b6250
2 changed files with 48 additions and 22 deletions

View File

@@ -26,22 +26,32 @@ describe('toHaveNoOtherSpyInteractions', function() {
});
it('fails when there are spy interactions', function() {
let matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions();
const matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
});
let matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(
matchersUtil
);
let spyObj = jasmineUnderTest
.getEnv()
.createSpyObj('NewClass', ['spyA', 'spyB']);
spyObj.spyA();
spyObj.spyA('x');
let result = matcher.compare(spyObj);
expect(result.pass).toBeFalse();
expect(result.message).toContain(
"Unverified spies' calls have been found in:"
'Expected to have no other spy interactions, but it had the following calls:'
);
});
it('shows the right message is negated', function() {
let matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions();
const matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
});
let matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(
matchersUtil
);
let spyObj = jasmineUnderTest
.getEnv()
.createSpyObj('NewClass', ['spyA', 'spyB']);
@@ -52,7 +62,7 @@ describe('toHaveNoOtherSpyInteractions', function() {
let result = matcher.compare(spyObj);
expect(result.pass).toBeFalse(),
expect(result.message).toContain(
"Unverified spies' calls have been found in:"
'Expected to have no other spy interactions, but it had the following calls:'
);
});
@@ -67,7 +77,9 @@ describe('toHaveNoOtherSpyInteractions', function() {
let result = matcher.compare(spyObj);
expect(result.pass).toBeTrue();
expect(result.message).toContain("Spies' calls are all verified.");
expect(result.message).toContain(
"Expected to have other spy interactions but it didn't"
);
});
it(`throws an error if a non-object is passed`, function() {
@@ -117,7 +129,9 @@ describe('toHaveNoOtherSpyInteractions', function() {
const matchersUtil = new jasmineUnderTest.MatchersUtil({
pp: jasmineUnderTest.makePrettyPrinter()
}),
matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(),
matcher = jasmineUnderTest.matchers.toHaveNoOtherSpyInteractions(
matchersUtil
),
toHaveBeenCalledWithMatcher = jasmineUnderTest.matchers.toHaveBeenCalledWith(
matchersUtil
),

View File

@@ -29,7 +29,7 @@ getJasmineRequireObj().toHaveNoOtherSpyInteractions = function(j$) {
result.pass = true;
let hasSpy = false;
const unexpectedCallsIn = [];
const unexpectedCalls = [];
for (const spy of Object.values(actual)) {
if (!j$.isSpy(spy)) {
@@ -38,14 +38,20 @@ getJasmineRequireObj().toHaveNoOtherSpyInteractions = function(j$) {
hasSpy = true;
if (!spy.calls.all().every(call => call.verified)) {
unexpectedCallsIn.push([
spy.and.identity,
spy.calls.unverifiedCount()
]);
const unverifiedCalls = spy.calls
.all()
.filter(call => !call.verified);
if (unverifiedCalls.length > 0) {
result.pass = false;
}
unverifiedCalls.forEach(unverifiedCall => {
unexpectedCalls.push([
spy.and.identity,
matchersUtil.pp(unverifiedCall.args)
]);
});
}
if (!hasSpy) {
@@ -56,15 +62,21 @@ getJasmineRequireObj().toHaveNoOtherSpyInteractions = function(j$) {
);
}
result.message = result.pass
? "Spies' calls are all verified."
: "Unverified spies' calls have been found in: " +
unexpectedCallsIn
.map(
([spyName, unverifiedCount]) =>
`${spyName} (${unverifiedCount} unverified call(s))`
)
.join(', ');
if (result.pass) {
result.message =
"Expected to have other spy interactions but it didn't.";
} else {
const ppUnexpectedCalls = unexpectedCalls
.map(
([spyName, arguments]) => ` ${spyName} called with ${arguments}`
)
.join(',\n');
result.message =
'Expected to have no other spy interactions, but it had the following calls:\n' +
ppUnexpectedCalls +
'.\n\n';
}
return result;
}