Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table for test.each cannot be loaded in beforeAll step #6888

Closed
meetmangukiya opened this issue Aug 25, 2018 · 7 comments
Closed

Table for test.each cannot be loaded in beforeAll step #6888

meetmangukiya opened this issue Aug 25, 2018 · 7 comments

Comments

@meetmangukiya
Copy link

meetmangukiya commented Aug 25, 2018

🐛 Bug Report

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.

To Reproduce

const tableData = [];

beforeAll(() => {
   tableData.push([1]);  
});

test.each(tableData, ('%d', (num) => console.log(num)));

Expected behavior

Test should run for all elements loaded into the table after execution of beforeAll.

@mattphillips
Copy link
Contributor

I had a similar issue to this in the original jest-each (mattphillips/jest-each#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’t recognise any tests when collecting the tests in a given test file, so I’m not sure if there is anything we can do?

cc/ @SimenB @thymikee @rickhanlonii

@SimenB
Copy link
Member

SimenB commented Sep 15, 2018

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

@LukasBombach
Copy link

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);
    }
  );
});

@agilov
Copy link

agilov commented Apr 26, 2020

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);
    }
  );
});

it seems that describe runs before beforeAll. Sot foo and bar will remain undefined.

@jondo89
Copy link

jondo89 commented Sep 3, 2020

How would one then pull in a sitemap.xml and set all of the endpoints?

@fguidobaldi
Copy link

fguidobaldi commented Apr 8, 2021

Is there any workaround to run test.each(table, fn) where table is a global state which is populated in beforeAll?

I have to execute async requests inside beforeAll, since they cannot be executed inside describe block.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants