2.1.0 • Published 1 year ago

@wistudent/jest-each-improved-types v2.1.0

Weekly downloads
6
License
MIT
Repository
github
Last release
1 year ago

jest-each-improved-types

npm version

When using .each with the table notation, it is possible to set the type of the test function argument on the .each function as a generic argument:

import {test, expect} from "@jest/globals";

test.each<{a: string, b: number, expected: boolean}>`
a      | b    | expected
${'1'} | ${1} | ${true}
${'1'} | ${2} | ${false}
`('some test', ({a, b, expected}) => {
  expect(a === `${b}`).toBe(expected)
});

But this only sets the type for the test function argument, it does not ensure that the correct values were used inside the table. The following does not result in a typescript error:

import {test, expect} from "@jest/globals";

test.each<{a: string, b: number, expected: boolean}>`
a            | b     | expected
${undefined} | ${{}} | ${null}
`('some test', ({a, b, expected}) => {
  expect(a === `${b}`).toBe(expected)
});

This package adds an additional way of providing type definitons to .each table tests that ensures that the values used inside the table are compatible with the provided typescript types. It does by reexporting @jest/globals and extending the type definition of the .each function.

import {test, expect} from "@wistudent/jest-each-improved-types"

test.each<[['a', string], ['b', number], ['expected', boolean]], 2>`
a      | b    | expected
${'1'} | ${1} | ${true}
${'1'} | ${2} | ${false}
`('some test', ({a, b, expected}) => {
  expect(a === `${b}`).toBe(expected)
});

Instead of using an interface as the generic argument, a tuple of Key-Type pairs and the number of rows is used. Using a value with the wrong type inside the table, or the number of values not being (Number of Key-Type pairs * Declared number of rows), will result in a typescript error.

2.1.0

1 year ago

1.0.8

2 years ago

1.0.7

2 years ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.0-dev.0

1 year ago

2.0.4

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago