Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(@jest/types): add partial support for done callbacks in typings of each #13756

Merged
merged 11 commits into from
Jan 26, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- `[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-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

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 @@ -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', {
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
30 changes: 22 additions & 8 deletions packages/jest-types/__typetests__/each.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ expectType<void>(
}),
);
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 +60,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 +142,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 @@ -164,10 +174,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 +197,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