Added a test example using nestjs-typeorm-testing. By now, it's necessary to install.
- @nestjs/typeorm
- @nestjs/common
- @nestjs/core
- rxjs
Todo:
- Replace nestjs-typeorm-testing with a module only about typeorm to avoid installing the modules mentioned above.
The example is here, you don't need even PostgreSQL installed on your machine to check if a hook on your entities is called or not.
import {
createFakeConnection,
FakeConnection
} from "@devniel/nestjs-typeorm-testing";
import { Category } from "../entity/Category";
import { Post } from "../entity/Post";
describe("test", () => {
let connection: FakeConnection;
beforeAll(async () => {
connection = await createFakeConnection({
name: "test",
type: "postgres",
entities: [Category, Post]
});
});
it("should invoke the before and after hooks before creating a new category", async () => {
let category = new Category();
category.name = "A new category";
const spyBeforeInsert = spyOn(category, "beforeInsert");
const spyAfterInsert = spyOn(category, "afterInsert");
const repository = connection.getRepository(Category);
await repository.save(category);
console.log(`Category has been saved. Category is '${category.name}'`);
expect(spyBeforeInsert).toHaveBeenCalled();
expect(spyAfterInsert).toHaveBeenCalled();
});
});- clone repository
- run
npm i - edit
ormconfig.jsonand change your database configuration (you can also change a database type, but don't forget to install specific database drivers) - run
npm start - enjoy!
- install
typeormglobally:npm i -g typeorm - run
typeorm -hto show list of available commands