Skip to content

Instantly share code, notes, and snippets.

@singhAmandeep007
Last active April 10, 2024 07:58
Show Gist options
  • Select an option

  • Save singhAmandeep007/89d55f88fb6d015ec304e84d8cc56ace to your computer and use it in GitHub Desktop.

Select an option

Save singhAmandeep007/89d55f88fb6d015ec304e84d8cc56ace to your computer and use it in GitHub Desktop.
Concurrent test in jest v29.7
import { expect, test } from "@jest/globals";
function sum(...args: number[]): number {
return args.reduce((acc, val) => acc + val, 0);
}
function isSumOddOrEven(...args: number[]): string {
const total = sum(...args);
if (total % 2 === 0) {
return "even";
} else {
return "odd";
}
}
describe("examples of running test concurrently", () => {
test.concurrent.each`
a | b | expected
${1} | ${1} | ${2}
${1} | ${2} | ${3}
${2} | ${1} | ${3}
`("returns $expected when $a is added to $b", async ({ a, b, expected }) => {
expect(sum(a, b)).toBe(expected);
});
test.concurrent.each([
[1, 1, 2],
[1, 2, 3],
[2, 1, 3]
])(`adding %i and %i returns %p`, async (a, b, expected) => {
expect(sum(a, b)).toBe(expected);
});
test.concurrent.each<{
a: number;
b: number;
result: string;
isExpectedResult: boolean;
}>`
a | b | result | isExpectedResult
${1} | ${2} | ${"odd"} | ${true}
${3} | ${4} | ${"odd"} | ${true}
${5} | ${5} | ${"even"} | ${true}
${2} | ${3} | ${"even"} | ${false}
`(
"Does adding $a and $b returns $result? $isExpectedResult",
async ({ a, b, result, isExpectedResult }) => {
expect(isSumOddOrEven(a, b) === result).toBe(isExpectedResult);
}
);
test.concurrent.each([
{ nums: [1, 2, 3], result: "even", isExpectedResult: true },
{ nums: [1, 9, 4], result: "even" },
{ nums: [0, 2, 3], result: "even", isExpectedResult: false }
])(
"Does adding $nums return $result? $isExpectedResult",
async ({ nums, result, isExpectedResult = true }) => {
expect(isSumOddOrEven(...nums) === result).toBe(isExpectedResult);
}
);
test.concurrent.each([
[[1, 2, 3], "even", true],
[[1, 9, 4], "even"],
[[0, 2, 3], "even", false]
])(
"Does adding %o return %s? %p",
async (nums, result, isExpectedResult = true) => {
expect(isSumOddOrEven(...nums) === result).toBe(isExpectedResult);
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment