lukeed/uvu

after is called at wrong time.

weepy opened this issue · 1 comments

weepy commented

I was having trouble with getting the "after" test callback to run at the right time.

// setup.js
import { test } from 'uvu'

test.before(async () => {
    dropDatabase()
})

test.after(async () => {
    closeDatabaseConnection()
})
// test-a.js
import "./setup.js"
import { test } from 'uvu'
test("should do something with database.", async () => ... ) 
// test-b.js
import "./setup.js"
import { test } from 'uvu'
test("should do something else with database.", async () => ... ) 

Now I was hoping that the closeDatabaseConnection() is called after both test's a and b are finished, but in fact it is called when test-a is finished, which is not useful since it closes the database connection and then test-b fails

The only work around I have got to work is creating a "zzz-teardown.js" file with the test.after within it - which is sorta guaranteed to run last bc of the filename. Obviously not a very nice hack.

Am I doing something wrong, or is there a better workaround or fix ?

That’s the way to do it for now. Hooks are tied to suites & there are no global lifecycle hooks yet. Closing as duplicate of #81