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:
- Created 5 years ago
- Reactions:8
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
If the data array is long, one could experiment with iterators like this:
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.