[英]How to unit test typeorm getRepository with Jest?
我將 typescript 與 typeorm 一起使用,並且我有一個這樣的存儲庫:
import { EntityRepository, getRepository, createQueryBuilder } from 'typeorm';
@EntityRepository()
export default class Repo {
async getSomething(): Promise<Result> {
const schemaQuery = getRepository(SomeModel)
.createQueryBuilder('sm')
.select(...)
.where(...);
.....
我的測試文件是這樣的
import * as typeorm from 'typeorm';
import Repo from '../../../../src/repositories/Repo';
describe(
'test',
() => {
let repo: Repo;
beforeEach(() => {
repo = new Repo();
});
test('getSomething works', async () => {
jest.spyOn(typeorm, 'getRepository').mockImplementation(() => ({ // typescript wants me to implement all properties of getRepository which i dont want
createQueryBuilder: jest.fn(),
}));
...
});
},
);
我如何直接從仍符合 typescript 類型檢查的 typeorm 模擬 getRepository?
我剛剛遇到了這個問題,實際上我使用您的代碼作為我的解決方案的基礎。 請試試這個:
jest.spyOn(typeorm, "getRepository").mockImplementation(() => {
const original = jest.requireActual("typeorm");
// You need all functions used in your Query builder
return {
...original,
createQueryBuilder: jest.fn().mockImplementation(() => ({
subQuery: jest.fn().mockReturnThis() as unknown,
from: jest.fn().mockReturnThis() as unknown,
where: jest.fn().mockReturnThis() as unknown,
select: jest.fn().mockReturnThis() as unknown,
getQuery: jest.fn().mockReturnThis() as unknown,
setParameter: jest.fn().mockReturnThis() as unknown,
getMany: jest
.fn()
.mockResolvedValue(expected) as unknown,
})),
};
});
在更新 jest 庫后遇到了同樣的問題,通過 mocking getRepository 方法直接從 typeorm/globals 而不是 typeorm(index file) 解決了這個問題
import * as typeorm_functions from 'typeorm/globals';
jest.spyOn(typeorm_functions, 'getRepository').mockReturnValue({
createQueryBuilder: jest.fn().mockImplementation(() => ({
subQuery: jest.fn().mockReturnThis() as unknown,
from: jest.fn().mockReturnThis() as unknown,
where: jest.fn().mockReturnThis() as unknown,
select: jest.fn().mockReturnThis() as unknown,
getQuery: jest.fn().mockReturnThis() as unknown,
setParameter: jest.fn().mockReturnThis() as unknown,
getMany: jest
.fn()
.mockResolvedValue(expected) as unknown,
})),
} as unknown as Repository<unknown>);
我在使用已批准的解決方案時遇到以下錯誤:
TypeError: Cannot redefine property: getRepository
at Function.defineProperty (<anonymous>)
為了解決這個問題,我改用了以下導入語句:
import * as typeorm from "typeorm/globals";
當我嘗試這樣做時,出現以下錯誤
TypeError: Cannot redefine property: getRepository
at Function.defineProperty (<anonymous>)
64 | } as unknown as Installation;
65 |
> 66 | jest.spyOn(typeorm, 'getRepository').mockImplementation(() => {
| ^
67 | const original = jest.requireActual('typeorm');
68 | // You need all functions used in your Query builder
69 | return {
看我的片段
import * as typeorm from 'typeorm';
.
.
.
jest.spyOn(typeorm, 'getRepository').mockImplementation(() => {
const original = jest.requireActual('typeorm');
// You need all functions used in your Query builder
return {
...original,
createQueryBuilder: jest.fn().mockImplementation(() => ({
subQuery: jest.fn().mockReturnThis() as unknown,
from: jest.fn().mockReturnThis() as unknown,
where: jest.fn().mockReturnThis() as unknown,
select: jest.fn().mockReturnThis() as unknown,
getQuery: jest.fn().mockReturnThis() as unknown,
setParameter: jest.fn().mockReturnThis() as unknown,
getMany: jest.fn().mockResolvedValue(expected) as unknown,
})),
};
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.