From 5907b2256f28bab9a68387c1294686fd45ea8c48 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 11 Jan 2023 19:38:42 +0200 Subject: [PATCH 1/8] fix(@jest/types): support `done` callback in `each` types --- .../jest-types/__typetests__/each.test.ts | 38 ++++++++++++++----- packages/jest-types/src/Global.ts | 11 +++--- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/packages/jest-types/__typetests__/each.test.ts b/packages/jest-types/__typetests__/each.test.ts index 3c9212ca5ed4..23bf9cb0f8a0 100644 --- a/packages/jest-types/__typetests__/each.test.ts +++ b/packages/jest-types/__typetests__/each.test.ts @@ -33,14 +33,20 @@ expectType( }), ); expectType( - test.each(list)('some test', a => { - expectType(a); - }), + test.each(list)( + 'some test', + a => { + expectType(a); + }, + 1000, + ), ); expectType( - test.each(tupleList)('some test', b => { + test.each(tupleList)('some test', (b, done) => { expectType<'one' | 'two' | 'three'>(b); + + expectType<(reason?: string | Error) => void>(done); }), ); expectType( @@ -54,8 +60,10 @@ expectType( ); expectType( - test.each([3, 4, 'seven'])('some test', c => { + test.each([3, 4, 'seven'])('some test', (c, done) => { expectType(c); + + expectType<(reason?: string | Error) => void>(done); }), ); expectType( @@ -88,11 +96,13 @@ expectType( ); expectType( - test.each(tupleTable)('some test', (a, b, expected, extra) => { + test.each(tupleTable)('some test', (a, b, expected, extra, done) => { expectType(a); expectType(b); expectType(expected); expectType(extra); + + expectType<(reason?: string | Error) => void>(done); }), ); expectType( @@ -112,10 +122,12 @@ expectType( test.each([ [1, 2, 'three'], [3, 4, 'seven'], - ])('some test', (a, b, expected) => { + ])('some test', (a, b, expected, done) => { expectType(a); expectType(b); expectType(expected); + + expectType<(reason?: string | Error) => void>(done); }), ); expectType( @@ -134,11 +146,13 @@ expectType( ); expectType( - test.each(objectTable)('some test', ({a, b, expected, extra}) => { + test.each(objectTable)('some test', ({a, b, expected, extra}, done) => { expectType(a); expectType(b); expectType(expected); expectType(extra); + + expectType<(reason?: string | Error) => void>(done); }), ); expectType( @@ -164,10 +178,12 @@ expectType( ${1} | ${1} | ${2} ${1} | ${2} | ${3} ${2} | ${1} | ${3} - `('some test', ({a, b, expected}) => { + `('some test', ({a, b, expected}, done) => { expectType(a); expectType(b); expectType(expected); + + expectType<(reason?: string | Error) => void>(done); }), ); expectType( @@ -185,9 +201,11 @@ expectType( item | expected ${'a'} | ${true} ${'b'} | ${false} - `('some test', ({item, expected}) => { + `('some test', ({item, expected}, done) => { expectType(item); expectType(expected); + + expectType<(reason?: string | Error) => void>(done); }), ); expectType( diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 6a1bbecb235a..3f6445f7e6f2 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -59,18 +59,19 @@ interface Each { // when the table is an array of object literals >(table: ReadonlyArray): ( name: string | NameLike, - fn: (arg: T) => ReturnType, + fn: (arg: T, done: DoneFn) => ReturnType, timeout?: number, ) => void; // when the table is an array of tuples ]>(table: ReadonlyArray): ( name: string | NameLike, - fn: (...args: T) => ReturnType, + fn: (...args: [...T, DoneFn]) => ReturnType, timeout?: number, ) => void; // when the table is an array of arrays + // impossible to implement `done` callback, because arguments would be of type `number | string | DoneFn` >(table: ReadonlyArray): ( name: string | NameLike, fn: (...args: T) => ReturnType, @@ -80,14 +81,14 @@ interface Each { // when the table is a tuple or array (table: ReadonlyArray): ( name: string | NameLike, - fn: (arg: T) => ReturnType, + fn: (arg: T, done: DoneFn) => ReturnType, timeout?: number, ) => void; // when the table is a template literal (strings: TemplateStringsArray, ...expressions: Array): ( name: string | NameLike, - fn: (arg: Record) => ReturnType, + fn: (arg: Record, done: DoneFn) => ReturnType, timeout?: number, ) => void; @@ -97,7 +98,7 @@ interface Each { ...expressions: Array ): ( name: string | NameLike, - fn: (arg: T) => ReturnType, + fn: (arg: T, done: DoneFn) => ReturnType, timeout?: number, ) => void; } From 372f410038c275a8110b9619500fffbaf5bd2a2f Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 11 Jan 2023 19:44:29 +0200 Subject: [PATCH 2/8] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 067791482e22..6b23eb0dab2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - `[jest-runtime]` Support Wasm files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608)) - `[jest-runtime]` Using the scriptTransformer cache in jest-runner ([#13735](https://github.com/facebook/jest/pull/13735)) - `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694)) +- `[@jest/types]` Support `done` callbacks in `each` types ([#13756](https://github.com/facebook/jest/pull/13756)) ### Chore & Maintenance From 0a284daa633ab902bb9b21f7a04347b67f054b5b Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Wed, 11 Jan 2023 19:45:31 +0200 Subject: [PATCH 3/8] tweak --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b23eb0dab2b..3d6935a4079a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ - `[jest-runtime]` Support Wasm files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608)) - `[jest-runtime]` Using the scriptTransformer cache in jest-runner ([#13735](https://github.com/facebook/jest/pull/13735)) - `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694)) -- `[@jest/types]` Support `done` callbacks in `each` types ([#13756](https://github.com/facebook/jest/pull/13756)) +- `[@jest/types]` Support `done` callbacks in typings of `each` ([#13756](https://github.com/facebook/jest/pull/13756)) ### Chore & Maintenance From 5e041cb3ee7be72c15427867d80d610e085a843e Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 14 Jan 2023 15:16:49 +0200 Subject: [PATCH 4/8] partial support --- packages/jest-types/__typetests__/each.test.ts | 8 ++------ packages/jest-types/src/Global.ts | 3 +-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/jest-types/__typetests__/each.test.ts b/packages/jest-types/__typetests__/each.test.ts index 23bf9cb0f8a0..cbfc07cf27a5 100644 --- a/packages/jest-types/__typetests__/each.test.ts +++ b/packages/jest-types/__typetests__/each.test.ts @@ -96,13 +96,11 @@ expectType( ); expectType( - test.each(tupleTable)('some test', (a, b, expected, extra, done) => { + test.each(tupleTable)('some test', (a, b, expected, extra) => { expectType(a); expectType(b); expectType(expected); expectType(extra); - - expectType<(reason?: string | Error) => void>(done); }), ); expectType( @@ -122,12 +120,10 @@ expectType( test.each([ [1, 2, 'three'], [3, 4, 'seven'], - ])('some test', (a, b, expected, done) => { + ])('some test', (a, b, expected) => { expectType(a); expectType(b); expectType(expected); - - expectType<(reason?: string | Error) => void>(done); }), ); expectType( diff --git a/packages/jest-types/src/Global.ts b/packages/jest-types/src/Global.ts index 3f6445f7e6f2..87b9449be23b 100644 --- a/packages/jest-types/src/Global.ts +++ b/packages/jest-types/src/Global.ts @@ -66,12 +66,11 @@ interface Each { // when the table is an array of tuples ]>(table: ReadonlyArray): ( name: string | NameLike, - fn: (...args: [...T, DoneFn]) => ReturnType, + fn: (...args: T) => ReturnType, timeout?: number, ) => void; // when the table is an array of arrays - // impossible to implement `done` callback, because arguments would be of type `number | string | DoneFn` >(table: ReadonlyArray): ( name: string | NameLike, fn: (...args: T) => ReturnType, From 7f3d25cf48500aaee27fb18d4ef4673576f9e5ec Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 14 Jan 2023 15:16:59 +0200 Subject: [PATCH 5/8] clean up tests --- packages/babel-jest/src/__tests__/index.ts | 30 ++++--------------- .../src/__tests__/hooksError.test.ts | 11 +------ 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/packages/babel-jest/src/__tests__/index.ts b/packages/babel-jest/src/__tests__/index.ts index 9903c2b773b0..386a50a6df60 100644 --- a/packages/babel-jest/src/__tests__/index.ts +++ b/packages/babel-jest/src/__tests__/index.ts @@ -106,38 +106,20 @@ test('Returns source string with inline maps when no transformOptions is passed describe('caller option correctly merges from defaults and options', () => { test.each([ [ - { - supportsDynamicImport: true, - supportsStaticESM: true, - }, - { - supportsDynamicImport: true, - supportsStaticESM: true, - }, + {supportsDynamicImport: true, supportsStaticESM: true}, + {supportsDynamicImport: true, supportsStaticESM: true}, ], [ - { - supportsDynamicImport: false, - supportsStaticESM: false, - }, - { - supportsDynamicImport: false, - supportsStaticESM: false, - }, + {supportsDynamicImport: false, supportsStaticESM: false}, + {supportsDynamicImport: false, supportsStaticESM: false}, ], [ {supportsStaticESM: false}, - { - supportsDynamicImport: false, - supportsStaticESM: false, - }, + {supportsDynamicImport: false, supportsStaticESM: false}, ], [ {supportsDynamicImport: true}, - { - supportsDynamicImport: true, - supportsStaticESM: false, - }, + {supportsDynamicImport: true, supportsStaticESM: false}, ], ])('%j -> %j', (input, output) => { defaultBabelJestTransformer.process(sourceString, 'dummy_path.js', { diff --git a/packages/jest-circus/src/__tests__/hooksError.test.ts b/packages/jest-circus/src/__tests__/hooksError.test.ts index a7a08414d43e..08c135ffe03f 100644 --- a/packages/jest-circus/src/__tests__/hooksError.test.ts +++ b/packages/jest-circus/src/__tests__/hooksError.test.ts @@ -10,16 +10,7 @@ import circus from '../'; describe.each(['beforeEach', 'beforeAll', 'afterEach', 'afterAll'] as const)( '%s hooks error throwing', fn => { - test.each([ - ['String'], - [1], - [[]], - [{}], - [Symbol('hello')], - [true], - [null], - [undefined], - ])( + test.each(['String', 1, [], {}, Symbol('hello'), true, null, undefined])( `${fn} throws an error when %p is provided as a first argument to it`, el => { expect(() => { From 1be88b5f3b86d497f59b61cf17cdb9a49edbff09 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Sat, 14 Jan 2023 15:18:30 +0200 Subject: [PATCH 6/8] tweak changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3d6935a4079a..64a037bf1ee9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ - `[jest-runtime]` Support Wasm files that import JS resources ([#13608](https://github.com/facebook/jest/pull/13608)) - `[jest-runtime]` Using the scriptTransformer cache in jest-runner ([#13735](https://github.com/facebook/jest/pull/13735)) - `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694)) -- `[@jest/types]` Support `done` callbacks in typings of `each` ([#13756](https://github.com/facebook/jest/pull/13756)) +- `[@jest/types]` Add partial support for `done` callbacks in typings of `each` ([#13756](https://github.com/facebook/jest/pull/13756)) ### Chore & Maintenance From 3af2d669ce039da73f77ed8ac8a4705863497008 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 26 Jan 2023 11:38:57 +0200 Subject: [PATCH 7/8] fix changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64d1d902a84b..1add3d3fb0bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ ### Fixes - `[@jest/expect-utils]` `toMatchObject` diffs should include `Symbol` properties ([#13810](https://github.com/facebook/jest/pull/13810)) +- `[@jest/types]` Add partial support for `done` callbacks in typings of `each` ([#13756](https://github.com/facebook/jest/pull/13756)) ### Chore & Maintenance @@ -38,7 +39,6 @@ - `[jest-runtime]` Enforce import assertions when importing JSON in ESM ([#12755](https://github.com/facebook/jest/pull/12755) & [#13805](https://github.com/facebook/jest/pull/13805)) - `[jest-snapshot]` Make sure to import `babel` outside of the sandbox ([#13694](https://github.com/facebook/jest/pull/13694)) - `[jest-transform]` Ensure the correct configuration is passed to preprocessors specified multiple times in the `transform` option ([#13770](https://github.com/facebook/jest/pull/13770)) -- `[@jest/types]` Add partial support for `done` callbacks in typings of `each` ([#13756](https://github.com/facebook/jest/pull/13756)) ### Chore & Maintenance From 80b1e42fdb2dd9abf9225b90dddf52458ad87770 Mon Sep 17 00:00:00 2001 From: Tom Mrazauskas Date: Thu, 26 Jan 2023 12:05:24 +0200 Subject: [PATCH 8/8] tweak type tests --- packages/jest-types/__typetests__/each.test.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/jest-types/__typetests__/each.test.ts b/packages/jest-types/__typetests__/each.test.ts index cbfc07cf27a5..cc3965cd3e96 100644 --- a/packages/jest-types/__typetests__/each.test.ts +++ b/packages/jest-types/__typetests__/each.test.ts @@ -28,8 +28,10 @@ const objectTable = [ // test.each expectType( - test.each(list)('some test', a => { + test.each(list)('some test', (a, done) => { expectType(a); + + expectType<(reason?: string | Error) => void>(done); }), ); expectType( @@ -158,11 +160,13 @@ expectType( {a: 5, b: 6, expected: 'eleven'}, ])( 'some test', - ({a, b, expected, extra}) => { + ({a, b, expected, extra}, done) => { expectType(a); expectType(b); expectType(expected); expectType(extra); + + expectType<(reason?: string | Error) => void>(done); }, 1000, ),