Skip to content

Commit

Permalink
fix(@jest/types): add partial support for done callbacks in typings…
Browse files Browse the repository at this point in the history
… of `each` (#13756)
  • Loading branch information
mrazauskas authored Jan 26, 2023
1 parent fbf0fa8 commit 8884105
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

- `[@jest/expect-utils]` `toMatchObject` diffs should include `Symbol` properties ([#13810](https://github.com/facebook/jest/pull/13810))
- `[jest-runtime]` Handle missing `replaceProperty` ([#13823](https://github.com/facebook/jest/pull/13823))
- `[@jest/types]` Add partial support for `done` callbacks in typings of `each` ([#13756](https://github.com/facebook/jest/pull/13756))

### Chore & Maintenance

Expand Down
30 changes: 6 additions & 24 deletions packages/babel-jest/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,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', {
Expand Down
11 changes: 1 addition & 10 deletions packages/jest-circus/src/__tests__/hooksError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
38 changes: 28 additions & 10 deletions packages/jest-types/__typetests__/each.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,27 @@ const objectTable = [
// test.each

expectType<void>(
test.each(list)('some test', a => {
test.each(list)('some test', (a, done) => {
expectType<number>(a);

expectType<(reason?: string | Error) => void>(done);
}),
);
expectType<void>(
test.each(list)('some test', a => {
expectType<number>(a);
}),
test.each(list)(
'some test',
a => {
expectType<number>(a);
},
1000,
),
);

expectType<void>(
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<void>(
Expand All @@ -54,8 +62,10 @@ expectType<void>(
);

expectType<void>(
test.each([3, 4, 'seven'])('some test', c => {
test.each([3, 4, 'seven'])('some test', (c, done) => {
expectType<string | number>(c);

expectType<(reason?: string | Error) => void>(done);
}),
);
expectType<void>(
Expand Down Expand Up @@ -134,11 +144,13 @@ expectType<void>(
);

expectType<void>(
test.each(objectTable)('some test', ({a, b, expected, extra}) => {
test.each(objectTable)('some test', ({a, b, expected, extra}, done) => {
expectType<number>(a);
expectType<number>(b);
expectType<string>(expected);
expectType<boolean | undefined>(extra);

expectType<(reason?: string | Error) => void>(done);
}),
);
expectType<void>(
Expand All @@ -148,11 +160,13 @@ expectType<void>(
{a: 5, b: 6, expected: 'eleven'},
])(
'some test',
({a, b, expected, extra}) => {
({a, b, expected, extra}, done) => {
expectType<number>(a);
expectType<number>(b);
expectType<string>(expected);
expectType<boolean | undefined>(extra);

expectType<(reason?: string | Error) => void>(done);
},
1000,
),
Expand All @@ -164,10 +178,12 @@ expectType<void>(
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`('some test', ({a, b, expected}) => {
`('some test', ({a, b, expected}, done) => {
expectType<number>(a);
expectType<number>(b);
expectType<number>(expected);

expectType<(reason?: string | Error) => void>(done);
}),
);
expectType<void>(
Expand All @@ -185,9 +201,11 @@ expectType<void>(
item | expected
${'a'} | ${true}
${'b'} | ${false}
`('some test', ({item, expected}) => {
`('some test', ({item, expected}, done) => {
expectType<string>(item);
expectType<boolean>(expected);

expectType<(reason?: string | Error) => void>(done);
}),
);
expectType<void>(
Expand Down
8 changes: 4 additions & 4 deletions packages/jest-types/src/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface Each<EachFn extends TestFn | BlockFn> {
// when the table is an array of object literals
<T extends Record<string, unknown>>(table: ReadonlyArray<T>): (
name: string | NameLike,
fn: (arg: T) => ReturnType<EachFn>,
fn: (arg: T, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;

Expand All @@ -80,14 +80,14 @@ interface Each<EachFn extends TestFn | BlockFn> {
// when the table is a tuple or array
<T>(table: ReadonlyArray<T>): (
name: string | NameLike,
fn: (arg: T) => ReturnType<EachFn>,
fn: (arg: T, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;

// when the table is a template literal
<T = unknown>(strings: TemplateStringsArray, ...expressions: Array<T>): (
name: string | NameLike,
fn: (arg: Record<string, T>) => ReturnType<EachFn>,
fn: (arg: Record<string, T>, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;

Expand All @@ -97,7 +97,7 @@ interface Each<EachFn extends TestFn | BlockFn> {
...expressions: Array<unknown>
): (
name: string | NameLike,
fn: (arg: T) => ReturnType<EachFn>,
fn: (arg: T, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;
}
Expand Down

0 comments on commit 8884105

Please sign in to comment.