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.

jest-each tests fail when using parameters set in beforeAll

See original GitHub issue

🐛 Bug Report

You cannot pass a variable set in the beforeAll block as a parameter to a parameterized test, as .each is run before beforeAll.

To Reproduce

let values
beforeAll(() => {
  values = ['hello', 'world']
})

describe('test setting variable in before all', () => {
  test('non-parameterized', () => {
    expect(values).toHaveLength(2) // passes
  })

  test.each(values)('parameterized', (value) => {
    expect(value).toBeDefined() // fails - values is undefined
  })

  let otherValues = [1, 2, 3]
  test.each(otherValues)('parameterized with variable set right before test', (value) => {
    expect(value).toBeDefined() // passes
  })
})

Expected behavior

The beforeAll block should run before a parameterized test, in order to match the behaviour in non-parameterized tests.

Link to repl or repo (highly encouraged)

N/A see block of code above

Run npx envinfo --preset jest

 System:
    OS: macOS High Sierra 10.13.6
    CPU: x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 8.9.4 - ~/.nvm/versions/node/v8.9.4/bin/node
    npm: 6.1.0 - ~/.nvm/versions/node/v8.9.4/bin/npm
  npmPackages:
    jest: ^23.4.0 => 23.4.0 

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
ray-of-lightcommented, Apr 25, 2020

If anyone needs to compute variables dynamically in beforeAll block, here is a way to do so: pass function references instead of values. Code example:

let values;

function getFirstValue() {
  return values[0];
}

function getSecondValue() {
  return values[1];
}

beforeAll(() => {
  // Values of the array are computed dynamically
  values = ['hello', 'world'];
});

describe('Test setting variables in beforeAll', () => {
  test.each([getFirstValue, getSecondValue])('parameterized', (valueFn) => {
    let value = valueFn();
    // the rest of the test code...
  })
});

If the data array is long, one could experiment with iterators like this:

let values;
let i = -1;

function getValue() {
  i++;
  return values[i];
}

beforeAll(() => {
  values = ['hello', 'world'];
});

describe('Test setting variables in beforeAll', () => {
  test.each([getValue, getValue])('parameterized', (valueFn) => {
    let value = valueFn();
    // the rest of the test code...
  })
});
0reactions
github-actions[bot]commented, May 10, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Globals - Jest
Here the beforeAll ensures that the database is set up before tests run. If setup was synchronous, you could do this without beforeAll...
Read more >
Unable to run tests with jest.each - javascript - Stack Overflow
So, beforeAll will run after the tests were defined. This means the files array won't be defined while the tests are being registered....
Read more >
AfterAll - Pester
It's guaranteed to run even if tests fail. The typical usage is to clean up state or temporary used in tests. BeforeAll and...
Read more >
jest-each | Yarn - Package Manager
jest-each allows you to provide multiple arguments to your test / describe which results in the test/suite being run once per row of...
Read more >
jest-each - npm
.test : · To inject nested object values use you can supply a keyPath i.e. $variable.path.to.value · You can use $# to inject...
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