je kunt jest.spyOn gebruiken voor de QueryBuilder
prototype zoals dit:
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);
});
});
functiegebruik
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();
}