Tests don't run if there is a test.each(table, fn) where table is a global state which is populated in beforeAll. But since jest analysis sees it as no tests it doesn't run beforeAll either.
const tableData = [];
beforeAll(() => {
tableData.push([1]);
});
test.each(tableData, ('%d', (num) => console.log(num)));
Test should run for all elements loaded into the table after execution of beforeAll.
I had a similar issue to this in the original jest-each (https://github.com/mattphillips/jest-each/issues/6). We would need Jest to run the beforeAll so the tests can be built up dynamically.
test.each is syntactic sugar for a forEach under the hood which means if the array is empty then Jest doesn鈥檛 recognise any tests when collecting the tests in a given test file, so I鈥檓 not sure if there is anything we can do?
cc/ @SimenB @thymikee @rickhanlonii
We're not gonna run any hooks before collecting tests, we don't wanna "execute" a file multiple times.
jest-each should probably throw an explicit error if given an empty array
For people finding this googling how to use jest.each with beforeAll ore beforeEach. You can still put those before your describe block:
let foo: string;
let bar: number;
beforeAll(async () => {
foo = await asyncFunctionA();
bar = await asyncFunctionB();
}, 10000);
afterAll(async () => {
await tearDown();
});
describe("Characteristic", () => {
it.each`
foo | bar | whatever | returnValue
${foo} | ${bar} | ${1} | ${2}
${foo} | ${bar} | ${2} | ${4}
${foo} | ${bar} | ${3} | ${6}
${foo} | ${bar} | ${4} | ${8}
`(
"table works with $foo, $bar and $whatever",
async ({ foo, bar, whatever, returnValue }) => {
expect(yourFunction(foo, bar, whatever)).toBe(returnValue);
}
);
});
For people finding this googling how to use
jest.eachwithbeforeAllorebeforeEach. You can still put those before yourdescribeblock:let foo: string; let bar: number; beforeAll(async () => { foo = await asyncFunctionA(); bar = await asyncFunctionB(); }, 10000); afterAll(async () => { await tearDown(); }); describe("Characteristic", () => { it.each` foo | bar | whatever | returnValue ${foo} | ${bar} | ${1} | ${2} ${foo} | ${bar} | ${2} | ${4} ${foo} | ${bar} | ${3} | ${6} ${foo} | ${bar} | ${4} | ${8} `( "table works with $foo, $bar and $whatever", async ({ foo, bar, whatever, returnValue }) => { expect(yourFunction(foo, bar, whatever)).toBe(returnValue); } ); });
it seems that describe runs before beforeAll. Sot foo and bar will remain undefined.
How would one then pull in a sitemap.xml and set all of the endpoints?