Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -7,28 +7,30 @@ import supportedMatchers from './supported-matchers';
/**
* 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 )
.spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' )
.mockName( `console.${ methodName }` );

/**
* Resets the spy to its initial state.
*/
function resetSpy() {
spy.mockReset();
spy.assertionsNumber = 0;
( spy as any ).assertionsNumber = 0;
}

/**
* Verifies that the spy has only been called if expected.
*/
function assertExpectedCalls() {
if ( spy.assertionsNumber === 0 && spy.mock.calls.length > 0 ) {
if (
( spy as any ).assertionsNumber === 0 &&
spy.mock.calls.length > 0
) {
expect( console ).not[ matcherName ]();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
* Internal dependencies
*/
import supportedMatchers from './supported-matchers';
import type { Mock } from 'jest-mock';

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
calls: any[][];
matcherName: string;
methodName: string;
expected?: any[];
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 +38,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?: any[]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expected?: any[]
expected?: unknown[]

) => {
const calls = spy.mock.calls;

const pass = expected
Expand All @@ -51,22 +66,21 @@ const createSpyInfo = ( spy, matcherName, methodName, expected ) => {
};

const createToHaveBeenCalledMatcher =
( matcherName, methodName ) => ( received ) => {
const spy = received[ methodName ];
( matcherName: string, methodName: string ) => ( received: any ) => {
const spy = received[ methodName ] as Mock;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can get rid of any type and the assertion

Suggested change
( matcherName: string, methodName: string ) => ( received: any ) => {
const spy = received[ methodName ] as Mock;
( matcherName: string, methodName: string ) =>
( received: Record< string, Mock > ) => {
const spy = received[ methodName ];

const spyInfo = createSpyInfo( spy, matcherName, methodName );

spy.assertionsNumber += 1;

( spy as any ).assertionsNumber += 1;
return spyInfo;
};

const createToHaveBeenCalledWith = ( matcherName, methodName ) =>
function ( received, ...expected ) {
const spy = received[ methodName ];
const createToHaveBeenCalledWith = (
matcherName: string,
methodName: string
) =>
function ( received: any, ...expected: any[] ) {
const spy = received[ methodName ] as Mock;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function ( received: any, ...expected: any[] ) {
const spy = received[ methodName ] as Mock;
function ( received: Record< string, Mock >, ...expected: unknown[] ) {
const spy = received[ methodName ];

const spyInfo = createSpyInfo( spy, matcherName, methodName, expected );

spy.assertionsNumber += 1;

( spy as any ).assertionsNumber += 1;
return spyInfo;
};

Expand All @@ -87,6 +101,6 @@ expect.extend(
),
};
},
{}
{} as Record< string, any >

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this assertion here, I would pass this as the first argument to th .reduce generic

Record<
	string,
	( received: Record< string, Mock > ) => {
		pass: boolean;
		message: () => string;
	}
>

So, the .reduce call would look like this

Object.entries( supportedMatchers ).reduce<
	Record<
		string,
		( received: Record< string, Mock > ) => {
			pass: boolean;
			message: () => string;
		}
	>
>(/* Arguments here */);

)
);
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
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