@tsed/testcontainers-mongo 8.29.0 → 8.30.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.
@@ -0,0 +1 @@
1
+ export * from "./services/TestContainersMongo.js";
@@ -0,0 +1,51 @@
1
+ import { MongoDBContainer } from "@testcontainers/mongodb";
2
+ const KEY = "TEST_CONTAINER_MONGO";
3
+ // @ts-ignore
4
+ global[KEY] = null;
5
+ function getEnvironment(key) {
6
+ // @ts-ignore
7
+ return global[key];
8
+ }
9
+ function setEnvironment(key, environment) {
10
+ // @ts-ignore
11
+ global[key] = environment;
12
+ }
13
+ function createMongoContainer(image = "mongo:6.0.1") {
14
+ return new MongoDBContainer(image).start();
15
+ }
16
+ export async function startMongoServer(image) {
17
+ // await stopMockServer()
18
+ const container = getEnvironment(KEY) || (await createMongoContainer(image));
19
+ setEnvironment(KEY, container);
20
+ process.env.MONGO_URL = container.getConnectionString();
21
+ return {
22
+ container,
23
+ url: process.env.MONGO_URL
24
+ };
25
+ }
26
+ export async function stopMongoServer() {
27
+ const container = getEnvironment(KEY);
28
+ process.stdout.write("Stopping mongo server\n");
29
+ if (container) {
30
+ await container.stop();
31
+ }
32
+ }
33
+ export function getMongoUrl() {
34
+ return process.env.MONGO_URL;
35
+ }
36
+ export function getMongoConnectionOptions(id = "", opts = {}) {
37
+ const seed = Math.floor(Math.random() * 10000);
38
+ const { dbName = "db-test", ...otherOpts } = opts;
39
+ const url = `${getMongoUrl()}/${dbName}-${seed}`;
40
+ return {
41
+ id,
42
+ url,
43
+ connectionOptions: {
44
+ directConnection: true,
45
+ ...otherOpts
46
+ }
47
+ };
48
+ }
49
+ export function getMongoConnectionsOptions(opts = {}) {
50
+ return [getMongoConnectionOptions("default", opts)];
51
+ }
@@ -0,0 +1,46 @@
1
+ import { PlatformTest } from "@tsed/platform-http/testing";
2
+ import { MongoClient } from "mongodb";
3
+ import { getMongoConnectionOptions, getMongoConnectionsOptions, startMongoServer, stopMongoServer } from "./ContainerUtils.js";
4
+ export class TestContainersMongo {
5
+ static { this.startMongoServer = startMongoServer; }
6
+ static { this.stopMongoServer = stopMongoServer; }
7
+ static { this.getMongoConnectionsOptions = getMongoConnectionsOptions; }
8
+ static { this.getMongoConnectionOptions = getMongoConnectionOptions; }
9
+ static create(options = {}) {
10
+ TestContainersMongo.configuration = getMongoConnectionsOptions();
11
+ return PlatformTest.create({
12
+ ...options,
13
+ mongoose: TestContainersMongo.configuration
14
+ });
15
+ }
16
+ static bootstrap(mod, opts = {}) {
17
+ return () => {
18
+ TestContainersMongo.configuration = getMongoConnectionsOptions();
19
+ return PlatformTest.bootstrap(mod, {
20
+ mongoose: TestContainersMongo.configuration,
21
+ ...opts
22
+ })();
23
+ };
24
+ }
25
+ static async reset(collectionName) {
26
+ if (typeof collectionName === "string") {
27
+ await TestContainersMongo.cleanCollection(collectionName);
28
+ }
29
+ return PlatformTest.reset();
30
+ }
31
+ static async cleanCollection(collectionName, mongoSettings = TestContainersMongo.configuration) {
32
+ try {
33
+ const client = new MongoClient(mongoSettings[0].url, {
34
+ directConnection: true
35
+ });
36
+ await client.connect();
37
+ const db = client.db();
38
+ await db.collection(collectionName).deleteMany({});
39
+ await client.close();
40
+ }
41
+ catch (er) {
42
+ const error = er;
43
+ console.error(`Collection ${collectionName} not found. ${error.message} ${error.stack}`);
44
+ }
45
+ }
46
+ }
@@ -0,0 +1,7 @@
1
+ import { startMongoServer, stopMongoServer } from "../services/ContainerUtils.js";
2
+ export async function setup() {
3
+ await startMongoServer();
4
+ }
5
+ export async function teardown() {
6
+ await stopMongoServer();
7
+ }
@@ -0,0 +1 @@
1
+ export * from "./services/TestContainersMongo.js";
@@ -0,0 +1,26 @@
1
+ import type { ConnectionOptions } from "node:tls";
2
+ import { StartedMongoDBContainer } from "@testcontainers/mongodb";
3
+ import type { ConnectOptions } from "mongoose";
4
+ declare global {
5
+ namespace NodeJS {
6
+ interface Global {
7
+ TEST_CONTAINER_MONGO: StartedMongoDBContainer | null;
8
+ }
9
+ }
10
+ }
11
+ export declare function startMongoServer(image?: string): Promise<{
12
+ container: StartedMongoDBContainer;
13
+ url: string;
14
+ }>;
15
+ export declare function stopMongoServer(): Promise<void>;
16
+ export declare function getMongoUrl(): string;
17
+ export declare function getMongoConnectionOptions(id?: string, opts?: ConnectOptions): {
18
+ id: string;
19
+ url: string;
20
+ connectionOptions: ConnectOptions;
21
+ };
22
+ export declare function getMongoConnectionsOptions(opts?: ConnectionOptions): {
23
+ id: string;
24
+ url: string;
25
+ connectionOptions: ConnectOptions;
26
+ }[];
@@ -0,0 +1,16 @@
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
+ private static configuration;
8
+ static create(options?: Partial<TsED.Configuration>): Promise<void>;
9
+ static bootstrap(mod: unknown, opts?: Partial<TsED.Configuration>): () => Promise<void>;
10
+ static reset(collectionName?: string): Promise<void>;
11
+ static cleanCollection(collectionName: string, mongoSettings?: {
12
+ id: string;
13
+ url: string;
14
+ connectionOptions: import("mongoose").ConnectOptions;
15
+ }[]): Promise<void>;
16
+ }
@@ -0,0 +1,2 @@
1
+ export declare function setup(): Promise<void>;
2
+ export declare function teardown(): Promise<void>;
package/package.json CHANGED
@@ -2,7 +2,7 @@
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.29.0",
5
+ "version": "8.30.0",
6
6
  "source": "./src/index.ts",
7
7
  "main": "lib/esm/index.js",
8
8
  "module": "lib/esm/index.js",
@@ -31,11 +31,11 @@
31
31
  },
32
32
  "devDependencies": {
33
33
  "@testcontainers/mongodb": "^10.13.2",
34
- "@tsed/barrels": "8.29.0",
35
- "@tsed/core": "8.29.0",
36
- "@tsed/di": "8.29.0",
37
- "@tsed/platform-http": "8.29.0",
38
- "@tsed/typescript": "8.29.0",
34
+ "@tsed/barrels": "8.30.0",
35
+ "@tsed/core": "8.30.0",
36
+ "@tsed/di": "8.30.0",
37
+ "@tsed/platform-http": "8.30.0",
38
+ "@tsed/typescript": "8.30.0",
39
39
  "eslint": "9.12.0",
40
40
  "mongodb": "6.9.0",
41
41
  "testcontainers": ">=10.11.0",