From 5801b933d5e5f5fe8a97938b2901f70f228a7721 Mon Sep 17 00:00:00 2001 From: Kushagra Goyal Date: Fri, 27 Jun 2025 04:13:34 +0530 Subject: [PATCH 1/6] refactor: migrate jest-console package from js to ts for type safety --- .../jest-console/src/{index.js => index.ts} | 22 +++--- .../src/{matchers.js => matchers.ts} | 46 ++++++++----- ...rted-matchers.js => supported-matchers.ts} | 2 +- packages/jest-console/src/test/index.test.ts | 67 +++++++++++++++++++ packages/jest-console/tsconfig.json | 7 ++ tsconfig.json | 1 + 6 files changed, 113 insertions(+), 32 deletions(-) rename packages/jest-console/src/{index.js => index.ts} (58%) rename packages/jest-console/src/{matchers.js => matchers.ts} (71%) rename packages/jest-console/src/{supported-matchers.js => supported-matchers.ts} (71%) create mode 100644 packages/jest-console/src/test/index.test.ts create mode 100644 packages/jest-console/tsconfig.json diff --git a/packages/jest-console/src/index.js b/packages/jest-console/src/index.ts similarity index 58% rename from packages/jest-console/src/index.js rename to packages/jest-console/src/index.ts index 84cdb6f1d73ad2..a63e222548319a 100644 --- a/packages/jest-console/src/index.js +++ b/packages/jest-console/src/index.ts @@ -7,28 +7,24 @@ 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 ](); } } diff --git a/packages/jest-console/src/matchers.js b/packages/jest-console/src/matchers.ts similarity index 71% rename from packages/jest-console/src/matchers.js rename to packages/jest-console/src/matchers.ts index 9793164f532b32..7e849f2bf76f9a 100644 --- a/packages/jest-console/src/matchers.js +++ b/packages/jest-console/src/matchers.ts @@ -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[]; +} + +const createErrorMessage = ( spyInfo: SpyInfo ) => { const { spy, pass, calls, matcherName, methodName, expected } = spyInfo; const hint = pass ? `.not${ matcherName }` : matcherName; const message = pass @@ -28,13 +38,16 @@ 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[] +) => { const calls = spy.mock.calls; - const pass = expected ? JSON.stringify( calls ).includes( JSON.stringify( expected ) ) : calls.length > 0; - const message = createErrorMessage( { spy, pass, @@ -43,7 +56,6 @@ const createSpyInfo = ( spy, matcherName, methodName, expected ) => { methodName, expected, } ); - return { pass, message, @@ -51,22 +63,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; 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; const spyInfo = createSpyInfo( spy, matcherName, methodName, expected ); - - spy.assertionsNumber += 1; - + ( spy as any ).assertionsNumber += 1; return spyInfo; }; @@ -74,7 +85,6 @@ expect.extend( Object.entries( supportedMatchers ).reduce( ( result, [ methodName, matcherName ] ) => { const matcherNameWith = `${ matcherName }With`; - return { ...result, [ matcherName ]: createToHaveBeenCalledMatcher( @@ -87,6 +97,6 @@ expect.extend( ), }; }, - {} + {} as Record< string, any > ) ); 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.ts b/packages/jest-console/src/test/index.test.ts new file mode 100644 index 00000000000000..0f0525e446fc67 --- /dev/null +++ b/packages/jest-console/src/test/index.test.ts @@ -0,0 +1,67 @@ +/* eslint-disable no-console */ + +/** + * Internal dependencies + */ +import '../matchers'; + +describe( 'jest-console', () => { + describe.each( [ + [ 'error', 'toHaveErrored' ], + [ 'info', 'toHaveInformed' ], + [ 'log', 'toHaveLogged' ], + [ 'warn', 'toHaveWarned' ], + ] )( 'console.%s', ( methodName, matcherName ) => { + const matcherNameWith = `${ matcherName }With`; + const message = `This is ${ methodName }!`; + + test( `${ matcherName } works`, () => { + console[ methodName ]( message ); + expect( console )[ matcherName ](); + } ); + + test( `${ matcherName } works when not called`, () => { + expect( console ).not[ matcherName ](); + expect( () => expect( console )[ matcherName ]() ).toThrow( + 'Expected mock function to be called.' + ); + } ); + + test( `${ matcherNameWith } works with arguments that match`, () => { + console[ methodName ]( message ); + expect( console )[ matcherNameWith ]( message ); + } ); + + test( `${ matcherNameWith } works when not called`, () => { + expect( console ).not[ matcherNameWith ]( message ); + expect( () => + expect( console )[ matcherNameWith ]( message ) + ).toThrow( + /Expected mock function to be called with:.*but it was called with:/s + ); + } ); + + test( `${ matcherNameWith } works with many arguments that do not match`, () => { + console[ methodName ]( 'Unknown message.' ); + console[ methodName ]( message, 'Unknown param.' ); + expect( console ).not[ matcherNameWith ]( message ); + expect( () => + expect( console )[ matcherNameWith ]( message ) + ).toThrow( + /Expected mock function to be called with:.*but it was called with:.*Unknown param./s + ); + } ); + + test( 'assertions number gets incremented after every matcher call', () => { + const spy = console[ methodName ]; + // Call the console method so the matcher has something to assert + console[ methodName ]( message ); + expect( typeof ( spy as any ).assertionsNumber ).toBe( 'number' ); + const before = ( spy as any ).assertionsNumber; + expect( console )[ matcherName ](); + const after = ( spy as any ).assertionsNumber; + expect( after ).toBe( before + 1 ); + } ); + } ); +} ); +/* eslint-enable no-console */ diff --git a/packages/jest-console/tsconfig.json b/packages/jest-console/tsconfig.json new file mode 100644 index 00000000000000..17691f6c8d25e2 --- /dev/null +++ b/packages/jest-console/tsconfig.json @@ -0,0 +1,7 @@ +{ + "$schema": "https://json.schemastore.org/tsconfig.json", + "extends": "../../tsconfig.base.json", + "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": [] From df93b1963d865903dd2bf8db36a4b16cec8e498e Mon Sep 17 00:00:00 2001 From: Kushagra Goyal Date: Fri, 27 Jun 2025 19:24:17 +0530 Subject: [PATCH 2/6] fix: fix unit tests - remove js test file - add spaces and fix docs --- packages/jest-console/src/index.ts | 6 ++ packages/jest-console/src/matchers.ts | 4 + packages/jest-console/src/test/index.test.js | 93 -------------------- packages/jest-console/src/test/index.test.ts | 36 ++++++-- 4 files changed, 41 insertions(+), 98 deletions(-) delete mode 100644 packages/jest-console/src/test/index.test.js diff --git a/packages/jest-console/src/index.ts b/packages/jest-console/src/index.ts index a63e222548319a..aa3411711dc094 100644 --- a/packages/jest-console/src/index.ts +++ b/packages/jest-console/src/index.ts @@ -15,11 +15,17 @@ const setConsoleMethodSpy = ( args: [ string, string ] ) => { .spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' ) .mockName( `console.${ methodName }` ); + /** + * Resets the spy to its initial state. + */ function resetSpy() { spy.mockReset(); ( spy as any ).assertionsNumber = 0; } + /** + * Verifies that the spy has only been called if expected. + */ function assertExpectedCalls() { if ( ( spy as any ).assertionsNumber === 0 && diff --git a/packages/jest-console/src/matchers.ts b/packages/jest-console/src/matchers.ts index 7e849f2bf76f9a..a40841540f2491 100644 --- a/packages/jest-console/src/matchers.ts +++ b/packages/jest-console/src/matchers.ts @@ -45,9 +45,11 @@ const createSpyInfo = ( expected?: any[] ) => { const calls = spy.mock.calls; + const pass = expected ? JSON.stringify( calls ).includes( JSON.stringify( expected ) ) : calls.length > 0; + const message = createErrorMessage( { spy, pass, @@ -56,6 +58,7 @@ const createSpyInfo = ( methodName, expected, } ); + return { pass, message, @@ -85,6 +88,7 @@ expect.extend( Object.entries( supportedMatchers ).reduce( ( result, [ methodName, matcherName ] ) => { const matcherNameWith = `${ matcherName }With`; + return { ...result, [ matcherName ]: createToHaveBeenCalledMatcher( diff --git a/packages/jest-console/src/test/index.test.js b/packages/jest-console/src/test/index.test.js deleted file mode 100644 index caebf6915ab5c8..00000000000000 --- a/packages/jest-console/src/test/index.test.js +++ /dev/null @@ -1,93 +0,0 @@ -/* eslint-disable no-console */ - -/** - * Internal dependencies - */ -import '../matchers'; - -describe( 'jest-console', () => { - describe.each( [ - [ 'error', 'toHaveErrored' ], - [ 'info', 'toHaveInformed' ], - [ 'log', 'toHaveLogged' ], - [ 'warn', 'toHaveWarned' ], - ] )( 'console.%s', ( methodName, matcherName ) => { - const matcherNameWith = `${ matcherName }With`; - const message = `This is ${ methodName }!`; - - test( `${ matcherName } works`, () => { - console[ methodName ]( message ); - - expect( console )[ matcherName ](); - } ); - - test( `${ matcherName } works when not called`, () => { - expect( console ).not[ matcherName ](); - expect( () => expect( console )[ matcherName ]() ).toThrow( - 'Expected mock function to be called.' - ); - } ); - - test( `${ matcherNameWith } works with arguments that match`, () => { - console[ methodName ]( message ); - - expect( console )[ matcherNameWith ]( message ); - } ); - - test( `${ matcherNameWith } works when not called`, () => { - expect( console ).not[ matcherNameWith ]( message ); - expect( () => - expect( console )[ matcherNameWith ]( message ) - ).toThrow( - /Expected mock function to be called with:.*but it was called with:/s - ); - } ); - - test( `${ matcherNameWith } works with many arguments that do not match`, () => { - console[ methodName ]( 'Unknown message.' ); - console[ methodName ]( message, 'Unknown param.' ); - - expect( console ).not[ matcherNameWith ]( message ); - expect( () => - expect( console )[ matcherNameWith ]( message ) - ).toThrow( - /Expected mock function to be called with:.*but it was called with:.*Unknown param./s - ); - } ); - - test( 'assertions number gets incremented after every matcher call', () => { - const spy = console[ methodName ]; - - expect( spy.assertionsNumber ).toBe( 0 ); - - console[ methodName ]( message ); - - expect( console )[ matcherName ](); - expect( spy.assertionsNumber ).toBe( 1 ); - - expect( console )[ matcherNameWith ]( message ); - expect( spy.assertionsNumber ).toBe( 2 ); - } ); - - describe( 'lifecycle', () => { - beforeAll( () => { - // Disable reason: - // This is a difficult one to test, since the matcher's - // own lifecycle is defined to run before ours. Infer - // that we're being watched by testing the console - // method as being a spy. - // eslint-disable-next-line jest/no-standalone-expect - expect( - console[ methodName ].assertionsNumber - ).toBeGreaterThanOrEqual( 0 ); - } ); - - // Disable reason: - // See beforeAll implementation and explanation added there. - // eslint-disable-next-line jest/expect-expect - it( 'captures logging in lifecycle', () => {} ); - } ); - } ); -} ); - -/* eslint-enable no-console */ diff --git a/packages/jest-console/src/test/index.test.ts b/packages/jest-console/src/test/index.test.ts index 0f0525e446fc67..4c60ddfe14cc61 100644 --- a/packages/jest-console/src/test/index.test.ts +++ b/packages/jest-console/src/test/index.test.ts @@ -17,6 +17,7 @@ describe( 'jest-console', () => { test( `${ matcherName } works`, () => { console[ methodName ]( message ); + expect( console )[ matcherName ](); } ); @@ -29,6 +30,7 @@ describe( 'jest-console', () => { test( `${ matcherNameWith } works with arguments that match`, () => { console[ methodName ]( message ); + expect( console )[ matcherNameWith ]( message ); } ); @@ -44,6 +46,7 @@ describe( 'jest-console', () => { test( `${ matcherNameWith } works with many arguments that do not match`, () => { console[ methodName ]( 'Unknown message.' ); console[ methodName ]( message, 'Unknown param.' ); + expect( console ).not[ matcherNameWith ]( message ); expect( () => expect( console )[ matcherNameWith ]( message ) @@ -54,14 +57,37 @@ describe( 'jest-console', () => { test( 'assertions number gets incremented after every matcher call', () => { const spy = console[ methodName ]; - // Call the console method so the matcher has something to assert + + expect( ( spy as any ).assertionsNumber ).toBe( 0 ); + console[ methodName ]( message ); - expect( typeof ( spy as any ).assertionsNumber ).toBe( 'number' ); - const before = ( spy as any ).assertionsNumber; + expect( console )[ matcherName ](); - const after = ( spy as any ).assertionsNumber; - expect( after ).toBe( before + 1 ); + expect( ( spy as any ).assertionsNumber ).toBe( 1 ); + + expect( console )[ matcherNameWith ]( message ); + expect( ( spy as any ).assertionsNumber ).toBe( 2 ); + } ); + + describe( 'lifecycle', () => { + beforeAll( () => { + // Disable reason: + // This is a difficult one to test, since the matcher's + // own lifecycle is defined to run before ours. Infer + // that we're being watched by testing the console + // method as being a spy. + // eslint-disable-next-line jest/no-standalone-expect + expect( + console[ methodName ].assertionsNumber + ).toBeGreaterThanOrEqual( 0 ); + } ); + + // Disable reason: + // See beforeAll implementation and explanation added there. + // eslint-disable-next-line jest/expect-expect + it( 'captures logging in lifecycle', () => {} ); } ); } ); } ); + /* eslint-enable no-console */ From 44aa270e7d0aaa82894a4f268e91fb9e9b342f51 Mon Sep 17 00:00:00 2001 From: Kushagra Goyal Date: Fri, 4 Jul 2025 15:42:10 +0530 Subject: [PATCH 3/6] fix: remove unnecessary type assertion for assertionsNumber in jest-console tests --- packages/jest-console/src/test/index.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/jest-console/src/test/index.test.ts b/packages/jest-console/src/test/index.test.ts index 4c60ddfe14cc61..caebf6915ab5c8 100644 --- a/packages/jest-console/src/test/index.test.ts +++ b/packages/jest-console/src/test/index.test.ts @@ -58,15 +58,15 @@ describe( 'jest-console', () => { test( 'assertions number gets incremented after every matcher call', () => { const spy = console[ methodName ]; - expect( ( spy as any ).assertionsNumber ).toBe( 0 ); + expect( spy.assertionsNumber ).toBe( 0 ); console[ methodName ]( message ); expect( console )[ matcherName ](); - expect( ( spy as any ).assertionsNumber ).toBe( 1 ); + expect( spy.assertionsNumber ).toBe( 1 ); expect( console )[ matcherNameWith ]( message ); - expect( ( spy as any ).assertionsNumber ).toBe( 2 ); + expect( spy.assertionsNumber ).toBe( 2 ); } ); describe( 'lifecycle', () => { From 5c627f2cd7c106fcbe1101277655d9bbbec43ab3 Mon Sep 17 00:00:00 2001 From: Kushagra Goyal Date: Fri, 11 Jul 2025 18:59:57 +0530 Subject: [PATCH 4/6] feat: add ExtendedMock type and refactor matcher functions to use it --- packages/jest-console/src/index.ts | 10 +++---- packages/jest-console/src/matchers.ts | 38 +++++++++++++++++++-------- packages/jest-console/src/types.ts | 34 ++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 packages/jest-console/src/types.ts diff --git a/packages/jest-console/src/index.ts b/packages/jest-console/src/index.ts index aa3411711dc094..09cd7bccc78f37 100644 --- a/packages/jest-console/src/index.ts +++ b/packages/jest-console/src/index.ts @@ -3,6 +3,7 @@ */ 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. @@ -13,24 +14,21 @@ const setConsoleMethodSpy = ( args: [ string, string ] ) => { const [ methodName, matcherName ] = args; const spy = jest .spyOn( console, methodName as 'error' | 'info' | 'log' | 'warn' ) - .mockName( `console.${ methodName }` ); + .mockName( `console.${ methodName }` ) as ExtendedMock; /** * Resets the spy to its initial state. */ function resetSpy() { spy.mockReset(); - ( spy as any ).assertionsNumber = 0; + spy.assertionsNumber = 0; } /** * Verifies that the spy has only been called if expected. */ function assertExpectedCalls() { - if ( - ( spy as any ).assertionsNumber === 0 && - spy.mock.calls.length > 0 - ) { + if ( spy.assertionsNumber === 0 && spy.mock.calls.length > 0 ) { expect( console ).not[ matcherName ](); } } diff --git a/packages/jest-console/src/matchers.ts b/packages/jest-console/src/matchers.ts index a40841540f2491..d45e48990bbc85 100644 --- a/packages/jest-console/src/matchers.ts +++ b/packages/jest-console/src/matchers.ts @@ -8,14 +8,20 @@ import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils'; */ import supportedMatchers from './supported-matchers'; import type { Mock } from 'jest-mock'; +import type { + ExtendedMock, + MatcherFunction, + MatcherResult, + MatcherWithArgsFunction, +} from './types'; interface SpyInfo { spy: Mock; pass: boolean; - calls: any[][]; + calls: unknown[][]; matcherName: string; methodName: string; - expected?: any[]; + expected?: unknown[]; } const createErrorMessage = ( spyInfo: SpyInfo ) => { @@ -42,7 +48,7 @@ const createSpyInfo = ( spy: Mock, matcherName: string, methodName: string, - expected?: any[] + expected?: unknown[] ) => { const calls = spy.mock.calls; @@ -66,10 +72,11 @@ const createSpyInfo = ( }; const createToHaveBeenCalledMatcher = - ( matcherName: string, methodName: string ) => ( received: any ) => { - const spy = received[ methodName ] as Mock; + ( matcherName: string, methodName: string ) => + ( received: Record< string, Mock > ): MatcherResult => { + const spy = received[ methodName ] as ExtendedMock; const spyInfo = createSpyInfo( spy, matcherName, methodName ); - ( spy as any ).assertionsNumber += 1; + spy.assertionsNumber += 1; return spyInfo; }; @@ -77,15 +84,24 @@ const createToHaveBeenCalledWith = ( matcherName: string, methodName: string ) => - function ( received: any, ...expected: any[] ) { - const spy = received[ methodName ] as Mock; + function ( + received: Record< string, Mock >, + ...expected: unknown[] + ): MatcherResult { + const spy = received[ methodName ] as ExtendedMock; const spyInfo = createSpyInfo( spy, matcherName, methodName, expected ); - ( spy as any ).assertionsNumber += 1; + spy.assertionsNumber += 1; return spyInfo; }; +// Define the return type of our reduce call +type MatchersObject = Record< + string, + MatcherFunction | MatcherWithArgsFunction +>; + expect.extend( - Object.entries( supportedMatchers ).reduce( + Object.entries( supportedMatchers ).reduce< MatchersObject >( ( result, [ methodName, matcherName ] ) => { const matcherNameWith = `${ matcherName }With`; @@ -101,6 +117,6 @@ expect.extend( ), }; }, - {} as Record< string, any > + {} ) ); 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; From 7b00a268c4e990a0035efb23cd8ba9e6ce78f4df Mon Sep 17 00:00:00 2001 From: Kushagra Goyal Date: Fri, 11 Jul 2025 19:01:57 +0530 Subject: [PATCH 5/6] fix: remove unnecessary comment about return type in matchers.ts --- packages/jest-console/src/matchers.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/jest-console/src/matchers.ts b/packages/jest-console/src/matchers.ts index d45e48990bbc85..de25c186128a3b 100644 --- a/packages/jest-console/src/matchers.ts +++ b/packages/jest-console/src/matchers.ts @@ -94,7 +94,6 @@ const createToHaveBeenCalledWith = ( return spyInfo; }; -// Define the return type of our reduce call type MatchersObject = Record< string, MatcherFunction | MatcherWithArgsFunction From 28300d77256c6fec04edea0d0b16d89ac52da387 Mon Sep 17 00:00:00 2001 From: Kushagra Goyal Date: Thu, 24 Jul 2025 14:03:51 +0530 Subject: [PATCH 6/6] feat: add TypeScript declarations for custom Jest matchers and update package configuration --- packages/jest-console/package.json | 4 ++-- .../{types/index.d.ts => src/declarations.d.ts} | 8 ++++---- packages/jest-console/tsconfig.json | 1 + 3 files changed, 7 insertions(+), 6 deletions(-) rename packages/jest-console/{types/index.d.ts => src/declarations.d.ts} (84%) diff --git a/packages/jest-console/package.json b/packages/jest-console/package.json index 3270f68d93ddf4..fc8103828812b3 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/tsconfig.json b/packages/jest-console/tsconfig.json index 17691f6c8d25e2..f58ead725263bf 100644 --- a/packages/jest-console/tsconfig.json +++ b/packages/jest-console/tsconfig.json @@ -1,6 +1,7 @@ { "$schema": "https://json.schemastore.org/tsconfig.json", "extends": "../../tsconfig.base.json", + "files": [ "src/declarations.d.ts" ], "compilerOptions": { "types": [ "jest", "@testing-library/jest-dom" ] }