Jacob Paris
← Back to all content

Dynamically generate test cases with Jest

As long as you know which cases you want to test up-front (for example, you're not trying to generate them after a network request) you can use Jest to batch out multiple cases at once.

The syntax looks like this

test.each([])("test name", () => {})

Single argument per test

If you use the wildcard %s in your test name, it will inject the first string in your test case. The following test spec will generate five different tests.

js
1test.each([
2 'FULL_TIME',
3 'PART_TIME',
4 'SELF_EMPLOYED',
5 'UNEMPLOYED',
6 'RETIRED',
7])('can set employment type to %s', (type) => {
8
9 // Test content here
10}),
11
js
1test("can set employment type to FULL_TIME", () => {
2 const type = "FULL_TIME"
3
4 // Test content here
5})
6
7test("can set employment type to PART_TIME", () => {
8 const type = "PART_TIME"
9
10 // Test content here
11})
12
13test("can set employment type to SELF_EMPLOYED", () => {
14 const type = "SELF_EMPLOYED"
15
16 // Test content here
17})
18
19test("can set employment type to UNEMPLOYED", () => {
20 const type = "UNEMPLOYED"
21
22 // Test content here
23})
24
25test("can set employment type to RETIRED", () => {
26 const type = "RETIRED"
27
28 // Test content here
29})
30

Multiple arguments per test

Each item in the array can itself be an array of arguments.

In the test name, you can use %s multiple times in order to print out the argument.

js
1test.each([
2 ['FULL_TIME', 'FULL_TIME'],
3 ['FULL_TIME', 'PART_TIME'],
4 ['FULL_TIME', 'SELF_EMPLOYED'],
5 ['FULL_TIME', 'UNEMPLOYED'],
6 ['FULL_TIME', 'RETIRED'],
7 ['PART_TIME', 'FULL_TIME'],
8 ['PART_TIME', 'PART_TIME'],
9 ['PART_TIME', 'SELF_EMPLOYED'],
10 ['PART_TIME', 'UNEMPLOYED'],
11 ['PART_TIME', 'RETIRED'],
12 ['SELF_EMPLOYED', 'FULL_TIME'],
13 ['SELF_EMPLOYED', 'PART_TIME'],
14 ['SELF_EMPLOYED', 'SELF_EMPLOYED'],
15 ['SELF_EMPLOYED', 'UNEMPLOYED'],
16 ['SELF_EMPLOYED', 'RETIRED'],
17 ['UNEMPLOYED', 'FULL_TIME'],
18 ['UNEMPLOYED', 'PART_TIME'],
19 ['UNEMPLOYED', 'SELF_EMPLOYED'],
20 ['UNEMPLOYED', 'UNEMPLOYED'],
21 ['UNEMPLOYED', 'RETIRED'],
22 ['RETIRED', 'FULL_TIME'],
23 ['RETIRED', 'PART_TIME'],
24 ['RETIRED', 'SELF_EMPLOYED'],
25 ['RETIRED', 'UNEMPLOYED'],
26 ['RETIRED', 'RETIRED'],
27])('can set employment type from %s to %s', ([ firstType, secondType ]) => {
28
29 // Test content here
30}),
31
js
1test("can set employment type from FULL_TIME to FULL_TIME", () => {
2 const firstType = "FULL_TIME"
3 const secondType = "FULL_TIME"
4
5 // Test content here
6})
7
8test("can set employment type from FULL_TIME to PART_TIME", () => {
9 const firstType = "FULL_TIME"
10 const secondType = "PART_TIME"
11
12 // Test content here
13})
14
15test("can set employment type from FULL_TIME to SELF_EMPLOYED", () => {
16 const firstType = "FULL_TIME"
17 const secondType = "SELF_EMPLOYED"
18
19 // Test content here
20})
21
22// …and so on 22 more times
23
Professional headshot

Hi, I'm Jacob

Hey there! I'm a developer, designer, and digital nomad with a background in lean manufacturing.

About once per month, I send an email with new guides, new blog posts, and sneak peeks of what's coming next.

Everyone who subscribes gets access to the source code for this website and every example project for all my tutorials.

Stay up to date with everything I'm working on by entering your email below.

Unsubscribe at any time.