Jest's test.each function is helpful for running the same test with different data.
The use of template literals for providing the data is nice and easy to read
However, I have run into a problem. I would like to use the same data set for multiple different test cases. Here is an example with just one test for a dataset:
describe("Updating fields", () => {
test.each`
fieldLabel | defaultValue | newValue
${"Name"} | ${"My Name"} | ${"Newname"}
${"Job"} | ${"My Job"} | ${"Newjob"}
`(
"$fieldLabel field does not update until submit button is clicked",
({ fieldLabel, defaultValue, newValue }) => {
userEvent.type(screen.getByLabelText(fieldLabel), newValue);
expect(screen.queryByText(defaultValue)).toBeTruthy();
expect(screen.queryByText(newValue)).toBeFalsy();
}
);
});
But I would like to include another test to run with the same data:
"$fieldLabel field updates from $defaultValue to $newValue when submitted", (fieldLabel, defaultValue, newValue) => {
userEvent.type(screen.getByLabelText(fieldLabel), newValue);
userEvent.click(screen.getByText("Submit"));
expect(screen.queryByText(newValue)).toBeTruthy();
expect(screen.queryByText(defaultValue)).toBeFalsy();
});
Is there any way I can include multiple tests in a single each function?