Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
*/
import './matchers';
import supportedMatchers from './supported-matchers';
import type { ExtendedMock } from './types';

/**
* Sets spy on the console object's method to make it possible to fail test when method called without assertion.
*
* @param {Array} args
* @param {string} args."0" Name of console method.
* @param {string} args."1" Name of Jest matcher.
* @param args
*/
const setConsoleMethodSpy = ( [ methodName, matcherName ] ) => {
const setConsoleMethodSpy = ( args: [ string, string ] ) => {
const [ methodName, matcherName ] = args;
const spy = jest
.spyOn( console, methodName )
.mockName( `console.${ methodName }` );
.spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' )
.mockName( `console.${ methodName }` ) as ExtendedMock;

/**
* Resets the spy to its initial state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@ import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
* Internal dependencies
*/
import supportedMatchers from './supported-matchers';
import type { Mock } from 'jest-mock';
import type {
ExtendedMock,
MatcherFunction,
MatcherResult,
MatcherWithArgsFunction,
} from './types';

const createErrorMessage = ( spyInfo ) => {
interface SpyInfo {
spy: Mock;
pass: boolean;
calls: unknown[][];
matcherName: string;
methodName: string;
expected?: unknown[];
}

const createErrorMessage = ( spyInfo: SpyInfo ) => {
const { spy, pass, calls, matcherName, methodName, expected } = spyInfo;
const hint = pass ? `.not${ matcherName }` : matcherName;
const message = pass
Expand All @@ -28,7 +44,12 @@ const createErrorMessage = ( spyInfo ) => {
'See https://www.npmjs.com/package/@wordpress/jest-console for details.';
};

const createSpyInfo = ( spy, matcherName, methodName, expected ) => {
const createSpyInfo = (
spy: Mock,
matcherName: string,
methodName: string,
expected?: unknown[]
) => {
const calls = spy.mock.calls;

const pass = expected
Expand All @@ -51,27 +72,35 @@ const createSpyInfo = ( spy, matcherName, methodName, expected ) => {
};

const createToHaveBeenCalledMatcher =
( matcherName, methodName ) => ( received ) => {
const spy = received[ methodName ];
( matcherName: string, methodName: string ) =>
( received: Record< string, Mock > ): MatcherResult => {
const spy = received[ methodName ] as ExtendedMock;
const spyInfo = createSpyInfo( spy, matcherName, methodName );

spy.assertionsNumber += 1;

return spyInfo;
};

const createToHaveBeenCalledWith = ( matcherName, methodName ) =>
function ( received, ...expected ) {
const spy = received[ methodName ];
const createToHaveBeenCalledWith = (
matcherName: string,
methodName: string
) =>
function (
received: Record< string, Mock >,
...expected: unknown[]
): MatcherResult {
const spy = received[ methodName ] as ExtendedMock;
const spyInfo = createSpyInfo( spy, matcherName, methodName, expected );

spy.assertionsNumber += 1;

return spyInfo;
};

type MatchersObject = Record<
string,
MatcherFunction | MatcherWithArgsFunction
>;

expect.extend(
Object.entries( supportedMatchers ).reduce(
Object.entries( supportedMatchers ).reduce< MatchersObject >(
( result, [ methodName, matcherName ] ) => {
const matcherNameWith = `${ matcherName }With`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const supportedMatchers = {
const supportedMatchers: Record< string, string > = {
error: 'toHaveErrored',
info: 'toHaveInformed',
log: 'toHaveLogged',
Expand Down
34 changes: 34 additions & 0 deletions packages/jest-console/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* External dependencies
*/
import type { Mock } from 'jest-mock';

/**
* Interface for the extended Jest Mock that includes the assertionsNumber property
*/
export interface ExtendedMock extends Mock {
assertionsNumber: number;
}

/**
* Interface for the matcher result object
*/
export interface MatcherResult {
pass: boolean;
message: () => string;
}

/**
* Interface for the matcher function
*/
export type MatcherFunction = (
received: Record< string, Mock >
) => MatcherResult;

/**
* Interface for the matcher function with arguments
*/
export type MatcherWithArgsFunction = (
received: Record< string, Mock >,
...expected: unknown[]
) => MatcherResult;
7 changes: 7 additions & 0 deletions packages/jest-console/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"$schema": "https://json.schemastore.org/tsconfig.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"types": [ "jest", "@testing-library/jest-dom" ]
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
{ "path": "packages/warning" },
{ "path": "packages/wordcount" },
{ "path": "test/e2e" },
{ "path": "packages/jest-console" },
{ "path": "test/performance" }
],
"files": []
Expand Down
Loading