Skip to content

Commit fa5037a

Browse files
fix(home): isolate test user to prevent duplicate key errors
Clean up admin user in afterAll and use a unique email prefix (admin-home-health) to avoid E11000 collisions when running all test suites together via test:coverage. Closes #3348
1 parent 6237160 commit fa5037a

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

modules/home/tests/home.integration.tests.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe('Home integration tests:', () => {
1818
let agent;
1919
let HomeService;
2020
let adminToken;
21+
let adminUser;
2122
let originalOrganizationsEnabled;
2223

2324
// init
@@ -41,15 +42,16 @@ describe('Home integration tests:', () => {
4142

4243
// Create admin user and sign JWT for health endpoint test
4344
const User = mongoose.model('User');
44-
const admin = await User.create({
45+
await User.deleteOne({ email: 'admin-home-health@test.com' });
46+
adminUser = await User.create({
4547
firstName: 'Admin',
4648
lastName: 'Health',
47-
email: 'admin-health@test.com',
49+
email: 'admin-home-health@test.com',
4850
password: 'W@os.jsI$Aw3$0m3',
4951
provider: 'local',
5052
roles: ['admin'],
5153
});
52-
adminToken = jwt.sign({ userId: admin.id }, config.jwt.secret, { expiresIn: config.jwt.expiresIn });
54+
adminToken = jwt.sign({ userId: adminUser.id }, config.jwt.secret, { expiresIn: config.jwt.expiresIn });
5355
} catch (err) {
5456
console.log(err);
5557
expect(err).toBeFalsy();
@@ -204,10 +206,16 @@ describe('Home integration tests:', () => {
204206
});
205207
});
206208

207-
// Mongoose disconnect
209+
// Cleanup and mongoose disconnect
208210
afterAll(async () => {
209211
jest.restoreAllMocks();
210212
config.organizations.enabled = originalOrganizationsEnabled;
213+
try {
214+
if (adminUser) {
215+
const User = mongoose.model('User');
216+
await User.deleteOne({ _id: adminUser._id });
217+
}
218+
} catch (_) { /* cleanup – ignore errors */ }
211219
try {
212220
await mongooseService.disconnect();
213221
} catch (err) {

0 commit comments

Comments
 (0)