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

Improving error messages and codes in jest-console, matcher.js #53743

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 46 additions & 38 deletions packages/jest-console/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,66 @@ import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
*/
import supportedMatchers from './supported-matchers';

const createErrorMessage = ( spyInfo ) => {
const { spy, pass, calls, matcherName, methodName, expected } = spyInfo;
const hint = pass ? `.not${ matcherName }` : matcherName;
const message = pass
? `Expected mock function not to be called but it was called with:\n${ calls.map(
printReceived
) }`
: `Expected mock function to be called${
expected ? ` with:\n${ printExpected( expected ) }\n` : '.'
}\nbut it was called with:\n${ calls.map( printReceived ) }`;

return () =>
`${ matcherHint( hint, spy.getMockName() ) }` +
'\n\n' +
message +
'\n\n' +
`console.${ methodName }() should not be used unless explicitly expected\n` +
MericKarabulut marked this conversation as resolved.
Show resolved Hide resolved
'See https://www.npmjs.com/package/@wordpress/jest-console for details.';
};

const createSpyInfo = ( spy, matcherName, methodName, expected ) => {
const calls = spy.mock.calls;

const pass = expected
? JSON.stringify( calls ).includes( JSON.stringify( expected ) )
: calls.length > 0;

const message = createErrorMessage( {
spy,
pass,
calls,
matcherName,
methodName,
expected,
} );

return {
pass,
message,
};
};

const createToHaveBeenCalledMatcher =
( matcherName, methodName ) => ( received ) => {
const spy = received[ methodName ];
const calls = spy.mock.calls;
const pass = calls.length > 0;
const message = pass
? () =>
matcherHint( `.not${ matcherName }`, spy.getMockName() ) +
'\n\n' +
'Expected mock function not to be called but it was called with:\n' +
calls.map( printReceived )
: () =>
matcherHint( matcherName, spy.getMockName() ) +
'\n\n' +
'Expected mock function to be called.';
const spyInfo = createSpyInfo( spy, matcherName, methodName );

spy.assertionsNumber += 1;

return {
message,
pass,
};
return spyInfo;
};

const createToHaveBeenCalledWith = ( matcherName, methodName ) =>
function ( received, ...expected ) {
const spy = received[ methodName ];
const calls = spy.mock.calls;
const pass = calls.some( ( objects ) =>
this.equals( objects, expected )
);
const message = pass
? () =>
matcherHint( `.not${ matcherName }`, spy.getMockName() ) +
'\n\n' +
'Expected mock function not to be called with:\n' +
printExpected( expected )
: () =>
matcherHint( matcherName, spy.getMockName() ) +
'\n\n' +
'Expected mock function to be called with:\n' +
printExpected( expected ) +
'\n' +
'but it was called with:\n' +
calls.map( printReceived );
const spyInfo = createSpyInfo( spy, matcherName, methodName, expected );

spy.assertionsNumber += 1;

return {
message,
pass,
};
return spyInfo;
};

expect.extend(
Expand Down
Loading