in fixtures.ts I perform some API calls that create objects in database and returns their ids.
After all tests are done, I need clean the created objects in database.
How do I pass the ids to global.teardown.ts?
What I have
I import fixtures module with a singleton array of guid in global.teardown.ts but the module is reinitialized and array is empty.
global.teardown.ts
import { entityGuidsToDelete, api } from './fixtures'
async function globalTeardown() {
//not working yet, because the array is always empty
for(const entityGuid of entityGuidsToDelete) {
await api.deleteEntity(entityGuid);
}
}
export default globalTeardown;
playwright.config.ts
export default defineConfig({
...
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
],
globalTeardown: require.resolve('./global-teardown')
});
type Fixtures = {
userApi: Api;
newEntity: LegalEntityModel;
};
export const entityGuidsToDelete = new Array<string>();
export const test = base.extend<Fixtures>({
...
newEntity: async ({ userApi }, use) => {
const newEntity = await createNewEntity()
entityGuidsToDelete.push(newEntity.id); //this is being called
await use(newEntity);
}
}
export { expect } from '@playwright/test';
2
Answers
I would use an scope
worker
fixture:In this example an
entities
array will be created per worker. Then, you can use that fixture in theforEachWork
and do something with them afteruse
.This is how you can use it on a test:
The good news for this is you already have pretty much everything you need, you just have to reorganize a bit.
What happens now is, every time an entity is created by a fixture, your cleanup will automatically run once that test is over. No need to collect all of the IDs for-loop over them afterward.