Skip to content

Playwright-style automatic fixtures #4953

@jakebiesinger-storyhealth

Description

Clear and concise description of the problem

Playwright has a notion of automatic fixtures, where the fixture is executed even if it is not part of the fixture dependency list in a test. This enables test writers to implicitly set up fixtures that are common to all tests of a type.

I would like to use automatic fixtures in vitest to establish database connections in my integration tests rather than having to explicitly specify { testDb } as a fixture dependency in all of my tests.

Suggested solution

Playwright has a nice API for this, where you pass a tuple of [fixtureBody, config] instead of directly passing only the fixture body, something like:

export const myTest = test.extend({
  testDb: [

    async ({}, use) => { await setUpDb(); await use(); await tearDownDb(); },

    { auto: true } // <-- config object

  ],
 someOtherFixture: ...
})

It looks like the idea of fixture config was also present in the original vitest-fixtures package, but may have only been for fixture scope.

This aligns nicely with playwright's API and gives a path for future expansions of fixture-specific config (scopes, etc as playwright does).

Alternative

Maybe we could expose a way to access the fixtures themselves, and then wrap our tests / before* calls in some helper method that calls the fixtures manually, but this seems like it'd be very confusing.

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions