Skip to content

Commit 9230a2a

Browse files
committed
fix(testcontainers-mongo): update readme package and add documentation
1 parent bd781f1 commit 9230a2a

File tree

7 files changed

+282
-169
lines changed

7 files changed

+282
-169
lines changed

docs/readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ frameworks:
211211
- title: Pulse
212212
href: /tutorials/pulse.html
213213
src: /pulse.png
214+
- title: TestContainers
215+
href: /tutorials/mongoose.html#testing
216+
src: https://avatars.githubusercontent.com/u/13393021?s=200&v=4
214217
---
215218

216219
::: slot hero-brand

docs/tutorials/mongoose.md

Lines changed: 106 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,40 @@ Currently, [`@tsed/mongoose`](https://www.npmjs.com/package/@tsed/mongoose) allo
3838

3939
Before using the `@tsed/mongoose` package, we need to install the [mongoose](https://www.npmjs.com/package/mongoose) module.
4040

41-
```bash
42-
npm install --save mongoose
43-
npm install --save @tsed/mongoose
44-
npm install --save-dev @tsed/testing-mongoose
41+
<Tabs class="-code">
42+
<Tab label="npm">
43+
44+
```sh [npm]
45+
npm install --save mongoose @tsed/mongoose
46+
npm install --save-dev @tsed/testcontainers-mongo
4547
```
4648

47-
::: warning
48-
Since mongoose v5.11.0, the module expose his own file definition and can broke your build!
49-
To solve it, install @tsed/mongoose v6.14.1 and remove the `@types/mongoose` dependencies.
50-
:::
49+
</Tab>
50+
<Tab label="yarn">
51+
52+
```sh [yarn]
53+
yarn add mongoose @tsed/mongoose
54+
yarn add -D @tsed/testcontainers-mongo
55+
```
56+
57+
</Tab>
58+
<Tab label="pnpm">
59+
60+
```sh [pnpm]
61+
pnpm add mongoose @tsed/mongoose
62+
pnpm add -D @tsed/testcontainers-mongo
63+
```
64+
65+
</Tab>
66+
<Tab label="bun">
67+
68+
```sh [bun]
69+
bun add mongoose @tsed/mongoose
70+
bun add -D @tsed/testcontainers-mongo
71+
```
72+
73+
</Tab>
74+
</Tabs>
5175

5276
Then import `@tsed/mongoose` in your [Configuration](/docs/configuration.md):
5377

@@ -319,92 +343,84 @@ export class MyRepository {
319343

320344
## Testing
321345

322-
The package [`@tsed/testing-mongoose`](https://www.npmjs.com/package/@tsed/testing-mongoose) allows you to test your server with a memory database.
346+
The [`@tsed/testcontainers-mongo`](https://www.npmjs.com/package/@tsed/testcontainers-mongo) package allows you to test your code using the [TestContainers](https://node.testcontainers.org/) library.
323347

324-
::: tip
325-
This package uses the amazing [mongodb-memory-server](https://www.npmjs.com/package/mongodb-memory-server) to mock the mongo database.
326-
:::
348+
### Configuration
327349

328-
### Testing API
329-
330-
This example shows you how you can test your Rest API with superagent and a mocked Mongo database:
350+
To use the `@tsed/testcontainers-mongo` package, you need to install the package:
331351

332352
<Tabs class="-code">
333-
<Tab label="Jest">
353+
<Tab label="npm">
334354

335-
<<< @/tutorials/snippets/mongoose/testing-api.jest.ts
355+
```sh [npm]
356+
npm install --save-dev @tsed/testcontainers-mongo
357+
```
336358

337359
</Tab>
338-
<Tab label="Mocha">
360+
<Tab label="yarn">
339361

340-
<<< @/tutorials/snippets/mongoose/testing-api.mocha.ts
362+
```sh [yarn]
363+
yarn add --dev @tsed/testcontainers-mongo
364+
```
341365

342366
</Tab>
343-
</Tabs>
367+
<Tab label="pnpm">
344368

345-
::: tip
346-
To increase mocha timeout from 2000ms to 10000ms use option `--timeout 10000`.
347-
:::
348-
349-
### Testing API with ReplicaSet
369+
```sh [pnpm]
370+
pnpm add --dev @tsed/testcontainers-mongo
371+
```
350372

351-
A [ReplicaSet](https://github.com/nodkz/mongodb-memory-server#replica-set-start) can be easily started with:
373+
</Tab>
374+
<Tab label="bun">
352375

353-
```typescript
354-
import {PlatformTest} from "@tsed/common";
355-
import {PlatformExpress} from "@tsed/platform-express";
356-
import {TestMongooseContext} from "@tsed/testing-mongoose";
357-
import {expect} from "chai";
358-
import * as SuperTest from "supertest";
359-
import {Server} from "../Server";
360-
361-
describe("Rest", () => {
362-
// bootstrap your Server to load all endpoints before run your test
363-
let request: SuperTest.Agent;
364-
365-
before(
366-
TestMongooseContext.bootstrap(Server, {
367-
platform: PlatformExpress,
368-
mongod: {
369-
replicaSet: true
370-
}
371-
})
372-
); // Create a server with mocked database
373-
before((done) => {
374-
request = SuperTest(PlatformTest.callback());
375-
done();
376-
});
377-
378-
after(TestMongooseContext.reset); // reset database and injector
379-
380-
describe("GET /rest/calendars", () => {
381-
it("should do something", async () => {
382-
const response = await request.get("/rest/calendars").expect(200);
383-
384-
expect(response.body).to.be.an("array");
385-
});
386-
});
387-
});
376+
```sh [bun]
377+
bun add --dev @tsed/testcontainers-mongo
388378
```
389379

390-
### Jest additional setup
380+
</Tab>
381+
</Tabs>
382+
383+
Then add or update your jest or vitest configuration file to add a global setup file:
391384

392-
Add a script to close connection after all unit test. In your jest configuration file add the following line:
385+
<Tabs class="-code">
386+
<Tab label="Jest">
393387

394-
```json
395-
{
396-
"globalTeardown": "./scripts/jest/teardown.js"
397-
}
398-
```
388+
```ts [Jest]
389+
// jest.config.js
390+
module.exports = {
391+
globalSetup: ["jest.setup.js"],
392+
globalTeardown: ["jest.teardown.js"]
393+
};
399394

400-
And create the script with the following content:
395+
// jest.setup.js
396+
const {TestContainersMongo} = require("@tsed/testcontainers-mongo");
397+
module.exports = async () => {
398+
await TestContainersMongo.startMongoServer();
399+
};
401400

402-
```js
401+
// jest.teardown.js
402+
const {TestContainersMongo} = require("@tsed/testcontainers-mongo");
403403
module.exports = async () => {
404-
(await global.__MONGOD__) && global.__MONGOD__.stop();
404+
await TestContainersMongo.stopMongoServer();
405405
};
406406
```
407407

408+
</Tab>
409+
<Tab label="Vitest">
410+
411+
```ts [Vitest]
412+
import {defineConfig} from "vitest/config";
413+
414+
export default defineConfig({
415+
test: {
416+
globalSetup: [import.meta.resolve("@tsed/testcontainers-mongo/vitest/setup")]
417+
}
418+
});
419+
```
420+
421+
</Tab>
422+
</Tabs>
423+
408424
### Testing Model
409425

410426
This example shows you how can test the model:
@@ -415,13 +431,30 @@ This example shows you how can test the model:
415431
<<< @/tutorials/snippets/mongoose/testing-model.jest.ts
416432

417433
</Tab>
418-
<Tab label="Mocha">
434+
<Tab label="Vitest">
419435

420-
<<< @/tutorials/snippets/mongoose/testing-model.mocha.ts
436+
<<< @/tutorials/snippets/mongoose/testing-model.vitest.ts
421437

422438
</Tab>
423439
</Tabs>
424440

441+
### Testing API
442+
443+
This example shows you how you can test your Rest API with superagent and a mocked Mongo database:
444+
445+
<Tabs class="-code">
446+
<Tab label="Jest">
447+
448+
<<< @/tutorials/snippets/mongoose/testing-api.jest.ts
449+
450+
</Tab>
451+
<Tab label="Vitest">
452+
453+
<<< @/tutorials/snippets/mongoose/testing-api.jest.ts
454+
455+
</Tab>
456+
</Tabs>
457+
425458
## Author
426459

427460
<GithubContributors :users="['Romakita']"/>

docs/tutorials/snippets/mongoose/testing-api.jest.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import {PlatformTest} from "@tsed/common";
2-
import {TestMongooseContext} from "@tsed/testing-mongoose";
2+
import {TestContainersMongo} from "@tsed/testcontainers-mongo";
33
import * as SuperTest from "supertest";
44
import {Server} from "../Server";
55

66
describe("Rest", () => {
7-
// bootstrap your Server to load all endpoints before run your test
8-
let request: SuperTest.Agent;
9-
10-
before(TestMongooseContext.bootstrap(Server)); // Create a server with mocked database
11-
before(() => {
12-
request = SuperTest(PlatformTest.callback());
13-
});
14-
15-
after(TestMongooseContext.reset); // reset database and injector
7+
beforeAll(TestContainersMongo.bootstrap(Server)); // Create a server with mocked database
8+
afterAll(() => TestContainersMongo.reset()); // reset database and injector
169

1710
describe("GET /rest/calendars", () => {
1811
it("should do something", async () => {
12+
const request = SuperTest(PlatformTest.callback());
1913
const response = await request.get("/rest/calendars").expect(200);
2014

2115
expect(typeof response.body).toEqual("array");
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {PlatformTest} from "@tsed/common";
2+
import {TestContainersMongo} from "@tsed/testcontainers-mongo";
3+
import * as SuperTest from "supertest";
4+
import {Server} from "../Server";
5+
6+
describe("Rest", () => {
7+
beforeAll(TestContainersMongo.bootstrap(Server)); // Create a server with mocked database
8+
afterAll(() => TestContainersMongo.reset()); // reset database and injector
9+
10+
describe("GET /rest/calendars", () => {
11+
it("should do something", async () => {
12+
const request = SuperTest(PlatformTest.callback());
13+
const response = await request.get("/rest/calendars").expect(200);
14+
15+
expect(typeof response.body).toEqual("array");
16+
});
17+
});
18+
});

docs/tutorials/snippets/mongoose/testing-model.jest.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import {PlatformTest, Property, Required} from "@tsed/common";
1+
import {PlatformTest} from "@tsed/common";
2+
import {Property, Required} from "@tsed/schema";
23
import {Model, MongooseModel, ObjectID, PostHook, PreHook, Unique} from "@tsed/mongoose";
3-
import {TestMongooseContext} from "@tsed/testing-mongoose";
4+
import {TestContainersMongo} from "@tsed/testcontainers-mongo";
45

56
@Model({schemaOptions: {timestamps: true}})
67
@PreHook("save", (user: UserModel, next: any) => {
@@ -30,8 +31,8 @@ export class UserModel {
3031
}
3132

3233
describe("UserModel", () => {
33-
beforeEach(TestMongooseContext.create);
34-
afterEach(TestMongooseContext.reset);
34+
beforeEach(() => TestContainersMongo.create());
35+
afterEach(() => TestContainersMongo.reset("users")); // clean users collection after each test
3536

3637
it("should run pre and post hook", async () => {
3738
const userModel = PlatformTest.get<MongooseModel<UserModel>>(UserModel);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {PlatformTest} from "@tsed/common";
2+
import {Property, Required} from "@tsed/schema";
3+
import {Model, MongooseModel, ObjectID, PostHook, PreHook, Unique} from "@tsed/mongoose";
4+
import {TestContainersMongo} from "@tsed/testcontainers-mongo";
5+
6+
@Model({schemaOptions: {timestamps: true}})
7+
@PreHook("save", (user: UserModel, next: any) => {
8+
user.pre = "hello pre";
9+
10+
next();
11+
})
12+
@PostHook("save", (user: UserModel, next: any) => {
13+
user.post = "hello post";
14+
15+
next();
16+
})
17+
export class UserModel {
18+
@ObjectID("id")
19+
_id: string;
20+
21+
@Property()
22+
@Required()
23+
@Unique()
24+
email: string;
25+
26+
@Property()
27+
pre: string;
28+
29+
@Property()
30+
post: string;
31+
}
32+
33+
describe("UserModel", () => {
34+
beforeEach(() => TestContainersMongo.create());
35+
afterEach(() => TestContainersMongo.reset("users")); // clean users collection after each test
36+
37+
it("should run pre and post hook", async () => {
38+
const userModel = PlatformTest.get<MongooseModel<UserModel>>(UserModel);
39+
40+
// GIVEN
41+
const user = new userModel({
42+
43+
});
44+
45+
// WHEN
46+
await user.save();
47+
48+
// THEN
49+
expect(user.pre).toEqual("hello pre");
50+
expect(user.post).toEqual("hello post");
51+
});
52+
});

0 commit comments

Comments
 (0)