Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add types for jest test with describe.each?

const has = (object: Record<string, unknown>, key: string) => {
    return object != null && hasOwnProperty.call(object, key)
};

has.test.ts

describe('has', () => {
    const obj = {
        name: 'name',
        1: 1,
        false: false,
        undefined: undefined
    };
    describe.each([
        ['name', true],
        [1, true],
        [false, true],
        [undefined, true],
        ['no-such-key', false]
    ])('when key = %s', (key, expected) => {
        it(`should return ${expected}`, () => {
            expect(has(obj, key)).toBe(expected);
        });
    });
});

enter image description here

Does anyone have experience adding types for jest tests? I am using describe.each to loop over datasets. Though I am able to run the tests successfully, I want to fix that typing issue. Can someone help me?

like image 702
lch Avatar asked Aug 15 '19 04:08

lch


People also ask

What is the difference between describe and test in Jest?

describe breaks your test suite into components. Depending on your test strategy, you might have a describe for each function in your class, each module of your plugin, or each user-facing piece of functionality. You can also nest describes to further subdivide the suite. it is where you perform individual tests.

What is the use of describe in Jest?

Jest Basics: Describe Blocks. A describe block is used for organizing test cases in logical groups of tests. For example, we want to group all the tests for a specific class. We can further nest new describe blocks in an existing describe block.

What is the difference between describe and it in Jest?

The difference between describe and it in Jest is that describe lets us divide our test suite into sections. it is called to create individual tests which is used in the describe callback. const myBeverage = { delicious: true, sour: false, }; describe('my beverage', () => { it('is delicious', () => { expect(myBeverage.

What is test each in Jest?

jest-each allows you to provide multiple arguments to your test / describe which results in the test/suite being run once per row of parameters.


Video Answer


1 Answers

It seems that you don't have latest version of types for jest, try to update package @types/jest to latest version (it contains type definition for Each interface).

If that's not possible for some reasons you can always "extend" the types yourself using typescript feature called declaration-merging:

// jest.d.ts file

declare namespace jest {

  interface Each {
    // Exclusively arrays.
    <T extends any[]>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T) => any, timeout?: number) => void;
    // Not arrays.
    <T>(cases: ReadonlyArray<T>): (name: string, fn: (...args: T[]) => any, timeout?: number) => void;
    (cases: ReadonlyArray<ReadonlyArray<any>>): (
        name: string,
        fn: (...args: any[]) => any,
        timeout?: number
    ) => void;
    (strings: TemplateStringsArray, ...placeholders: any[]): (
        name: string,
        fn: (arg: any) => any,
        timeout?: number
    ) => void;
  }

  interface Describe {
    each: Each
  }
}

You might need also to specify typeRoots configuration option so that typescript can pick up your custom types

Upd: Sorry, I just noticed that your issue is not in absence of Each interface but in incorrect type. It seems that in your case typescript cannot infer the type correctly so you might want to specify generic type explicitly, e.g.:

type TestTuple = [string | number | boolean, boolean];

describe.each<TestTuple>([
  ['name', true],
  [1, true],
  [false, true],
  [undefined, true],
  ['no-such-key', false]
])('when key = %s', (a, b) => {
    // do your stuff
});
like image 147
Serhii Ohorodnyk Avatar answered Sep 30 '22 13:09

Serhii Ohorodnyk