Write tests that are stable, independent, and non-flaky by following proper isolation and waiting practices. Tests should create their own data, clean up after themselves, and avoid dependencies on other tests or external state.
Write tests that are stable, independent, and non-flaky by following proper isolation and waiting practices. Tests should create their own data, clean up after themselves, and avoid dependencies on other tests or external state.
Key practices:
waitForLoadState("networkidle")
or wait for specific elements instead of arbitrary timeouts with waitForTimeout()
randomString()
) instead of hardcoded identifiers that might conflictExample of proper test isolation:
it("should create the membership of the org", async () => {
// Create test user for this specific test
const testUserForCreate = await userRepositoryFixture.create({
email: `test-create-${randomString()}@api.com`,
username: `test-create-${randomString()}`,
});
return request(app.getHttpServer())
.post(`/v2/organizations/${org.id}/memberships`)
.send({ userId: testUserForCreate.id, accepted: true, role: "MEMBER" })
.expect(201)
.then(async (response) => {
const createdMembership = response.body.data;
// ... assertions ...
// Clean up
await membershipRepositoryFixture.delete(createdMembership.id);
await userRepositoryFixture.deleteByEmail(testUserForCreate.email);
});
});
Reliable tests reduce CI failures, improve developer confidence, and prevent blocking of development workflows.
Enter the URL of a public GitHub repository