How to configure integration testing #1530
Unanswered
detroitpro
asked this question in
Q&A
Replies: 2 comments 4 replies
-
Hello @detroitpro The principle is to mock the TypeORMService using the second parameters of .invoke method: import { PlatformTest } from '@tsed/common';
import { MessageModel } from '../../models/MessageModel';
import { MessageService } from '../../services/MessageService';
async function createMessageServiceFixture(update: any) {
const repository = {
save: jest.fn().mockImplementation((document: any) => {
return {...document, ...update}
}
};
const connection = {
getRepository: jest.fn().mockReturnValue(repository)
};
const typeORMService = {
get: jest.fn().mockReturnValue(connection)
};
const instance = await PlatformTest.invoke<MessageService>(MessageService, [
{
token: TypeORMService,
use: typeORMService
}
]);
instance.$afterRoutesInit();
return {instance, typeORMService, connection, repository}
}
describe('MessageService', () => {
beforeEach(PlatformTest.create);
afterEach(PlatformTest.reset);
it('should save to repository', async () => {
const expected = {updatedAt: new Date()};
const {instance, typeORMService, connection, repository} = await createMessageServiceFixture(expected);
const message = {
id: 'id'
}
const result = await instance.create({...message} as MessageModel);
expect(result).toEqual({...message, ...expected});
expect(typeORMService.get).toHaveBeenCalledWith("default");
expect(connection.getRepository).toHaveBeenCalledWith(MessageModel);
expect(repository.save).toHaveBeenCalledWith(message);
});
}); See you |
Beta Was this translation helpful? Give feedback.
4 replies
-
Hello @detroitpro Create a function to create a sqlite connection: import {DITest} from "@tsed/di";
import {TypeORMService} from "@tsed/typeorm";
export async function createConnectionFixture(options: any = {}) {
const typeormService = await DITest.invoke<TypeORMService>(TypeORMService);
return typeormService.createConnection({
name: "default",
type: "sqlite",
database: ":memory:",
dropSchema: true,
entities: [],
synchronize: true,
logging: false,
...options
});
} Then use it in your unit test import { PlatformTest } from '@tsed/common';
import { MessageModel } from '../../models/MessageModel';
import { MessageService } from '../../services/MessageService';
describe('MessageService', () => {
beforeEach(PlatformTest.create);
afterEach(PlatformTest.reset);
it('should save to repository', async () => {
await createConnectionFixture({
entities: [MessageModel]
});
const instance = await PlatformTest.invoke<MessageService>(MessageService);
await instance.create({} as MessageModel);
});
}); Tell me if it works ;) See you |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
What is the best way to integration test a service that uses the AfterRoutesInit to load the database connection by name?
This test fails with
Connection "default" was not found.
When I log the TypeORMService that is passed to the MessageService constructor, it looks like the connecdtionMap is empty and I assume that is need?
Should I build a
TypeORMService
in the test and pass it in to the message service? Is there another way to create a service?Beta Was this translation helpful? Give feedback.
All reactions