@tsed/testcontainers-mongo 7.80.0 → 7.80.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.
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
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": "commonjs",
5
- "version": "7.80.0",
5
+ "version": "7.80.2",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/cjs/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -11,6 +11,7 @@
11
11
  ".": {
12
12
  "types": "./lib/types/index.d.ts",
13
13
  "import": "./lib/esm/index.js",
14
+ "require": "./lib/cjs/index.js",
14
15
  "default": "./lib/cjs/index.js"
15
16
  },
16
17
  "./vitest/setup": {
@@ -29,12 +30,12 @@
29
30
  },
30
31
  "devDependencies": {
31
32
  "@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",
33
+ "@tsed/barrels": "7.80.2",
34
+ "@tsed/common": "7.80.2",
35
+ "@tsed/core": "7.80.2",
36
+ "@tsed/di": "7.80.2",
37
+ "@tsed/mongoose": "7.80.2",
38
+ "@tsed/typescript": "7.80.2",
38
39
  "eslint": "^8.57.0",
39
40
  "testcontainers": ">=10.11.0",
40
41
  "typescript": "4.9.5"
@@ -42,7 +43,7 @@
42
43
  "peerDependencies": {
43
44
  "@testcontainers/mongodb": ">=10.11.0",
44
45
  "@tsed/common": "workspace:*",
45
- "@tsed/core": "7.80.0",
46
+ "@tsed/core": "7.80.2",
46
47
  "@tsed/di": "workspace:*",
47
48
  "@tsed/mongoose": "workspace:*",
48
49
  "testcontainers": ">=10.11.0"
package/readme.md CHANGED
@@ -3,7 +3,7 @@
3
3
  </p>
4
4
 
5
5
  <div align="center">
6
- <h1>Mongoose testing</h1>
6
+ <h1>TestContainers Mongo</h1>
7
7
 
8
8
  [![Build & Release](https://github.com/tsedio/tsed/workflows/Build%20&%20Release/badge.svg)](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
9
9
  [![PR Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
@@ -29,111 +29,123 @@
29
29
 
30
30
  A package of Ts.ED framework. See website: https://tsed.io/
31
31
 
32
- This package is an helper to create unit test base on `@tsed/mongoose` package.
32
+ This package allows you to test your code using the [TestContainers](https://node.testcontainers.org/) library.
33
33
 
34
34
  # Installation
35
35
 
36
- ```bash
37
- npm install --save @tsed/testing-mongoose
36
+ To use the `@tsed/testcontainers-mongo` package, you need to install the package:
37
+
38
+ ```sh [npm]
39
+ npm install --save-dev @tsed/testcontainers-mongo
38
40
  ```
39
41
 
40
- ## Example usage
42
+ ```sh [yarn]
43
+ yarn add --dev @tsed/testcontainers-mongo
44
+ ```
41
45
 
42
- ### Testing server
46
+ ```sh [pnpm]
47
+ pnpm add --dev @tsed/testcontainers-mongo
48
+ ```
43
49
 
44
- Here an example to your server with a mocked database:
50
+ ```sh [bun]
51
+ bun add --dev @tsed/testcontainers-mongo
52
+ ```
45
53
 
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
- });
54
+ ### Configuration
63
55
 
64
- after(TestMongooseContext.reset); // reset database and injector
56
+ Add or update your jest or vitest configuration file to add a global setup file:
65
57
 
66
- describe("GET /rest/calendars", () => {
67
- it("should do something", async () => {
68
- const response = await request.get("/rest/calendars").expect(200);
58
+ #### Jest
69
59
 
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
60
+ ```ts
61
+ // jest.config.js
62
+ module.exports = {
63
+ globalSetup: ["jest.setup.js"],
64
+ globalTeardown: ["jest.teardown.js"]
65
+ };
81
66
 
82
- A [ReplicaSet](https://github.com/nodkz/mongodb-memory-server#replica-set-start) can be easily started with:
67
+ // jest.setup.js
68
+ const {TestContainersMongo} = require("@tsed/testcontainers-mongo");
69
+ module.exports = async () => {
70
+ await TestContainersMongo.startMongoServer();
71
+ };
83
72
 
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
- });
73
+ // jest.teardown.js
74
+ const {TestContainersMongo} = require("@tsed/testcontainers-mongo");
75
+ module.exports = async () => {
76
+ await TestContainersMongo.stopMongoServer();
77
+ };
78
+ ```
108
79
 
109
- after(TestMongooseContext.reset); // reset database and injector
80
+ ### Vitest
110
81
 
111
- describe("GET /rest/calendars", () => {
112
- it("should do something", async () => {
113
- const response = await request.get("/rest/calendars").expect(200);
82
+ ```ts
83
+ import {defineConfig} from "vitest/config";
114
84
 
115
- expect(response.body).to.be.an("array");
116
- });
117
- });
85
+ export default defineConfig({
86
+ test: {
87
+ globalSetup: [import.meta.resolve("@tsed/testcontainers-mongo/vitest/setup")]
88
+ }
118
89
  });
119
90
  ```
120
91
 
121
- ## Jest additional setup
92
+ :::
122
93
 
123
- Add a script to close connection after all unit test. In your jest configuration file add the following line:
94
+ ### Usage
124
95
 
125
- ```json
126
- {
127
- "globalTeardown": "./scripts/jest/teardown.js"
96
+ ```ts
97
+ import {PlatformTest} from "@tsed/common";
98
+ import {Property, Required} from "@tsed/schema";
99
+ import {Model, MongooseModel, ObjectID, PostHook, PreHook, Unique} from "@tsed/mongoose";
100
+ import {TestContainersMongo} from "@tsed/testcontainers-mongo";
101
+
102
+ @Model({schemaOptions: {timestamps: true}})
103
+ @PreHook("save", (user: UserModel, next: any) => {
104
+ user.pre = "hello pre";
105
+
106
+ next();
107
+ })
108
+ @PostHook("save", (user: UserModel, next: any) => {
109
+ user.post = "hello post";
110
+
111
+ next();
112
+ })
113
+ export class UserModel {
114
+ @ObjectID("id")
115
+ _id: string;
116
+
117
+ @Property()
118
+ @Required()
119
+ @Unique()
120
+ email: string;
121
+
122
+ @Property()
123
+ pre: string;
124
+
125
+ @Property()
126
+ post: string;
128
127
  }
129
- ```
130
128
 
131
- And create the script with the following content:
129
+ describe("UserModel", () => {
130
+ beforeEach(() => TestContainersMongo.create());
131
+ afterEach(() => TestContainersMongo.reset("users")); // clean users collection after each test
132
132
 
133
- ```js
134
- module.exports = async () => {
135
- await global.__MONGOD__.stop();
136
- };
133
+ it("should run pre and post hook", async () => {
134
+ const userModel = PlatformTest.get<MongooseModel<UserModel>>(UserModel);
135
+
136
+ // GIVEN
137
+ const user = new userModel({
138
+ email: "test@test.fr"
139
+ });
140
+
141
+ // WHEN
142
+ await user.save();
143
+
144
+ // THEN
145
+ expect(user.pre).toEqual("hello pre");
146
+ expect(user.post).toEqual("hello post");
147
+ });
148
+ });
137
149
  ```
138
150
 
139
151
  ## Contributors