du kan använda jest.spyOn för QueryBuilder
prototyp som denna:
import { QueryBuilder } from 'typeorm';
describe('Name of the group', () => {
it('should do something', () => {
const queryBuilder: any = {
into: jest.fn().mockReturnThis(),
values: jest.fn().mockReturnThis(),
onConflict: jest.fn().mockReturnThis(),
setParameter: jest.fn().mockReturnThis(),
execute: jest.fn().mockRejectedValueOnce(new Error('db Error')),
// execute: jest.fn().mockResolvedValueOnce({ res: 'mockRes' }),
};
jest
.spyOn(QueryBuilder.prototype, 'insert')
.mockReturnValueOnce(queryBuilder);
});
});
funktionsanvändning
export async function doQuery(input: any){
const queryRunner = await Entity.createQueryRunner();
await queryRunner.manager
.createQueryBuilder()
.insert()
.into(Entity)
.values(input)
.onConflict(
`("id") DO UPDATE SET "date" = :date`
)
.setParameter('date', date)
.execute();
}