You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, setup functions of fixtures execute the same way for each test. This is fine for most, but configuring the way the fixture is initialized per test/per test suite would be great for more complex cases.
For instance, consider the following setup with beforeEach/afterEach hooks for a given test suite:
describe('Context foo',()=>{letdb;beforeEach(async()=>{db=awaitcreateTestDb({// This should be configurable per test suite, or sometimes even per test if using `createTestDb` directlyschemaName: 'foo',// This as wellpathLayer: 'foo',// And this as wellseed: false,});});afterEach(async()=>{awaitclearTestDb(db);});test('Test 1',async()=>{// Some random IDdb.name;// Client setup for this testdb.client;});test('Test 2',async()=>{// Another random IDdb.name;// Different client instancedb.client;});});describe('Context bar',()=>{letdb;beforeEach(async()=>{db=awaitcreateTestDb({schemaName: 'bar',pathLayer: 'bar',seed: true,});});afterEach(async()=>{awaitclearTestDb(db);});test('Test 3',async()=>{// Yet another random IDdb.name;// Client instance 3db.client;});});
The setup is verbose, but flexible as it enables test-specific setups.
Suggested solution
It would be great to have an equivalent setup with fixtures, which could look like this:
// Default fixture setup for every test of the project. This is written only once in a setup filetest=test.extend({db: async({ schemaName, pathLayer, seed },use)=>{constdb=awaitcreateTestDb({ schemaName, pathLayer, seed });awaituse(db);awaitclearTestDb(db);},schemaName: 'public',pathLayer: 'public',seed: false,});// Specific test suitedescribe('Context foo',()=>{test.use({schemaName: 'foo',pathLayer: 'foo',seed: false});test('1',async({ db })=>{// Some random IDdb.name;// Client setup for this testdb.client;});test('2',async({ db })=>{// Another random IDdb.name;// Different client instancedb.client;});});describe('Context bar',()=>{test.use({schemaName: 'bar',pathLayer: 'bar',seed: true});test('3',async({ db })=>{// Yet another random IDdb.name;// Client instance 3db.client;});});
This is how Playwright enables configuring fixtures per test/per test suite (see here and there). They also have a concept of "option" fixtures, which might be suited for the aforementioned schemaName, pathLayer and seed fixtures (but they also have a concept of per-project config of those options, so I'm not sure whether this option concept should apply to Vitest in the same regard).
Clear and concise description of the problem
Currently, setup functions of fixtures execute the same way for each test. This is fine for most, but configuring the way the fixture is initialized per test/per test suite would be great for more complex cases.
For instance, consider the following setup with
beforeEach
/afterEach
hooks for a given test suite:The setup is verbose, but flexible as it enables test-specific setups.
Suggested solution
It would be great to have an equivalent setup with fixtures, which could look like this:
This is how Playwright enables configuring fixtures per test/per test suite (see here and there). They also have a concept of "option" fixtures, which might be suited for the aforementioned
schemaName
,pathLayer
andseed
fixtures (but they also have a concept of per-project config of those options, so I'm not sure whether this option concept should apply to Vitest in the same regard).Alternative
No response
Additional context
Initially posted as a question on Discord.
Validations
The text was updated successfully, but these errors were encountered: