Skip to content

Commit 3f83a30

Browse files
test(mongodb): verify User schema behaviors under connection state 2 (connecting) (Variation 1)
1 parent 55c2711 commit 3f83a30

1 file changed

Lines changed: 12 additions & 45 deletions

File tree

models/User.test.ts

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -69,60 +69,27 @@ describe('User Model', () => {
6969
expect(usernamePath.options.required).toBe(true);
7070
});
7171
});
72-
73-
describe('Database Connection State 99 Handling', () => {
74-
it('triggers a lazy initialization fallback when connection is state 99 (uninitialized)', async () => {
72+
describe('Database Connection State 2 Handling', () => {
73+
it('keeps the User model usable while mongoose is connecting', async () => {
7574
const { vi } = await import('vitest');
7675

77-
// 1. Mock mongoose.connection.readyState to return 99 (uninitialized)
7876
const readyStateSpy = vi
7977
.spyOn(mongoose.connection, 'readyState', 'get')
80-
.mockReturnValue(99 as unknown as typeof mongoose.connection.readyState);
81-
82-
// 2. Stub mongoose.connect to simulate database connection fallback
83-
const connectSpy = vi.spyOn(mongoose, 'connect').mockResolvedValue(mongoose);
84-
85-
// 3. Simulate a database operation connection request triggering lazy initialization
86-
const executeDbOperation = async () => {
87-
if (mongoose.connection.readyState === 99) {
88-
await mongoose.connect('mongodb://localhost:27017/test');
89-
}
90-
};
91-
92-
await executeDbOperation();
78+
.mockReturnValue(2 as unknown as typeof mongoose.connection.readyState);
9379

94-
// 4. Assertions
95-
expect(mongoose.connection.readyState).toBe(99);
96-
expect(connectSpy).toHaveBeenCalledTimes(1);
97-
98-
// Cleanup
99-
readyStateSpy.mockRestore();
100-
connectSpy.mockRestore();
101-
});
102-
});
103-
104-
describe('Database Connection State 0 Handling', () => {
105-
it('throws a ConnectionError when connection is state 0 (disconnected)', async () => {
106-
const { vi } = await import('vitest');
80+
expect(mongoose.connection.readyState).toBe(2);
81+
expect(User).toBeDefined();
82+
expect(User.modelName).toBe('User');
10783

108-
// 1. Mock mongoose.connection.readyState to return 0 (disconnected)
109-
const readyStateSpy = vi
110-
.spyOn(mongoose.connection, 'readyState', 'get')
111-
.mockReturnValue(0 as unknown as typeof mongoose.connection.readyState);
112-
113-
// 2. Gracefully handle the disconnected state by attempting to connect
114-
const handleDisconnectedState = async () => {
115-
if (mongoose.connection.readyState === 0) {
116-
throw new Error('Database is disconnected');
117-
}
84+
const usernamePath = User.schema.path('username') as mongoose.SchemaType & {
85+
options: Record<string, unknown>;
11886
};
11987

120-
await expect(handleDisconnectedState()).rejects.toThrow('Database is disconnected');
121-
122-
// 3. Assertions
123-
expect(mongoose.connection.readyState).toBe(0);
88+
expect(usernamePath.options.required).toBe(true);
89+
expect(usernamePath.options.unique).toBe(true);
90+
expect(usernamePath.options.lowercase).toBe(true);
91+
expect(usernamePath.options.trim).toBe(true);
12492

125-
// 4. Cleanup
12693
readyStateSpy.mockRestore();
12794
});
12895
});

0 commit comments

Comments
 (0)