question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Table for test.each cannot be loaded in beforeAll step

See original GitHub issue

🐛 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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

18reactions
LukasBombachcommented, Sep 10, 2019

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);
    }
  );
});
4reactions
SimenBcommented, 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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure the test.each table with variables defined in ...
From the doc Defining Tests says: Tests must be defined synchronously for Jest to be able to collect your tests.
Read more >
Setup and Teardown - Jest
By default, the beforeAll and afterAll blocks apply to every test in a file. You can also group tests together using a describe...
Read more >
Mocha - the fun, simple, flexible JavaScript test framework
When a test file is loaded, Mocha executes all of its suites and finds–but does not execute–any hooks and tests therein. Top-level hooks,...
Read more >
Testing - Spring
The Spring TestContext Framework provides consistent loading of Spring ... A TestContextManager is created for each test class (for example, ...
Read more >
Best Practices - Cypress Documentation
Instead, adding the data-cy attribute to the element gives us a targeted selector that's only used for testing. The data-cy attribute will not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found