@tsed/testcontainers-mongo
v8.50.2
Published
A module to test Ts.ED code that use MongoDB as database using Test Containers
Readme
A package of Ts.ED framework. See website: https://tsed.dev/
This package allows you to test your code using the TestContainers library.
Installation
To use the @tsed/testcontainers-mongo package, you need to install the package:
npm install --save-dev @tsed/mongoyarn add --dev @tsed/mongopnpm add --dev @tsed/mongobun add --dev @tsed/mongoImportant
This package requires the @tsed/platform-http package to work properly. If you're looking for a standalone package,
you can use the @tsedio/testcontainers-mongo premium package available here: https://tsed.dev/plugins/premium/config-source/mongo.html
Configuration
Add or update your jest or vitest configuration file to add a global setup file:
Jest
// jest.config.js
module.exports = {
globalSetup: ["jest.setup.js"],
globalTeardown: ["jest.teardown.js"]
};
// jest.setup.js
const {TestContainersMongo} = require("@tsed/mongo");
module.exports = async () => {
await TestContainersMongo.startMongoServer();
};
// jest.teardown.js
const {TestContainersMongo} = require("@tsed/mongo");
module.exports = async () => {
await TestContainersMongo.stopMongoServer();
};Vitest
import {defineConfig} from "vitest/config";
export default defineConfig({
test: {
globalSetup: [import.meta.resolve("@tsed/mongo/vitest/setup")]
}
});:::
Usage
@tsed/mongoose
Unit test
Use the TestContainersMongo.create method to start the mongo server before your test:
import {PlatformTest} from "@tsed/platform-http/testing";
import {Property, Required} from "@tsed/schema";
import {Model, MongooseModel, ObjectID, PostHook, PreHook, Unique} from "@tsed/mongoose";
import {TestContainersMongo} from "@tsed/mongo";
@Model({schemaOptions: {timestamps: true}})
@PreHook("save", (user: UserModel, next: any) => {
user.pre = "hello pre";
next();
})
@PostHook("save", (user: UserModel, next: any) => {
user.post = "hello post";
next();
})
export class UserModel {
@ObjectID("id")
_id: string;
@Property()
@Required()
@Unique()
email: string;
@Property()
pre: string;
@Property()
post: string;
}
describe("UserModel", () => {
beforeEach(() => TestContainersMongo.create());
afterEach(() => TestContainersMongo.reset("users")); // clean users collection after each test
it("should run pre and post hook", async () => {
const userModel = PlatformTest.get<MongooseModel<UserModel>>(UserModel);
// GIVEN
const user = new userModel({
email: "[email protected]"
});
// WHEN
await user.save();
// THEN
expect(user.pre).toEqual("hello pre");
expect(user.post).toEqual("hello post");
});
});Integration test
Just use the TestContainersMongo.bootstrap method to start the mongo server before your test:
beforeEach(() => TestContainersMongo.bootstrap(Server, {}));Mikro ORM
TestContainersMongo provides a method to get the connection options for MikroORM:
import {EntityManager, MikroORM} from "@mikro-orm/core";
import {defineConfig} from "@mikro-orm/mongodb";
import {PlatformTest} from "@tsed/platform-http/testing";
import {TestContainersMongo} from "@tsed/mongo";
beforeEach(async () => {
const mongoSettings = TestContainersMongo.getMongoConnectionOptions();
const bstrp = PlatformTest.bootstrap(Server, {
disableComponentScan: true,
imports: [MikroOrmModule],
mikroOrm: [
defineConfig({
clientUrl: mongoSettings.url,
driverOptions: mongoSettings.connectionOptions,
entities: [User],
subscribers: [UnmanagedEventSubscriber1, new UnmanagedEventSubscriber2()]
})
]
});
});Contributors
Backers
Thank you to all our backers! 🙏 [Become a backer]
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
License
The MIT License (MIT)
Copyright (c) 2016 - 2022 Romain Lenzotti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
