Skip to content

Commit 045a4d4

Browse files
test(users): isolate count unit test from Organization model (deterministic CI)
Add repository-level mocks for organizations.repository.js and organizations.membership.repository.js so mongoose.model('Organization') is never evaluated at module-link time with --maxWorkers=2. Service-level mocks intercept call paths but ESM static imports on the real service files are still resolved by the V8 VM module linker in CI, causing MissingSchemaError when the Organization schema hasn't been registered yet in that worker's context.
1 parent ec3de30 commit 045a4d4

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

modules/users/tests/users.service.count.unit.tests.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,38 @@ jest.unstable_mockModule('../../organizations/services/organizations.crud.servic
3838
default: { create: jest.fn(), get: jest.fn(), remove: jest.fn() },
3939
}));
4040

41+
// Mock repositories directly so mongoose.model('Organization') / mongoose.model('Membership')
42+
// are never called at module-evaluation time (the service-level mocks above intercept call
43+
// paths but ESM static imports on the real service files are still resolved by the V8 VM
44+
// module linker in some Node/Jest versions — mocking at repository layer is the safe guard).
45+
jest.unstable_mockModule('../../organizations/repositories/organizations.repository.js', () => ({
46+
default: {
47+
list: jest.fn(),
48+
create: jest.fn(),
49+
get: jest.fn(),
50+
remove: jest.fn(),
51+
removeById: jest.fn(),
52+
findOne: jest.fn(),
53+
exists: jest.fn(),
54+
update: jest.fn(),
55+
updateById: jest.fn(),
56+
setPlan: jest.fn(),
57+
deleteMany: jest.fn(),
58+
},
59+
}));
60+
61+
jest.unstable_mockModule('../../organizations/repositories/organizations.membership.repository.js', () => ({
62+
default: {
63+
list: jest.fn(),
64+
create: jest.fn(),
65+
get: jest.fn(),
66+
remove: jest.fn(),
67+
count: jest.fn(),
68+
deleteMany: jest.fn(),
69+
aggregateCountByOrganizations: jest.fn(),
70+
},
71+
}));
72+
4173
jest.unstable_mockModule('../../organizations/lib/constants.js', () => ({
4274
MEMBERSHIP_ROLES: { OWNER: 'owner', MEMBER: 'member' },
4375
MEMBERSHIP_STATUSES: { ACTIVE: 'active', PENDING: 'pending' },

0 commit comments

Comments
 (0)