@tsed/testcontainers-mongo 8.28.7 → 8.29.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/package.json +9 -9
- package/readme.md +10 -11
- package/lib/esm/index.js +0 -1
- package/lib/esm/services/ContainerUtils.js +0 -51
- package/lib/esm/services/TestContainersMongo.js +0 -46
- package/lib/esm/setup/vi.setup.js +0 -7
- package/lib/types/index.d.ts +0 -1
- package/lib/types/services/ContainerUtils.d.ts +0 -26
- package/lib/types/services/TestContainersMongo.d.ts +0 -16
- package/lib/types/setup/vi.setup.d.ts +0 -2
package/package.json
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
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.
|
|
5
|
+
"version": "8.29.0",
|
|
6
6
|
"source": "./src/index.ts",
|
|
7
|
-
"main": "
|
|
8
|
-
"module": "
|
|
7
|
+
"main": "lib/esm/index.js",
|
|
8
|
+
"module": "lib/esm/index.js",
|
|
9
9
|
"typings": "./lib/types/index.d.ts",
|
|
10
10
|
"exports": {
|
|
11
11
|
".": {
|
|
@@ -31,11 +31,11 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@testcontainers/mongodb": "^10.13.2",
|
|
34
|
-
"@tsed/barrels": "8.
|
|
35
|
-
"@tsed/core": "8.
|
|
36
|
-
"@tsed/di": "8.
|
|
37
|
-
"@tsed/platform-http": "8.
|
|
38
|
-
"@tsed/typescript": "8.
|
|
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",
|
|
39
39
|
"eslint": "9.12.0",
|
|
40
40
|
"mongodb": "6.9.0",
|
|
41
41
|
"testcontainers": ">=10.11.0",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"bugs": {
|
|
54
54
|
"url": "https://github.com/tsedio/tsed/issues"
|
|
55
55
|
},
|
|
56
|
-
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/
|
|
56
|
+
"homepage": "https://github.com/tsedio/tsed/tree/production/packages/testcontainers/mongo",
|
|
57
57
|
"author": "Romain Lenzotti",
|
|
58
58
|
"license": "MIT"
|
|
59
59
|
}
|
package/readme.md
CHANGED
|
@@ -36,26 +36,25 @@ This package allows you to test your code using the [TestContainers](https://nod
|
|
|
36
36
|
To use the `@tsed/testcontainers-mongo` package, you need to install the package:
|
|
37
37
|
|
|
38
38
|
```sh [npm]
|
|
39
|
-
npm install --save-dev @tsed/
|
|
39
|
+
npm install --save-dev @tsed/mongo
|
|
40
40
|
```
|
|
41
41
|
|
|
42
42
|
```sh [yarn]
|
|
43
|
-
yarn add --dev @tsed/
|
|
43
|
+
yarn add --dev @tsed/mongo
|
|
44
44
|
```
|
|
45
45
|
|
|
46
46
|
```sh [pnpm]
|
|
47
|
-
pnpm add --dev @tsed/
|
|
47
|
+
pnpm add --dev @tsed/mongo
|
|
48
48
|
```
|
|
49
49
|
|
|
50
50
|
```sh [bun]
|
|
51
|
-
bun add --dev @tsed/
|
|
51
|
+
bun add --dev @tsed/mongo
|
|
52
52
|
```
|
|
53
53
|
|
|
54
54
|
### Important
|
|
55
55
|
|
|
56
56
|
This package requires the `@tsed/platform-http` package to work properly. If you're looking for a standalone package,
|
|
57
|
-
you can use the `@tsedio/testcontainers-mongo` premium package available
|
|
58
|
-
here: https://tsed.dev/plugins/premium/config-source/mongo.html
|
|
57
|
+
you can use the `@tsedio/testcontainers-mongo` premium package available here: https://tsed.dev/plugins/premium/config-source/mongo.html
|
|
59
58
|
|
|
60
59
|
### Configuration
|
|
61
60
|
|
|
@@ -71,13 +70,13 @@ module.exports = {
|
|
|
71
70
|
};
|
|
72
71
|
|
|
73
72
|
// jest.setup.js
|
|
74
|
-
const {TestContainersMongo} = require("@tsed/
|
|
73
|
+
const {TestContainersMongo} = require("@tsed/mongo");
|
|
75
74
|
module.exports = async () => {
|
|
76
75
|
await TestContainersMongo.startMongoServer();
|
|
77
76
|
};
|
|
78
77
|
|
|
79
78
|
// jest.teardown.js
|
|
80
|
-
const {TestContainersMongo} = require("@tsed/
|
|
79
|
+
const {TestContainersMongo} = require("@tsed/mongo");
|
|
81
80
|
module.exports = async () => {
|
|
82
81
|
await TestContainersMongo.stopMongoServer();
|
|
83
82
|
};
|
|
@@ -90,7 +89,7 @@ import {defineConfig} from "vitest/config";
|
|
|
90
89
|
|
|
91
90
|
export default defineConfig({
|
|
92
91
|
test: {
|
|
93
|
-
globalSetup: [import.meta.resolve("@tsed/
|
|
92
|
+
globalSetup: [import.meta.resolve("@tsed/mongo/vitest/setup")]
|
|
94
93
|
}
|
|
95
94
|
});
|
|
96
95
|
```
|
|
@@ -109,7 +108,7 @@ Use the `TestContainersMongo.create` method to start the mongo server before you
|
|
|
109
108
|
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
110
109
|
import {Property, Required} from "@tsed/schema";
|
|
111
110
|
import {Model, MongooseModel, ObjectID, PostHook, PreHook, Unique} from "@tsed/mongoose";
|
|
112
|
-
import {TestContainersMongo} from "@tsed/
|
|
111
|
+
import {TestContainersMongo} from "@tsed/mongo";
|
|
113
112
|
|
|
114
113
|
@Model({schemaOptions: {timestamps: true}})
|
|
115
114
|
@PreHook("save", (user: UserModel, next: any) => {
|
|
@@ -176,7 +175,7 @@ TestContainersMongo provides a method to get the connection options for MikroORM
|
|
|
176
175
|
import {EntityManager, MikroORM} from "@mikro-orm/core";
|
|
177
176
|
import {defineConfig} from "@mikro-orm/mongodb";
|
|
178
177
|
import {PlatformTest} from "@tsed/platform-http/testing";
|
|
179
|
-
import {TestContainersMongo} from "@tsed/
|
|
178
|
+
import {TestContainersMongo} from "@tsed/mongo";
|
|
180
179
|
|
|
181
180
|
beforeEach(async () => {
|
|
182
181
|
const mongoSettings = TestContainersMongo.getMongoConnectionOptions();
|
package/lib/esm/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./services/TestContainersMongo.js";
|
|
@@ -1,51 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
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
|
-
}
|
package/lib/types/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./services/TestContainersMongo.js";
|
|
@@ -1,26 +0,0 @@
|
|
|
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
|
-
}[];
|
|
@@ -1,16 +0,0 @@
|
|
|
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
|
-
}
|