Using an async function in describe instead of beforeAll()? #4858
-
When using beforeAll(), the tests don't have access to any variables created inside the beforeAll callback. Is there anything wrong with doing something like this?
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Vitest runs tests in two phases:
If you don't use a hook, then |
Beta Was this translation helpful? Give feedback.
-
I've just been attempting the same thing, however I've experienced weird issues regarding the ordering of execution, presumably because of the different phases that @sheremet-va described. For example when I have: Describe 1 While you may expect everything to run in sequence, instead what happens is Describe 1 and Describe 2 are run first, before all of the tests, and then the tests are run 1a, 1b, 2a, 2b. In my case a shared/global variable was getting modified by Describe 2 and affecting tests 1a and 1b, even though it comes later in the script. So I think I will have to refactor to define the variable inside the describe (so they can be accessed by all tests) but use the - Describe 1 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
This is expected. As explained in #4858 (comment),
describe
andtest.each(posts)
() => { ... }
functions frombeforeAll(() => { ... })
andtest('kek', () => { ... })
,test.each(posts)('...', () => { ... })