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
fixtures are not preserved between beforeEach and test #4165
Comments
It sounds like a bug 🤔 We should have a test that tests this specifically 🤔 |
@Dunqing what do you think? |
I think this is a bug. Does this only happen in the latest version? |
Yes, it was not possible to access these fixtures in previous versions. |
Sorry to bother you again but I don't think #4168 resolves my issue The new behavior in
Whereas my desired behavior (similar to playwright) is:
Although I suppose this could be a philosophical question on whether or not fixtures should only be initialized once. Personally since vitest isn't Just for reference, in
|
Agreed 👍🏻 @Dunqing what do you think? |
While I think it's better to be consistent with In general, we don't need to initialize You can see this example. describe('case1', () => {
test('xxx', () => {})
})
describe('case2', () => {
test('xxx', () => {})
}) This way we will initialize the |
Performance issues aside, I think it makes more sense for fixtures to be scoped to individual tests. I use test fixtures for mocking my API and provisioning databases. Since they have internal states, I want to be able to access a fresh instance for every test. Essentially I'm replacing this pattern: let db
beforeEach(() => {
db = createSqliteDb(':memory:')
seedData(db)
})
afterEach(() => {
db.close()
})
test('test 1', () => {
doSomething(db)
expect(db.query()).toBe(...)
})
test('test 2', () => {
doSomethingElse(db)
expect(db.query()).toBe(...)
}) With: export const dbTest = test.extend({
db: async({ use }) => {
const db = createSqliteDb(':memory:')
seedData(db)
await use(db)
db.close()
}
})
dbTest('test 1', ({ db }) => {
doSomething(db)
expect(db.query()).toBe(...)
})
dbTest('test 2', ({ db }) => {
doSomethingElse(db)
expect(db.query()).toBe(...)
}) I'm not sure if this pattern is specific to playwright but I find it to be cleaner and allow reusability. Not having access to a fresh instance (for this use case) would defeat the purpose of fixtures for me since I would have to write the export const dbTest = test.extend({
db: async({ use }) => {
const db = createSqliteDb(':memory:')
await use(db)
db.close()
}
})
// have to be rewritten in every file that wants to use dbTest
beforeEach(({ db }) => {
seedData(db)
})
afterEach(({ db }) => {
dropTables(db)
}) |
Yes, In your case, It is more useful to initialize fixtures for each test. Does writing your need to re-initialise fixtures of test cases in |
I would disagree. As a user, I would expect |
Okay, I will handle this soon |
Describe the bug
Fixture instances are not preserved between
beforeEach
andtest
. This is counterintuitive because it defeats the purpose of having fixtures inbeforeEach
(I'm a bit biased because this is opposite to how playwright behaves)Reproduction
System Info
The text was updated successfully, but these errors were encountered: