@tsed/testcontainers-mongo 8.0.1 → 8.0.2
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/package.json
CHANGED
|
@@ -2,13 +2,14 @@
|
|
|
2
2
|
"name": "@tsed/testcontainers-mongo",
|
|
3
3
|
"description": "A module to test Ts.ED code that use MongoDB as database using Test Containers",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"version": "8.0.
|
|
5
|
+
"version": "8.0.2",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
7
|
"main": "./lib/esm/index.js",
|
|
8
8
|
"module": "./lib/esm/index.js",
|
|
9
9
|
"typings": "./lib/types/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
12
|
+
"@tsed/source": "./src/index.ts",
|
|
12
13
|
"types": "./lib/types/index.d.ts",
|
|
13
14
|
"import": "./lib/esm/index.js",
|
|
14
15
|
"default": "./lib/esm/index.js"
|
|
@@ -29,11 +30,11 @@
|
|
|
29
30
|
},
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@testcontainers/mongodb": "^10.13.2",
|
|
32
|
-
"@tsed/barrels": "8.0.
|
|
33
|
-
"@tsed/core": "8.0.
|
|
34
|
-
"@tsed/di": "8.0.
|
|
35
|
-
"@tsed/platform-http": "8.0.
|
|
36
|
-
"@tsed/typescript": "8.0.
|
|
33
|
+
"@tsed/barrels": "8.0.2",
|
|
34
|
+
"@tsed/core": "8.0.2",
|
|
35
|
+
"@tsed/di": "8.0.2",
|
|
36
|
+
"@tsed/platform-http": "8.0.2",
|
|
37
|
+
"@tsed/typescript": "8.0.2",
|
|
37
38
|
"eslint": "9.12.0",
|
|
38
39
|
"mongodb": "6.9.0",
|
|
39
40
|
"testcontainers": ">=10.11.0",
|
|
@@ -41,9 +42,9 @@
|
|
|
41
42
|
},
|
|
42
43
|
"peerDependencies": {
|
|
43
44
|
"@testcontainers/mongodb": ">=10.11.0",
|
|
44
|
-
"@tsed/core": "8.0.
|
|
45
|
-
"@tsed/di": "8.0.
|
|
46
|
-
"@tsed/platform-http": "8.0.
|
|
45
|
+
"@tsed/core": "8.0.2",
|
|
46
|
+
"@tsed/di": "8.0.2",
|
|
47
|
+
"@tsed/platform-http": "8.0.2",
|
|
47
48
|
"mongodb": ">=6",
|
|
48
49
|
"testcontainers": ">=10.11.0"
|
|
49
50
|
},
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./services/TestContainersMongo.js";
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type {ConnectionOptions} from "node:tls";
|
|
2
|
+
|
|
3
|
+
import {MongoDBContainer, StartedMongoDBContainer} from "@testcontainers/mongodb";
|
|
4
|
+
import type {ConnectOptions} from "mongoose";
|
|
5
|
+
|
|
6
|
+
declare global {
|
|
7
|
+
namespace NodeJS {
|
|
8
|
+
interface Global {
|
|
9
|
+
TEST_CONTAINER_MONGO: StartedMongoDBContainer | null;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const KEY = "TEST_CONTAINER_MONGO";
|
|
15
|
+
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
global[KEY] = null;
|
|
18
|
+
|
|
19
|
+
function getEnvironment<T>(key: string): T | null {
|
|
20
|
+
// @ts-ignore
|
|
21
|
+
return global[key];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function setEnvironment(key: string, environment: unknown) {
|
|
25
|
+
// @ts-ignore
|
|
26
|
+
global[key] = environment;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function createMongoContainer(image = "mongo:6.0.1") {
|
|
30
|
+
return new MongoDBContainer(image).start();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export async function startMongoServer(image?: string) {
|
|
34
|
+
// await stopMockServer()
|
|
35
|
+
const container = getEnvironment<StartedMongoDBContainer>(KEY) || (await createMongoContainer(image));
|
|
36
|
+
|
|
37
|
+
setEnvironment(KEY, container);
|
|
38
|
+
|
|
39
|
+
process.env.MONGO_URL = container.getConnectionString();
|
|
40
|
+
|
|
41
|
+
return {
|
|
42
|
+
container,
|
|
43
|
+
url: process.env.MONGO_URL
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function stopMongoServer() {
|
|
48
|
+
const container = getEnvironment<StartedMongoDBContainer>(KEY);
|
|
49
|
+
|
|
50
|
+
process.stdout.write("Stopping mongo server\n");
|
|
51
|
+
if (container) {
|
|
52
|
+
await container.stop();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function getMongoUrl(): string {
|
|
57
|
+
return process.env.MONGO_URL as string;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function getMongoConnectionOptions(
|
|
61
|
+
id: string = "",
|
|
62
|
+
opts: ConnectOptions = {}
|
|
63
|
+
): {
|
|
64
|
+
id: string;
|
|
65
|
+
url: string;
|
|
66
|
+
connectionOptions: ConnectOptions;
|
|
67
|
+
} {
|
|
68
|
+
const seed = Math.floor(Math.random() * 10000);
|
|
69
|
+
const {dbName = "db-test", ...otherOpts} = opts;
|
|
70
|
+
const url = `${getMongoUrl()}/${dbName}-${seed}`;
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
id,
|
|
74
|
+
url,
|
|
75
|
+
connectionOptions: {
|
|
76
|
+
directConnection: true,
|
|
77
|
+
...otherOpts
|
|
78
|
+
} satisfies ConnectOptions
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function getMongoConnectionsOptions(opts: ConnectionOptions = {}) {
|
|
83
|
+
return [getMongoConnectionOptions("default", opts)];
|
|
84
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
2
|
+
import {MongoClient} from "mongodb";
|
|
3
|
+
|
|
4
|
+
import {getMongoConnectionOptions, getMongoConnectionsOptions, startMongoServer, stopMongoServer} from "./ContainerUtils.js";
|
|
5
|
+
|
|
6
|
+
export class TestContainersMongo {
|
|
7
|
+
static startMongoServer = startMongoServer;
|
|
8
|
+
static stopMongoServer = stopMongoServer;
|
|
9
|
+
static getMongoConnectionsOptions = getMongoConnectionsOptions;
|
|
10
|
+
static getMongoConnectionOptions = getMongoConnectionOptions;
|
|
11
|
+
|
|
12
|
+
private static configuration: ReturnType<typeof getMongoConnectionsOptions>;
|
|
13
|
+
|
|
14
|
+
static create(options: Partial<TsED.Configuration> = {}) {
|
|
15
|
+
TestContainersMongo.configuration = getMongoConnectionsOptions();
|
|
16
|
+
|
|
17
|
+
return PlatformTest.create({
|
|
18
|
+
...options,
|
|
19
|
+
mongoose: TestContainersMongo.configuration
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static bootstrap(mod: unknown, opts: Partial<TsED.Configuration> = {}) {
|
|
24
|
+
return () => {
|
|
25
|
+
TestContainersMongo.configuration = getMongoConnectionsOptions();
|
|
26
|
+
return PlatformTest.bootstrap(mod, {
|
|
27
|
+
mongoose: TestContainersMongo.configuration,
|
|
28
|
+
...opts
|
|
29
|
+
})();
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static async reset(collectionName?: string) {
|
|
34
|
+
if (typeof collectionName === "string") {
|
|
35
|
+
await TestContainersMongo.cleanCollection(collectionName);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return PlatformTest.reset();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
static async cleanCollection(collectionName: string, mongoSettings = TestContainersMongo.configuration) {
|
|
42
|
+
try {
|
|
43
|
+
const client = new MongoClient(mongoSettings[0].url, {
|
|
44
|
+
directConnection: true
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
await client.connect();
|
|
48
|
+
const db = client.db();
|
|
49
|
+
await db.collection(collectionName).deleteMany({});
|
|
50
|
+
await client.close();
|
|
51
|
+
} catch (er) {
|
|
52
|
+
console.error(`Collection ${collectionName} not found. ${er.message} ${er.stack}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|