'hot to moke the typeORM repositery for tesing with jest in the express/koa
I am calling repository methods inside of the service and now I have to write the unit test for the service. I want to moke repositery Class or Method both.
Here is the service code for which I have to write a test.
import { AttackMatrixRepo } from '../repositories/index';
export const serGetAttackMatrixDetail = async () => {
const attMatRepo = new AttackMatrixRepo();
const table = await attMatRepo.repGetMatric();
return table;
};
Here is the repository code
import { AttackMatrix } from '../entities/index'; // typeorm AttackMatrix schema
import { Repository, BaseEntity } from 'typeorm';
import { AppDataSource } from '../bootstrap/typeorm'; // typeorm connection
export class AttackMatrixRepo extends BaseEntity {
private attackMatrixEntity: Repository<AttackMatrix>;
constructor() {
super();
this.attackMatrixEntity = AppDataSource.getRepository(AttackMatrix);
}
public async repGetMatric() {
return this.attackMatrixEntity.createQueryBuilder('attMatric').getOne();
}
public async getMatrixByStixId({ columNames = ['*'], stixId }): Promise<AttackMatrix> {
const tacticQueryBuilder = await this.attackMatrixEntity.createQueryBuilder('matrix');
const query = tacticQueryBuilder.select(columNames).where('matrix.stix_id = :stixId', { stixId });
return query.getRawOne();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
