-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
jest-each tests fail when using parameters set in beforeAll #7100
Comments
@mattphillips what do you think about this? |
All tests tests and hooks are collected before we start executing any of them, so you cannot use dynamic data from a hook to define |
Ah, we've added an error message now! Nice. I'm fine with adding a note in the FAQ or something, a lot of people want to use stuff in EDIT: We should probably throw on empty arrays as well, not just non-arrays |
FWIW, I've also run into this and expected beforeAll() to actually complete before The documentation on test.each doesn't say anything about this. |
@dandv it's documented under troubleshooting - hope that helps |
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:
|
Another example with a more complexe structure and Typescript: test.each<[string, () => Partial<TransactionsData>, any]>([
[
'empty data',
() => ({}),
{
type: {
type: 'any.required',
},
amount: {
type: 'any.required',
},
userId: {
type: 'any.required',
},
},
],
[
'type and negative amount',
() => ({
// @ts-expect-error validation test
type: 'foo',
amount: -1,
userId: customer1._id,
}),
{
type: {
type: 'any.required',
},
amount: {
type: 'any.required',
},
},
],
[
'not existent user',
() => ({
type: 'credit',
amount: 10,
userId: 'not-existent'
}),
{
userId: {
type: 'any.not-found'
},
},
]
])(
'admin creates a transaction with invalid %s',
(_, data, expectedErrors) => assertInvalidData(
app.service('kidsous-transactions').create(data()),
expectedErrors,
),
); As the submitted data is the only part where I need a variable define in This also allows me to keep raw data for the string representation of each case. |
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. |
🐛 Bug Report
You cannot pass a variable set in the
beforeAll
block as a parameter to a parameterized test, as.each
is run beforebeforeAll
.To Reproduce
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
The text was updated successfully, but these errors were encountered: