@tsed/testcontainers-mongo 7.80.0
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/lib/cjs/index.js +5 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/services/ContainerUtils.js +63 -0
- package/lib/cjs/services/ContainerUtils.js.map +1 -0
- package/lib/cjs/services/TestContainersMongo.js +41 -0
- package/lib/cjs/services/TestContainersMongo.js.map +1 -0
- package/lib/cjs/setup/vi.setup.js +13 -0
- package/lib/cjs/setup/vi.setup.js.map +1 -0
- package/lib/esm/index.js +2 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/services/ContainerUtils.js +55 -0
- package/lib/esm/services/ContainerUtils.js.map +1 -0
- package/lib/esm/services/TestContainersMongo.js +37 -0
- package/lib/esm/services/TestContainersMongo.js.map +1 -0
- package/lib/esm/setup/vi.setup.js +8 -0
- package/lib/esm/setup/vi.setup.js.map +1 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/services/ContainerUtils.d.ts +27 -0
- package/lib/types/services/TestContainersMongo.d.ts +11 -0
- package/lib/types/setup/vi.setup.d.ts +2 -0
- package/lib/types-esm/index.d.ts +1 -0
- package/lib/types-esm/services/ContainerUtils.d.ts +27 -0
- package/lib/types-esm/services/TestContainersMongo.d.ts +11 -0
- package/lib/types-esm/setup/vi.setup.d.ts +2 -0
- package/package.json +57 -0
- package/readme.md +165 -0
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,4EAAkD"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMongoConnectionsOptions = exports.getMongoConnectionOptions = exports.getMongoUrl = exports.stopMongoServer = exports.startMongoServer = void 0;
|
|
4
|
+
const mongodb_1 = require("@testcontainers/mongodb");
|
|
5
|
+
const KEY = "TEST_CONTAINER_MONGO";
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
7
|
+
// @ts-ignore
|
|
8
|
+
global[KEY] = null;
|
|
9
|
+
function getEnvironment(key) {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
return global[key];
|
|
13
|
+
}
|
|
14
|
+
function setEnvironment(key, environment) {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
global[key] = environment;
|
|
18
|
+
}
|
|
19
|
+
function createMongoContainer(image = "mongo:6.0.1") {
|
|
20
|
+
return new mongodb_1.MongoDBContainer(image).start();
|
|
21
|
+
}
|
|
22
|
+
async function startMongoServer(image) {
|
|
23
|
+
// await stopMockServer()
|
|
24
|
+
const container = getEnvironment(KEY) || (await createMongoContainer(image));
|
|
25
|
+
setEnvironment(KEY, container);
|
|
26
|
+
process.env.MONGO_URL = container.getConnectionString();
|
|
27
|
+
return {
|
|
28
|
+
container,
|
|
29
|
+
url: process.env.MONGO_URL
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.startMongoServer = startMongoServer;
|
|
33
|
+
async function stopMongoServer() {
|
|
34
|
+
const container = getEnvironment(KEY);
|
|
35
|
+
process.stdout.write("Stopping mongo server\n");
|
|
36
|
+
if (container) {
|
|
37
|
+
await container.stop();
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.stopMongoServer = stopMongoServer;
|
|
41
|
+
function getMongoUrl() {
|
|
42
|
+
return process.env.MONGO_URL;
|
|
43
|
+
}
|
|
44
|
+
exports.getMongoUrl = getMongoUrl;
|
|
45
|
+
function getMongoConnectionOptions(id, opts = {}) {
|
|
46
|
+
const seed = Math.floor(Math.random() * 10000);
|
|
47
|
+
const { dbName = "db-test", ...otherOpts } = opts;
|
|
48
|
+
const url = `${getMongoUrl()}/${dbName}-${seed}`;
|
|
49
|
+
return {
|
|
50
|
+
id,
|
|
51
|
+
url,
|
|
52
|
+
connectionOptions: {
|
|
53
|
+
directConnection: true,
|
|
54
|
+
...otherOpts
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
exports.getMongoConnectionOptions = getMongoConnectionOptions;
|
|
59
|
+
function getMongoConnectionsOptions(opts = {}) {
|
|
60
|
+
return [getMongoConnectionOptions("default", opts)];
|
|
61
|
+
}
|
|
62
|
+
exports.getMongoConnectionsOptions = getMongoConnectionsOptions;
|
|
63
|
+
//# sourceMappingURL=ContainerUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContainerUtils.js","sourceRoot":"","sources":["../../../src/services/ContainerUtils.ts"],"names":[],"mappings":";;;AAAA,qDAAkF;AAalF,MAAM,GAAG,GAAG,sBAAsB,CAAC;AAEnC,6DAA6D;AAC7D,aAAa;AACb,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;AAEnB,SAAS,cAAc,CAAI,GAAW;IACpC,6DAA6D;IAC7D,aAAa;IACb,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;AACrB,CAAC;AAED,SAAS,cAAc,CAAC,GAAW,EAAE,WAAoB;IACvD,6DAA6D;IAC7D,aAAa;IACb,MAAM,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC;AAC5B,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAK,GAAG,aAAa;IACjD,OAAO,IAAI,0BAAgB,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,CAAC;AAC7C,CAAC;AAEM,KAAK,UAAU,gBAAgB,CAAC,KAAc;IACnD,yBAAyB;IACzB,MAAM,SAAS,GAAG,cAAc,CAA0B,GAAG,CAAC,IAAI,CAAC,MAAM,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;IAEtG,cAAc,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IAE/B,OAAO,CAAC,GAAG,CAAC,SAAS,GAAG,SAAS,CAAC,mBAAmB,EAAE,CAAC;IAExD,OAAO;QACL,SAAS;QACT,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS;KAC3B,CAAC;AACJ,CAAC;AAZD,4CAYC;AAEM,KAAK,UAAU,eAAe;IACnC,MAAM,SAAS,GAAG,cAAc,CAA0B,GAAG,CAAC,CAAC;IAE/D,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAChD,IAAI,SAAS,EAAE;QACb,MAAM,SAAS,CAAC,IAAI,EAAE,CAAC;KACxB;AACH,CAAC;AAPD,0CAOC;AAED,SAAgB,WAAW;IACzB,OAAO,OAAO,CAAC,GAAG,CAAC,SAAmB,CAAC;AACzC,CAAC;AAFD,kCAEC;AAED,SAAgB,yBAAyB,CACvC,EAAU,EACV,OAAuB,EAAE;IAMzB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;IAC/C,MAAM,EAAC,MAAM,GAAG,SAAS,EAAE,GAAG,SAAS,EAAC,GAAG,IAAI,CAAC;IAChD,MAAM,GAAG,GAAG,GAAG,WAAW,EAAE,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;IAEjD,OAAO;QACL,EAAE;QACF,GAAG;QACH,iBAAiB,EAAE;YACjB,gBAAgB,EAAE,IAAI;YACtB,GAAG,SAAS;SACY;KAC3B,CAAC;AACJ,CAAC;AApBD,8DAoBC;AAED,SAAgB,0BAA0B,CAAC,OAA0B,EAAE;IACrE,OAAO,CAAC,yBAAyB,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;AACtD,CAAC;AAFD,gEAEC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TestContainersMongo = void 0;
|
|
4
|
+
const common_1 = require("@tsed/common");
|
|
5
|
+
const mongoose_1 = require("@tsed/mongoose");
|
|
6
|
+
const ContainerUtils_js_1 = require("./ContainerUtils.js");
|
|
7
|
+
class TestContainersMongo {
|
|
8
|
+
static startMongoServer = ContainerUtils_js_1.startMongoServer;
|
|
9
|
+
static stopMongoServer = ContainerUtils_js_1.stopMongoServer;
|
|
10
|
+
static getMongoConnectionsOptions = ContainerUtils_js_1.getMongoConnectionsOptions;
|
|
11
|
+
static getMongoConnectionOptions = ContainerUtils_js_1.getMongoConnectionOptions;
|
|
12
|
+
static create(options = {}) {
|
|
13
|
+
return common_1.PlatformTest.create({
|
|
14
|
+
...options,
|
|
15
|
+
mongoose: (0, ContainerUtils_js_1.getMongoConnectionsOptions)()
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
static bootstrap(mod, opts = {}) {
|
|
19
|
+
return () => common_1.PlatformTest.bootstrap(mod, {
|
|
20
|
+
mongoose: (0, ContainerUtils_js_1.getMongoConnectionsOptions)(),
|
|
21
|
+
...opts
|
|
22
|
+
})();
|
|
23
|
+
}
|
|
24
|
+
static async reset(collectionName) {
|
|
25
|
+
if (typeof collectionName === "string") {
|
|
26
|
+
await TestContainersMongo.cleanCollection(collectionName);
|
|
27
|
+
}
|
|
28
|
+
return common_1.PlatformTest.reset();
|
|
29
|
+
}
|
|
30
|
+
static async cleanCollection(collectionName) {
|
|
31
|
+
const service = common_1.PlatformTest.injector.get(mongoose_1.MongooseService);
|
|
32
|
+
const { collections } = service.get("default");
|
|
33
|
+
if (!collections[collectionName]) {
|
|
34
|
+
console.error(`Collection ${collectionName} not found. Here available collection names: ${Object.keys(collections)}`);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
await collections[collectionName].deleteMany({});
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.TestContainersMongo = TestContainersMongo;
|
|
41
|
+
//# sourceMappingURL=TestContainersMongo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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AgD,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;YACtH,OAAO;SACR;QAED,MAAM,WAAW,CAAC,cAAc,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACnD,CAAC;;AAxCH,kDAyCC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.teardown = exports.setup = void 0;
|
|
4
|
+
const ContainerUtils_js_1 = require("../services/ContainerUtils.js");
|
|
5
|
+
async function setup() {
|
|
6
|
+
await (0, ContainerUtils_js_1.startMongoServer)();
|
|
7
|
+
}
|
|
8
|
+
exports.setup = setup;
|
|
9
|
+
async function teardown() {
|
|
10
|
+
await (0, ContainerUtils_js_1.stopMongoServer)();
|
|
11
|
+
}
|
|
12
|
+
exports.teardown = teardown;
|
|
13
|
+
//# sourceMappingURL=vi.setup.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vi.setup.js","sourceRoot":"","sources":["../../../src/setup/vi.setup.ts"],"names":[],"mappings":";;;AAAA,qEAAgF;AAEzE,KAAK,UAAU,KAAK;IACzB,MAAM,IAAA,oCAAgB,GAAE,CAAC;AAC3B,CAAC;AAFD,sBAEC;AAEM,KAAK,UAAU,QAAQ;IAC5B,MAAM,IAAA,mCAAe,GAAE,CAAC;AAC1B,CAAC;AAFD,4BAEC"}
|
package/lib/esm/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,mCAAmC,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { MongoDBContainer } from "@testcontainers/mongodb";
|
|
2
|
+
const KEY = "TEST_CONTAINER_MONGO";
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
4
|
+
// @ts-ignore
|
|
5
|
+
global[KEY] = null;
|
|
6
|
+
function getEnvironment(key) {
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
return global[key];
|
|
10
|
+
}
|
|
11
|
+
function setEnvironment(key, environment) {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
global[key] = environment;
|
|
15
|
+
}
|
|
16
|
+
function createMongoContainer(image = "mongo:6.0.1") {
|
|
17
|
+
return new MongoDBContainer(image).start();
|
|
18
|
+
}
|
|
19
|
+
export async function startMongoServer(image) {
|
|
20
|
+
// await stopMockServer()
|
|
21
|
+
const container = getEnvironment(KEY) || (await createMongoContainer(image));
|
|
22
|
+
setEnvironment(KEY, container);
|
|
23
|
+
process.env.MONGO_URL = container.getConnectionString();
|
|
24
|
+
return {
|
|
25
|
+
container,
|
|
26
|
+
url: process.env.MONGO_URL
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
export async function stopMongoServer() {
|
|
30
|
+
const container = getEnvironment(KEY);
|
|
31
|
+
process.stdout.write("Stopping mongo server\n");
|
|
32
|
+
if (container) {
|
|
33
|
+
await container.stop();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export function getMongoUrl() {
|
|
37
|
+
return process.env.MONGO_URL;
|
|
38
|
+
}
|
|
39
|
+
export function getMongoConnectionOptions(id, opts = {}) {
|
|
40
|
+
const seed = Math.floor(Math.random() * 10000);
|
|
41
|
+
const { dbName = "db-test", ...otherOpts } = opts;
|
|
42
|
+
const url = `${getMongoUrl()}/${dbName}-${seed}`;
|
|
43
|
+
return {
|
|
44
|
+
id,
|
|
45
|
+
url,
|
|
46
|
+
connectionOptions: {
|
|
47
|
+
directConnection: true,
|
|
48
|
+
...otherOpts
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export function getMongoConnectionsOptions(opts = {}) {
|
|
53
|
+
return [getMongoConnectionOptions("default", opts)];
|
|
54
|
+
}
|
|
55
|
+
//# sourceMappingURL=ContainerUtils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ContainerUtils.js","sourceRoot":"","sources":["../../../src/services/ContainerUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,gBAAgB,EAA0B,MAAM,yBAAyB,CAAC;AAalF,MAAM,GAAG,GAAG,sBAAsB,CAAC;AAEnC,6DAA6D;AAC7D,aAAa;AACb,MAAM,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;AAEnB,SAAS,cAAc,CAAI,GAAW;IACpC,6DAA6D;IAC7D,aAAa;IACb,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC;AACrB,CAAC;AAED,SAAS,cAAc,CAAC,GAAW,EAAE,WAAoB;IACvD,6DAA6D;IAC7D,aAAa;IACb,MAAM,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC;AAC5B,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAK,GAAG,aAAa;IACjD,OAAO,IAAI,gBAAgB,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,CAAC;AAC7C,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,KAAc;IACnD,yBAAyB;IACzB,MAAM,SAAS,GAAG,cAAc,CAA0B,GAAG,CAAC,IAAI,CAAC,MAAM,oBAAoB,CAAC,KAAK,CAAC,CAAC,CAAC;IAEtG,cAAc,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IAE/B,OAAO,CAAC,GAAG,CAAC,SAAS,GAAG,SAAS,CAAC,mBAAmB,EAAE,CAAC;IAExD,OAAO;QACL,SAAS;QACT,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,SAAS;KAC3B,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe;IACnC,MAAM,SAAS,GAAG,cAAc,CAA0B,GAAG,CAAC,CAAC;IAE/D,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,yBAAyB,CAAC,CAAC;IAChD,IAAI,SAAS,EAAE;QACb,MAAM,SAAS,CAAC,IAAI,EAAE,CAAC;KACxB;AACH,CAAC;AAED,MAAM,UAAU,WAAW;IACzB,OAAO,OAAO,CAAC,GAAG,CAAC,SAAmB,CAAC;AACzC,CAAC;AAED,MAAM,UAAU,yBAAyB,CACvC,EAAU,EACV,OAAuB,EAAE;IAMzB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAC;IAC/C,MAAM,EAAC,MAAM,GAAG,SAAS,EAAE,GAAG,SAAS,EAAC,GAAG,IAAI,CAAC;IAChD,MAAM,GAAG,GAAG,GAAG,WAAW,EAAE,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;IAEjD,OAAO;QACL,EAAE;QACF,GAAG;QACH,iBAAiB,EAAE;YACjB,gBAAgB,EAAE,IAAI;YACtB,GAAG,SAAS;SACY;KAC3B,CAAC;AACJ,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,OAA0B,EAAE;IACrE,OAAO,CAAC,yBAAyB,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC,CAAC;AACtD,CAAC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { PlatformTest } from "@tsed/common";
|
|
2
|
+
import { MongooseService } from "@tsed/mongoose";
|
|
3
|
+
import { getMongoConnectionOptions, getMongoConnectionsOptions, startMongoServer, stopMongoServer } from "./ContainerUtils.js";
|
|
4
|
+
export class TestContainersMongo {
|
|
5
|
+
static startMongoServer = startMongoServer;
|
|
6
|
+
static stopMongoServer = stopMongoServer;
|
|
7
|
+
static getMongoConnectionsOptions = getMongoConnectionsOptions;
|
|
8
|
+
static getMongoConnectionOptions = getMongoConnectionOptions;
|
|
9
|
+
static create(options = {}) {
|
|
10
|
+
return PlatformTest.create({
|
|
11
|
+
...options,
|
|
12
|
+
mongoose: getMongoConnectionsOptions()
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
static bootstrap(mod, opts = {}) {
|
|
16
|
+
return () => PlatformTest.bootstrap(mod, {
|
|
17
|
+
mongoose: getMongoConnectionsOptions(),
|
|
18
|
+
...opts
|
|
19
|
+
})();
|
|
20
|
+
}
|
|
21
|
+
static async reset(collectionName) {
|
|
22
|
+
if (typeof collectionName === "string") {
|
|
23
|
+
await TestContainersMongo.cleanCollection(collectionName);
|
|
24
|
+
}
|
|
25
|
+
return PlatformTest.reset();
|
|
26
|
+
}
|
|
27
|
+
static async cleanCollection(collectionName) {
|
|
28
|
+
const service = PlatformTest.injector.get(MongooseService);
|
|
29
|
+
const { collections } = service.get("default");
|
|
30
|
+
if (!collections[collectionName]) {
|
|
31
|
+
console.error(`Collection ${collectionName} not found. Here available collection names: ${Object.keys(collections)}`);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
await collections[collectionName].deleteMany({});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=TestContainersMongo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TestContainersMongo.js","sourceRoot":"","sources":["../../../src/services/TestContainersMongo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,YAAY,EAAC,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAC,eAAe,EAAC,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAC,yBAAyB,EAAE,0BAA0B,EAAE,gBAAgB,EAAE,eAAe,EAAC,MAAM,qBAAqB,CAAC;AAE7H,MAAM,OAAO,mBAAmB;IAC9B,MAAM,CAAC,gBAAgB,GAAG,gBAAgB,CAAC;IAC3C,MAAM,CAAC,eAAe,GAAG,eAAe,CAAC;IACzC,MAAM,CAAC,0BAA0B,GAAG,0BAA0B,CAAC;IAC/D,MAAM,CAAC,yBAAyB,GAAG,yBAAyB,CAAC;IAE7D,MAAM,CAAC,MAAM,CAAC,UAAuC,EAAE;QACrD,OAAO,YAAY,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;AAED,MAAM,CAAC,KAAK,UAAU,QAAQ;IAC5B,MAAM,eAAe,EAAE,CAAC;AAC1B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./services/TestContainersMongo.js";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { StartedMongoDBContainer } from "@testcontainers/mongodb";
|
|
3
|
+
import type { ConnectOptions } from "mongoose";
|
|
4
|
+
import type { ConnectionOptions } from "node:tls";
|
|
5
|
+
declare global {
|
|
6
|
+
namespace NodeJS {
|
|
7
|
+
interface Global {
|
|
8
|
+
TEST_CONTAINER_MONGO: StartedMongoDBContainer | null;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export declare function startMongoServer(image?: string): Promise<{
|
|
13
|
+
container: StartedMongoDBContainer;
|
|
14
|
+
url: string;
|
|
15
|
+
}>;
|
|
16
|
+
export declare function stopMongoServer(): Promise<void>;
|
|
17
|
+
export declare function getMongoUrl(): string;
|
|
18
|
+
export declare function getMongoConnectionOptions(id: string, opts?: ConnectOptions): {
|
|
19
|
+
id: string;
|
|
20
|
+
url: string;
|
|
21
|
+
connectionOptions: ConnectOptions;
|
|
22
|
+
};
|
|
23
|
+
export declare function getMongoConnectionsOptions(opts?: ConnectionOptions): {
|
|
24
|
+
id: string;
|
|
25
|
+
url: string;
|
|
26
|
+
connectionOptions: ConnectOptions;
|
|
27
|
+
}[];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getMongoConnectionOptions, getMongoConnectionsOptions, startMongoServer, stopMongoServer } from "./ContainerUtils.js";
|
|
2
|
+
export declare class TestContainersMongo {
|
|
3
|
+
static startMongoServer: typeof startMongoServer;
|
|
4
|
+
static stopMongoServer: typeof stopMongoServer;
|
|
5
|
+
static getMongoConnectionsOptions: typeof getMongoConnectionsOptions;
|
|
6
|
+
static getMongoConnectionOptions: typeof getMongoConnectionOptions;
|
|
7
|
+
static create(options?: Partial<TsED.Configuration>): Promise<void>;
|
|
8
|
+
static bootstrap(mod: unknown, opts?: Partial<TsED.Configuration>): () => Promise<void>;
|
|
9
|
+
static reset(collectionName?: string): Promise<void>;
|
|
10
|
+
static cleanCollection(collectionName: string): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./services/TestContainersMongo.js";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { StartedMongoDBContainer } from "@testcontainers/mongodb";
|
|
3
|
+
import type { ConnectOptions } from "mongoose";
|
|
4
|
+
import type { ConnectionOptions } from "node:tls";
|
|
5
|
+
declare global {
|
|
6
|
+
namespace NodeJS {
|
|
7
|
+
interface Global {
|
|
8
|
+
TEST_CONTAINER_MONGO: StartedMongoDBContainer | null;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export declare function startMongoServer(image?: string): Promise<{
|
|
13
|
+
container: StartedMongoDBContainer;
|
|
14
|
+
url: string;
|
|
15
|
+
}>;
|
|
16
|
+
export declare function stopMongoServer(): Promise<void>;
|
|
17
|
+
export declare function getMongoUrl(): string;
|
|
18
|
+
export declare function getMongoConnectionOptions(id: string, opts?: ConnectOptions): {
|
|
19
|
+
id: string;
|
|
20
|
+
url: string;
|
|
21
|
+
connectionOptions: ConnectOptions;
|
|
22
|
+
};
|
|
23
|
+
export declare function getMongoConnectionsOptions(opts?: ConnectionOptions): {
|
|
24
|
+
id: string;
|
|
25
|
+
url: string;
|
|
26
|
+
connectionOptions: ConnectOptions;
|
|
27
|
+
}[];
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getMongoConnectionOptions, getMongoConnectionsOptions, startMongoServer, stopMongoServer } from "./ContainerUtils.js";
|
|
2
|
+
export declare class TestContainersMongo {
|
|
3
|
+
static startMongoServer: typeof startMongoServer;
|
|
4
|
+
static stopMongoServer: typeof stopMongoServer;
|
|
5
|
+
static getMongoConnectionsOptions: typeof getMongoConnectionsOptions;
|
|
6
|
+
static getMongoConnectionOptions: typeof getMongoConnectionOptions;
|
|
7
|
+
static create(options?: Partial<TsED.Configuration>): Promise<void>;
|
|
8
|
+
static bootstrap(mod: unknown, opts?: Partial<TsED.Configuration>): () => Promise<void>;
|
|
9
|
+
static reset(collectionName?: string): Promise<void>;
|
|
10
|
+
static cleanCollection(collectionName: string): Promise<void>;
|
|
11
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tsed/testcontainers-mongo",
|
|
3
|
+
"description": "A module to test Ts.ED code that use MongoDB as database using Test Containers",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"version": "7.80.0",
|
|
6
|
+
"source": "./src/index.ts",
|
|
7
|
+
"main": "./lib/cjs/index.js",
|
|
8
|
+
"module": "./lib/esm/index.js",
|
|
9
|
+
"typings": "./lib/types/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./lib/types/index.d.ts",
|
|
13
|
+
"import": "./lib/esm/index.js",
|
|
14
|
+
"default": "./lib/cjs/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./vitest/setup": {
|
|
17
|
+
"types": "./lib/types/setup/vi.setup.d.ts",
|
|
18
|
+
"import": "./lib/esm/setup/vi.setup.js",
|
|
19
|
+
"default": "./lib/cjs/setup/vi.setup.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "yarn build:ts",
|
|
24
|
+
"build:ts": "tsc --build tsconfig.json"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"semver": "^7.6.0",
|
|
28
|
+
"tslib": "2.6.1"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@testcontainers/mongodb": ">=10.11.0",
|
|
32
|
+
"@tsed/barrels": "7.80.0",
|
|
33
|
+
"@tsed/common": "7.80.0",
|
|
34
|
+
"@tsed/core": "7.80.0",
|
|
35
|
+
"@tsed/di": "7.80.0",
|
|
36
|
+
"@tsed/mongoose": "7.80.0",
|
|
37
|
+
"@tsed/typescript": "7.80.0",
|
|
38
|
+
"eslint": "^8.57.0",
|
|
39
|
+
"testcontainers": ">=10.11.0",
|
|
40
|
+
"typescript": "4.9.5"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@testcontainers/mongodb": ">=10.11.0",
|
|
44
|
+
"@tsed/common": "workspace:*",
|
|
45
|
+
"@tsed/core": "7.80.0",
|
|
46
|
+
"@tsed/di": "workspace:*",
|
|
47
|
+
"@tsed/mongoose": "workspace:*",
|
|
48
|
+
"testcontainers": ">=10.11.0"
|
|
49
|
+
},
|
|
50
|
+
"repository": "https://github.com/tsedio/tsed",
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/tsedio/tsed/issues"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/orm/testcontainers-mongo",
|
|
55
|
+
"author": "Romain Lenzotti",
|
|
56
|
+
"license": "MIT"
|
|
57
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
<p style="text-align: center" align="center">
|
|
2
|
+
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<div align="center">
|
|
6
|
+
<h1>Mongoose testing</h1>
|
|
7
|
+
|
|
8
|
+
[](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
|
|
9
|
+
[](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
|
|
10
|
+
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
11
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
12
|
+
[](https://github.com/prettier/prettier)
|
|
13
|
+
[](https://github.com/sponsors/romakita)
|
|
14
|
+
[](https://opencollective.com/tsed)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
<div align="center">
|
|
19
|
+
<a href="https://tsed.io/">Website</a>
|
|
20
|
+
<span> • </span>
|
|
21
|
+
<a href="https://tsed.io/getting-started/">Getting started</a>
|
|
22
|
+
<span> • </span>
|
|
23
|
+
<a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
|
|
24
|
+
<span> • </span>
|
|
25
|
+
<a href="https://twitter.com/TsED_io">Twitter</a>
|
|
26
|
+
</div>
|
|
27
|
+
|
|
28
|
+
<hr />
|
|
29
|
+
|
|
30
|
+
A package of Ts.ED framework. See website: https://tsed.io/
|
|
31
|
+
|
|
32
|
+
This package is an helper to create unit test base on `@tsed/mongoose` package.
|
|
33
|
+
|
|
34
|
+
# Installation
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
npm install --save @tsed/testing-mongoose
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Example usage
|
|
41
|
+
|
|
42
|
+
### Testing server
|
|
43
|
+
|
|
44
|
+
Here an example to your server with a mocked database:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import {PlatformTest} from "@tsed/common";
|
|
48
|
+
import {PlatformExpress} from "@tsed/platform-express";
|
|
49
|
+
import {TestMongooseContext} from "@tsed/testing-mongoose";
|
|
50
|
+
import {expect} from "chai";
|
|
51
|
+
import * as SuperTest from "supertest";
|
|
52
|
+
import {Server} from "../Server.js";
|
|
53
|
+
|
|
54
|
+
describe("Rest", () => {
|
|
55
|
+
// bootstrap your Server to load all endpoints before run your test
|
|
56
|
+
let request: SuperTest.Agent;
|
|
57
|
+
|
|
58
|
+
before(TestMongooseContext.bootstrap(Server, {platform: PlatformExpress})); // Create a server with mocked database
|
|
59
|
+
before((done) => {
|
|
60
|
+
request = SuperTest(PlatformTest.callback());
|
|
61
|
+
done();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
after(TestMongooseContext.reset); // reset database and injector
|
|
65
|
+
|
|
66
|
+
describe("GET /rest/calendars", () => {
|
|
67
|
+
it("should do something", async () => {
|
|
68
|
+
const response = await request.get("/rest/calendars").expect(200);
|
|
69
|
+
|
|
70
|
+
expect(response.body).to.be.an("array");
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
::: tip
|
|
77
|
+
To increase mocha timeout from 2000ms to 10000ms use option `--timeout 10000`.
|
|
78
|
+
:::
|
|
79
|
+
|
|
80
|
+
### Testing server with Replica set
|
|
81
|
+
|
|
82
|
+
A [ReplicaSet](https://github.com/nodkz/mongodb-memory-server#replica-set-start) can be easily started with:
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import {PlatformTest} from "@tsed/common";
|
|
86
|
+
import {PlatformExpress} from "@tsed/platform-express";
|
|
87
|
+
import {TestMongooseContext} from "@tsed/testing-mongoose";
|
|
88
|
+
import {expect} from "chai";
|
|
89
|
+
import * as SuperTest from "supertest";
|
|
90
|
+
import {Server} from "../Server.js";
|
|
91
|
+
|
|
92
|
+
describe("Rest", () => {
|
|
93
|
+
// bootstrap your Server to load all endpoints before run your test
|
|
94
|
+
let request: SuperTest.Agent;
|
|
95
|
+
|
|
96
|
+
before(
|
|
97
|
+
TestMongooseContext.bootstrap(Server, {
|
|
98
|
+
platform: PlatformExpress,
|
|
99
|
+
mongod: {
|
|
100
|
+
replicaSet: true
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
); // Create a server with mocked database
|
|
104
|
+
before((done) => {
|
|
105
|
+
request = SuperTest(PlatformTest.callback());
|
|
106
|
+
done();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
after(TestMongooseContext.reset); // reset database and injector
|
|
110
|
+
|
|
111
|
+
describe("GET /rest/calendars", () => {
|
|
112
|
+
it("should do something", async () => {
|
|
113
|
+
const response = await request.get("/rest/calendars").expect(200);
|
|
114
|
+
|
|
115
|
+
expect(response.body).to.be.an("array");
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Jest additional setup
|
|
122
|
+
|
|
123
|
+
Add a script to close connection after all unit test. In your jest configuration file add the following line:
|
|
124
|
+
|
|
125
|
+
```json
|
|
126
|
+
{
|
|
127
|
+
"globalTeardown": "./scripts/jest/teardown.js"
|
|
128
|
+
}
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
And create the script with the following content:
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
module.exports = async () => {
|
|
135
|
+
await global.__MONGOD__.stop();
|
|
136
|
+
};
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Contributors
|
|
140
|
+
|
|
141
|
+
Please read [contributing guidelines here](https://tsed.io/contributing.html)
|
|
142
|
+
|
|
143
|
+
<a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
|
|
144
|
+
|
|
145
|
+
## Backers
|
|
146
|
+
|
|
147
|
+
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]
|
|
148
|
+
|
|
149
|
+
<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/tiers/backer.svg?width=890"></a>
|
|
150
|
+
|
|
151
|
+
## Sponsors
|
|
152
|
+
|
|
153
|
+
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]
|
|
154
|
+
|
|
155
|
+
## License
|
|
156
|
+
|
|
157
|
+
The MIT License (MIT)
|
|
158
|
+
|
|
159
|
+
Copyright (c) 2016 - 2022 Romain Lenzotti
|
|
160
|
+
|
|
161
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
162
|
+
|
|
163
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
164
|
+
|
|
165
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|