如何使用 Jest 对 typeorm getRepository 进行单元测试?
How to unit test typeorm getRepository with Jest?
我正在使用带有 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,
})),
};
});
当我尝试这样做时,出现以下错误
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,
})),
};
});
在更新 jest 库后遇到同样的问题,通过直接从 typeorm/globals 而不是 typeorm(index file)
模拟 getRepository 方法解决了这个问题
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";
我正在使用带有 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,
})),
};
});
当我尝试这样做时,出现以下错误
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,
})),
};
});
在更新 jest 库后遇到同样的问题,通过直接从 typeorm/globals 而不是 typeorm(index file)
模拟 getRepository 方法解决了这个问题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";