@digitaldefiance/express-suite-test-utils 1.0.9 → 1.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -97,12 +97,79 @@ it('should log message', async () => {
|
|
|
97
97
|
});
|
|
98
98
|
```
|
|
99
99
|
|
|
100
|
+
### Direct Log Mocks
|
|
101
|
+
|
|
102
|
+
Mock `fs.writeSync` for testing direct console output:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
import { withDirectLogMocks, directLogContains, getDirectLogMessages } from '@digitaldefiance/express-suite-test-utils';
|
|
106
|
+
import * as fs from 'fs';
|
|
107
|
+
|
|
108
|
+
// Important: Mock fs module at module level before importing
|
|
109
|
+
jest.mock('fs', () => ({
|
|
110
|
+
...jest.requireActual('fs'),
|
|
111
|
+
writeSync: jest.fn(),
|
|
112
|
+
}));
|
|
113
|
+
|
|
114
|
+
it('should capture direct writes to stdout', async () => {
|
|
115
|
+
await withDirectLogMocks({ mute: true }, async (spies) => {
|
|
116
|
+
const buffer = Buffer.from('hello world\n', 'utf8');
|
|
117
|
+
fs.writeSync(1, buffer); // stdout
|
|
118
|
+
|
|
119
|
+
expect(directLogContains(spies.writeSync, 1, 'hello', 'world')).toBe(true);
|
|
120
|
+
expect(getDirectLogMessages(spies.writeSync, 1)).toEqual(['hello world\n']);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Mongoose Memory Database
|
|
126
|
+
|
|
127
|
+
In-memory MongoDB testing utilities using mongodb-memory-server:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
import { connectMemoryDB, disconnectMemoryDB, clearMemoryDB } from '@digitaldefiance/express-suite-test-utils';
|
|
131
|
+
import { User } from './models/user';
|
|
132
|
+
|
|
133
|
+
describe('User model', () => {
|
|
134
|
+
beforeAll(async () => {
|
|
135
|
+
await connectMemoryDB();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
afterAll(async () => {
|
|
139
|
+
await disconnectMemoryDB();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
afterEach(async () => {
|
|
143
|
+
await clearMemoryDB();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('should validate user schema', async () => {
|
|
147
|
+
const user = new User({ username: 'test', email: 'test@example.com' });
|
|
148
|
+
await user.validate(); // Real Mongoose validation!
|
|
149
|
+
|
|
150
|
+
await expect(async () => {
|
|
151
|
+
const invalid = new User({ username: 'ab' }); // too short
|
|
152
|
+
await invalid.validate();
|
|
153
|
+
}).rejects.toThrow();
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
**Note:** Requires `mongoose` as a peer dependency and `mongodb-memory-server` as a dependency (already included).
|
|
159
|
+
|
|
100
160
|
## License
|
|
101
161
|
|
|
102
162
|
MIT
|
|
103
163
|
|
|
104
164
|
## ChangeLog
|
|
105
165
|
|
|
166
|
+
### v1.0.10
|
|
167
|
+
|
|
168
|
+
- Fix direct-log mocks to work with non-configurable fs.writeSync in newer Node.js versions
|
|
169
|
+
- Add comprehensive mongoose memory database testing utilities
|
|
170
|
+
- Fix memory mongoose connectMemoryDB
|
|
171
|
+
|
|
172
|
+
|
|
106
173
|
### v1.0.9
|
|
107
174
|
|
|
108
175
|
- Add mongoose memory helpers
|
package/package.json
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { Connection } from 'mongoose';
|
|
2
2
|
/**
|
|
3
3
|
* Connect to in-memory MongoDB for testing
|
|
4
|
+
* @returns Object with both the connection and URI
|
|
4
5
|
*/
|
|
5
|
-
export declare function connectMemoryDB(): Promise<
|
|
6
|
+
export declare function connectMemoryDB(): Promise<{
|
|
7
|
+
connection: Connection;
|
|
8
|
+
uri: string;
|
|
9
|
+
}>;
|
|
6
10
|
/**
|
|
7
11
|
* Drop all collections and disconnect
|
|
8
12
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongoose-memory.d.ts","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-express-suite-test-utils/src/lib/mongoose-memory.ts"],"names":[],"mappings":"AACA,OAAiB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAKhD
|
|
1
|
+
{"version":3,"file":"mongoose-memory.d.ts","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-express-suite-test-utils/src/lib/mongoose-memory.ts"],"names":[],"mappings":"AACA,OAAiB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAKhD;;;GAGG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC;IAAE,UAAU,EAAE,UAAU,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC,CAsBxF;AAED;;GAEG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC,CAWxD;AAED;;GAEG;AACH,wBAAsB,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC,CAOnD"}
|
|
@@ -10,16 +10,25 @@ let mongoServer;
|
|
|
10
10
|
let connection;
|
|
11
11
|
/**
|
|
12
12
|
* Connect to in-memory MongoDB for testing
|
|
13
|
+
* @returns Object with both the connection and URI
|
|
13
14
|
*/
|
|
14
15
|
async function connectMemoryDB() {
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
// If mongoose is connected but we don't have our server, disconnect first
|
|
17
|
+
if (mongoose_1.default.connection.readyState !== 0 && !mongoServer) {
|
|
18
|
+
await mongoose_1.default.disconnect();
|
|
19
|
+
connection = undefined;
|
|
20
|
+
}
|
|
21
|
+
// Create new server if needed
|
|
22
|
+
if (!mongoServer) {
|
|
23
|
+
mongoServer = await mongodb_memory_server_1.MongoMemoryServer.create();
|
|
17
24
|
}
|
|
18
|
-
mongoServer = await mongodb_memory_server_1.MongoMemoryServer.create();
|
|
19
25
|
const uri = mongoServer.getUri();
|
|
20
|
-
|
|
26
|
+
// Connect if not already connected
|
|
27
|
+
if (mongoose_1.default.connection.readyState === 0) {
|
|
28
|
+
await mongoose_1.default.connect(uri);
|
|
29
|
+
}
|
|
21
30
|
connection = mongoose_1.default.connection;
|
|
22
|
-
return connection;
|
|
31
|
+
return { connection, uri };
|
|
23
32
|
}
|
|
24
33
|
/**
|
|
25
34
|
* Drop all collections and disconnect
|