@digitaldefiance/express-suite-test-utils 1.0.7 → 1.0.9
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 +20 -6
- package/package.json +8 -3
- package/src/index.d.ts +2 -0
- package/src/index.d.ts.map +1 -1
- package/src/index.js +2 -0
- package/src/lib/direct-log.d.ts +38 -0
- package/src/lib/direct-log.d.ts.map +1 -0
- package/src/lib/direct-log.js +76 -0
- package/src/lib/mongoose-memory.d.ts +14 -0
- package/src/lib/mongoose-memory.d.ts.map +1 -0
- package/src/lib/mongoose-memory.js +48 -0
package/README.md
CHANGED
|
@@ -14,24 +14,30 @@ yarn add @digitaldefiance/express-suite-test-utils
|
|
|
14
14
|
|
|
15
15
|
## Usage
|
|
16
16
|
|
|
17
|
-
### Importing Test Utilities from
|
|
17
|
+
### Importing Test Utilities from Express Suite Packages
|
|
18
18
|
|
|
19
|
-
Test helpers and mocks
|
|
19
|
+
Test helpers and mocks are available via separate `/testing` entry points:
|
|
20
20
|
|
|
21
21
|
```typescript
|
|
22
|
+
// node-express-suite test helpers
|
|
22
23
|
import {
|
|
23
|
-
|
|
24
|
-
setupTestEnv
|
|
25
|
-
// ... other test helpers
|
|
24
|
+
createApplicationMock,
|
|
25
|
+
setupTestEnv
|
|
26
26
|
} from '@digitaldefiance/node-express-suite/testing';
|
|
27
27
|
|
|
28
|
+
// node-ecies-lib test mocks
|
|
29
|
+
import { mockBackendMember } from '@digitaldefiance/node-ecies-lib/testing';
|
|
30
|
+
|
|
31
|
+
// ecies-lib test mocks
|
|
32
|
+
import { mockFrontendMember } from '@digitaldefiance/ecies-lib/testing';
|
|
33
|
+
|
|
28
34
|
// Use in your tests
|
|
29
35
|
beforeAll(async () => {
|
|
30
36
|
await setupTestEnv();
|
|
31
37
|
});
|
|
32
38
|
```
|
|
33
39
|
|
|
34
|
-
**Note:**
|
|
40
|
+
**Note:** All `/testing` entry points require `@faker-js/faker` as a peer dependency:
|
|
35
41
|
|
|
36
42
|
```bash
|
|
37
43
|
npm install -D @faker-js/faker
|
|
@@ -97,6 +103,14 @@ MIT
|
|
|
97
103
|
|
|
98
104
|
## ChangeLog
|
|
99
105
|
|
|
106
|
+
### v1.0.9
|
|
107
|
+
|
|
108
|
+
- Add mongoose memory helpers
|
|
109
|
+
|
|
110
|
+
### v1.0.8
|
|
111
|
+
|
|
112
|
+
- Add directLog mocks
|
|
113
|
+
|
|
100
114
|
### v1.0.6
|
|
101
115
|
|
|
102
116
|
- Add react mocks
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@digitaldefiance/express-suite-test-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "Test utilities for Digital Defiance Express Suite",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "src/index.d.ts",
|
|
@@ -37,10 +37,15 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@jest/globals": "^30.0.5",
|
|
39
39
|
"@types/jest": "^30.0.0",
|
|
40
|
-
"expect": "^30.0.5"
|
|
40
|
+
"expect": "^30.0.5",
|
|
41
|
+
"mongodb-memory-server": "^10.1.3"
|
|
42
|
+
},
|
|
43
|
+
"peerDependencies": {
|
|
44
|
+
"mongoose": "^8.0.0"
|
|
41
45
|
},
|
|
42
46
|
"devDependencies": {
|
|
43
|
-
"@types/node": "^22.0.0"
|
|
47
|
+
"@types/node": "^22.0.0",
|
|
48
|
+
"mongoose": "^8.9.3"
|
|
44
49
|
},
|
|
45
50
|
"type": "commonjs"
|
|
46
51
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export * from './lib/to-throw-type';
|
|
2
2
|
export * from './lib/console';
|
|
3
|
+
export * from './lib/direct-log';
|
|
3
4
|
export * from './lib/localStorage-mock';
|
|
4
5
|
export * from './lib/bson-mock';
|
|
5
6
|
export * from './lib/react-mocks';
|
|
7
|
+
export * from './lib/mongoose-memory';
|
|
6
8
|
//# sourceMappingURL=index.d.ts.map
|
package/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../packages/digitaldefiance-express-suite-test-utils/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,yBAAyB,CAAC;AACxC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../packages/digitaldefiance-express-suite-test-utils/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC;AAC9B,cAAc,kBAAkB,CAAC;AACjC,cAAc,yBAAyB,CAAC;AACxC,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC;AAClC,cAAc,uBAAuB,CAAC"}
|
package/src/index.js
CHANGED
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./lib/to-throw-type"), exports);
|
|
5
5
|
tslib_1.__exportStar(require("./lib/console"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./lib/direct-log"), exports);
|
|
6
7
|
tslib_1.__exportStar(require("./lib/localStorage-mock"), exports);
|
|
7
8
|
tslib_1.__exportStar(require("./lib/bson-mock"), exports);
|
|
8
9
|
tslib_1.__exportStar(require("./lib/react-mocks"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./lib/mongoose-memory"), exports);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spies returned from withDirectLogMocks
|
|
3
|
+
*/
|
|
4
|
+
export interface DirectLogSpies {
|
|
5
|
+
writeSync: jest.Mock;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Options for withDirectLogMocks
|
|
9
|
+
*/
|
|
10
|
+
export interface WithDirectLogOptions {
|
|
11
|
+
/**
|
|
12
|
+
* If false, do not mute the output (let it pass through).
|
|
13
|
+
* By default (true), mutes output.
|
|
14
|
+
*/
|
|
15
|
+
mute?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Wrap a test body with fs.writeSync spy for directLog testing.
|
|
19
|
+
* By default mutes output (does nothing on write).
|
|
20
|
+
* The spy will capture calls with file descriptor and buffer.
|
|
21
|
+
*
|
|
22
|
+
* Note: Requires fs module to be mocked at module level with jest.mock('fs')
|
|
23
|
+
*/
|
|
24
|
+
export declare function withDirectLogMocks<T = unknown>(options: WithDirectLogOptions, fn: (spies: DirectLogSpies) => Promise<T> | T): Promise<T>;
|
|
25
|
+
/**
|
|
26
|
+
* Helper to check if writeSync was called with a specific file descriptor and message.
|
|
27
|
+
* @param spy The writeSync mock
|
|
28
|
+
* @param fd The file descriptor (1 for stdout, 2 for stderr)
|
|
29
|
+
* @param needles Substrings to search for in the buffer content
|
|
30
|
+
*/
|
|
31
|
+
export declare function directLogContains(spy: jest.Mock, fd: number, ...needles: string[]): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Get all messages written to a specific file descriptor.
|
|
34
|
+
* @param spy The writeSync mock
|
|
35
|
+
* @param fd The file descriptor (1 for stdout, 2 for stderr)
|
|
36
|
+
*/
|
|
37
|
+
export declare function getDirectLogMessages(spy: jest.Mock, fd: number): string[];
|
|
38
|
+
//# sourceMappingURL=direct-log.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"direct-log.d.ts","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-express-suite-test-utils/src/lib/direct-log.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,IAAI,CAAC,IAAI,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAED;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,GAAG,OAAO,EAClD,OAAO,EAAE,oBAAoB,EAC7B,EAAE,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,GAC5C,OAAO,CAAC,CAAC,CAAC,CAgCZ;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,IAAI,CAAC,IAAI,EACd,EAAE,EAAE,MAAM,EACV,GAAG,OAAO,EAAE,MAAM,EAAE,GACnB,OAAO,CAWT;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAClC,GAAG,EAAE,IAAI,CAAC,IAAI,EACd,EAAE,EAAE,MAAM,GACT,MAAM,EAAE,CASV"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.withDirectLogMocks = withDirectLogMocks;
|
|
4
|
+
exports.directLogContains = directLogContains;
|
|
5
|
+
exports.getDirectLogMessages = getDirectLogMessages;
|
|
6
|
+
const tslib_1 = require("tslib");
|
|
7
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
8
|
+
/**
|
|
9
|
+
* Wrap a test body with fs.writeSync spy for directLog testing.
|
|
10
|
+
* By default mutes output (does nothing on write).
|
|
11
|
+
* The spy will capture calls with file descriptor and buffer.
|
|
12
|
+
*
|
|
13
|
+
* Note: Requires fs module to be mocked at module level with jest.mock('fs')
|
|
14
|
+
*/
|
|
15
|
+
async function withDirectLogMocks(options, fn) {
|
|
16
|
+
const mute = options?.mute !== false; // default true
|
|
17
|
+
// Get the mocked writeSync function
|
|
18
|
+
const writeSync = fs.writeSync;
|
|
19
|
+
// Store previous mock implementation if any
|
|
20
|
+
const previousImpl = writeSync.getMockImplementation();
|
|
21
|
+
// Set implementation based on mute option
|
|
22
|
+
if (mute) {
|
|
23
|
+
writeSync.mockImplementation(() => undefined);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
// Pass through - requires original implementation to be available
|
|
27
|
+
writeSync.mockImplementation((...args) => {
|
|
28
|
+
// In test environment, we can't easily call the real fs.writeSync
|
|
29
|
+
// so we just no-op but track the calls
|
|
30
|
+
return args[1]?.length || 0;
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
const spies = { writeSync };
|
|
34
|
+
try {
|
|
35
|
+
return await fn(spies);
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
// Clear calls and restore previous implementation
|
|
39
|
+
writeSync.mockClear();
|
|
40
|
+
if (previousImpl) {
|
|
41
|
+
writeSync.mockImplementation(previousImpl);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Helper to check if writeSync was called with a specific file descriptor and message.
|
|
47
|
+
* @param spy The writeSync mock
|
|
48
|
+
* @param fd The file descriptor (1 for stdout, 2 for stderr)
|
|
49
|
+
* @param needles Substrings to search for in the buffer content
|
|
50
|
+
*/
|
|
51
|
+
function directLogContains(spy, fd, ...needles) {
|
|
52
|
+
return spy.mock.calls.some((args) => {
|
|
53
|
+
const [calledFd, buffer] = args;
|
|
54
|
+
if (calledFd !== fd)
|
|
55
|
+
return false;
|
|
56
|
+
const text = buffer instanceof Buffer
|
|
57
|
+
? buffer.toString('utf8')
|
|
58
|
+
: String(buffer);
|
|
59
|
+
return needles.every((n) => text.includes(n));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get all messages written to a specific file descriptor.
|
|
64
|
+
* @param spy The writeSync mock
|
|
65
|
+
* @param fd The file descriptor (1 for stdout, 2 for stderr)
|
|
66
|
+
*/
|
|
67
|
+
function getDirectLogMessages(spy, fd) {
|
|
68
|
+
return spy.mock.calls
|
|
69
|
+
.filter((args) => args[0] === fd)
|
|
70
|
+
.map((args) => {
|
|
71
|
+
const buffer = args[1];
|
|
72
|
+
return buffer instanceof Buffer
|
|
73
|
+
? buffer.toString('utf8')
|
|
74
|
+
: String(buffer);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Connection } from 'mongoose';
|
|
2
|
+
/**
|
|
3
|
+
* Connect to in-memory MongoDB for testing
|
|
4
|
+
*/
|
|
5
|
+
export declare function connectMemoryDB(): Promise<Connection>;
|
|
6
|
+
/**
|
|
7
|
+
* Drop all collections and disconnect
|
|
8
|
+
*/
|
|
9
|
+
export declare function disconnectMemoryDB(): Promise<void>;
|
|
10
|
+
/**
|
|
11
|
+
* Clear all collections without disconnecting
|
|
12
|
+
*/
|
|
13
|
+
export declare function clearMemoryDB(): Promise<void>;
|
|
14
|
+
//# sourceMappingURL=mongoose-memory.d.ts.map
|
|
@@ -0,0 +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;;GAEG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC,UAAU,CAAC,CAY3D;AAED;;GAEG;AACH,wBAAsB,kBAAkB,IAAI,OAAO,CAAC,IAAI,CAAC,CAWxD;AAED;;GAEG;AACH,wBAAsB,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC,CAOnD"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.connectMemoryDB = connectMemoryDB;
|
|
4
|
+
exports.disconnectMemoryDB = disconnectMemoryDB;
|
|
5
|
+
exports.clearMemoryDB = clearMemoryDB;
|
|
6
|
+
const tslib_1 = require("tslib");
|
|
7
|
+
const mongodb_memory_server_1 = require("mongodb-memory-server");
|
|
8
|
+
const mongoose_1 = tslib_1.__importDefault(require("mongoose"));
|
|
9
|
+
let mongoServer;
|
|
10
|
+
let connection;
|
|
11
|
+
/**
|
|
12
|
+
* Connect to in-memory MongoDB for testing
|
|
13
|
+
*/
|
|
14
|
+
async function connectMemoryDB() {
|
|
15
|
+
if (connection && connection.readyState === 1) {
|
|
16
|
+
return connection;
|
|
17
|
+
}
|
|
18
|
+
mongoServer = await mongodb_memory_server_1.MongoMemoryServer.create();
|
|
19
|
+
const uri = mongoServer.getUri();
|
|
20
|
+
await mongoose_1.default.connect(uri);
|
|
21
|
+
connection = mongoose_1.default.connection;
|
|
22
|
+
return connection;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Drop all collections and disconnect
|
|
26
|
+
*/
|
|
27
|
+
async function disconnectMemoryDB() {
|
|
28
|
+
if (connection) {
|
|
29
|
+
await connection.dropDatabase();
|
|
30
|
+
await mongoose_1.default.disconnect();
|
|
31
|
+
connection = undefined;
|
|
32
|
+
}
|
|
33
|
+
if (mongoServer) {
|
|
34
|
+
await mongoServer.stop();
|
|
35
|
+
mongoServer = undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Clear all collections without disconnecting
|
|
40
|
+
*/
|
|
41
|
+
async function clearMemoryDB() {
|
|
42
|
+
if (connection) {
|
|
43
|
+
const collections = connection.collections;
|
|
44
|
+
for (const key in collections) {
|
|
45
|
+
await collections[key].deleteMany({});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|