diff --git a/packages/jest-console/package.json b/packages/jest-console/package.json index 9bde59e6b94b8c..2a887b93e92a8b 100644 --- a/packages/jest-console/package.json +++ b/packages/jest-console/package.json @@ -27,11 +27,11 @@ "files": [ "build", "build-module", - "types" + "build-types" ], "main": "build/index.js", "module": "build-module/index.js", - "types": "types", + "types": "build-types", "dependencies": { "@babel/runtime": "7.25.7", "jest-matcher-utils": "^29.6.2" diff --git a/packages/jest-console/types/index.d.ts b/packages/jest-console/src/declarations.d.ts similarity index 84% rename from packages/jest-console/types/index.d.ts rename to packages/jest-console/src/declarations.d.ts index 817fd407c4e9d2..ee4f48fd27edf5 100644 --- a/packages/jest-console/types/index.d.ts +++ b/packages/jest-console/src/declarations.d.ts @@ -11,7 +11,7 @@ declare namespace jest { /** * Ensure that `console.error` function was called with specific arguments. */ - toHaveErroredWith( ...args: any[] ): R; + toHaveErroredWith( ...args: unknown[] ): R; /** * Ensure that `console.info` function was called. @@ -21,7 +21,7 @@ declare namespace jest { /** * Ensure that `console.info` function was called with specific arguments. */ - toHaveInformedWith( ...args: any[] ): R; + toHaveInformedWith( ...args: unknown[] ): R; /** * Ensure that `console.log` function was called. @@ -31,7 +31,7 @@ declare namespace jest { /** * Ensure that `console.log` function was called with specific arguments. */ - toHaveLoggedWith( ...args: any[] ): R; + toHaveLoggedWith( ...args: unknown[] ): R; /** * Ensure that `console.warn` function was called. @@ -41,6 +41,6 @@ declare namespace jest { /** * Ensure that `console.warn` function was called with specific arguments. */ - toHaveWarnedWith( ...args: any[] ): R; + toHaveWarnedWith( ...args: unknown[] ): R; } } diff --git a/packages/jest-console/src/index.js b/packages/jest-console/src/index.ts similarity index 73% rename from packages/jest-console/src/index.js rename to packages/jest-console/src/index.ts index 84cdb6f1d73ad2..09cd7bccc78f37 100644 --- a/packages/jest-console/src/index.js +++ b/packages/jest-console/src/index.ts @@ -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. diff --git a/packages/jest-console/src/matchers.js b/packages/jest-console/src/matchers.ts similarity index 65% rename from packages/jest-console/src/matchers.js rename to packages/jest-console/src/matchers.ts index 9793164f532b32..de25c186128a3b 100644 --- a/packages/jest-console/src/matchers.js +++ b/packages/jest-console/src/matchers.ts @@ -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 @@ -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 @@ -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`; diff --git a/packages/jest-console/src/supported-matchers.js b/packages/jest-console/src/supported-matchers.ts similarity index 71% rename from packages/jest-console/src/supported-matchers.js rename to packages/jest-console/src/supported-matchers.ts index 636ad897e00efe..6bff52eb04e49b 100644 --- a/packages/jest-console/src/supported-matchers.js +++ b/packages/jest-console/src/supported-matchers.ts @@ -1,4 +1,4 @@ -const supportedMatchers = { +const supportedMatchers: Record< string, string > = { error: 'toHaveErrored', info: 'toHaveInformed', log: 'toHaveLogged', diff --git a/packages/jest-console/src/test/index.test.js b/packages/jest-console/src/test/index.test.ts similarity index 100% rename from packages/jest-console/src/test/index.test.js rename to packages/jest-console/src/test/index.test.ts diff --git a/packages/jest-console/src/types.ts b/packages/jest-console/src/types.ts new file mode 100644 index 00000000000000..77de3d28e8ca60 --- /dev/null +++ b/packages/jest-console/src/types.ts @@ -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; diff --git a/packages/jest-console/tsconfig.json b/packages/jest-console/tsconfig.json new file mode 100644 index 00000000000000..f58ead725263bf --- /dev/null +++ b/packages/jest-console/tsconfig.json @@ -0,0 +1,8 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig.json", + "extends": "../../tsconfig.base.json", + "files": [ "src/declarations.d.ts" ], + "compilerOptions": { + "types": [ "jest", "@testing-library/jest-dom" ] + } +} diff --git a/tsconfig.json b/tsconfig.json index d6bbcb27f0adb6..458315ef44a289 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -62,6 +62,7 @@ { "path": "packages/warning" }, { "path": "packages/wordcount" }, { "path": "test/e2e" }, + { "path": "packages/jest-console" }, { "path": "test/performance" } ], "files": []