Nux*_*Nux 4 unit-testing node.js typescript jestjs typeorm
我想对一个在其构造函数中使用 getCustomRepository 的类进行单元测试,但我只是想不出一种简单的方法来模拟它。这是我的班级代码
import {getCustomRepository} from 'typeorm';
export class Controller {
private repository: UserRepository;
constructor() {
this.repository = getCustomRepository(UserRepository); //I want to mock this.
}
async init() {
return this.repository.findUser(1);
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试
describe('User Tests', () => {
it('should return user', async () => {
//Fake user to be resolved.
const user = new User();
user.id = 2;
//I want to mock getCustomRepository(UserRepository); here
//getCustomRepository = jest.fn().mockResolvedValue(UserRepository); HERE HOW???
//Mocking find user
UserRepository.prototype.findUser = jest.fn().mockResolvedValue(user);
const controller = new Controller();
const result = await controller.init();
expect(result).toBeDefined();
});
});
Run Code Online (Sandbox Code Playgroud)
注意:模拟存储库方法效果很好,但我真的想模拟 getCustomRepository,以便它可以减少尝试连接到数据库所浪费的时间。
这就是 typeORM 中 getCustomRepository 的样子
export declare function getCustomRepository<T>(customRepository: ObjectType<T>, connectionName?: string): T;
Run Code Online (Sandbox Code Playgroud)
用户存储库.ts
@EntityRepository(User)
export class UserRepository extends Repository<User> {
public async findUser(id: number) {
return 'real user';
}
}
Run Code Online (Sandbox Code Playgroud)
用户.ts
@Entity('users')
export class User{
@PrimaryGeneratedColumn()
id: number;
@Column({type: 'varchar', length: 100})
name: string;
}
Run Code Online (Sandbox Code Playgroud)
所以问题是我如何嘲笑它?任何帮助都感激不尽。
您可以使用jest.mock(moduleName,factory,options)来模拟typeorm模块、getCustomRepository函数及其返回值。
例如
\n\ncontroller.ts:
import { getCustomRepository } from \'typeorm\';\nimport { UserRepository } from \'./userRepo\';\n\nexport class Controller {\n private repository: UserRepository;\n\n constructor() {\n this.repository = getCustomRepository(UserRepository);\n }\n\n async init() {\n return this.repository.findUser(1);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\nuserRepo.ts:
export class UserRepository {\n public async findUser(id: number) {\n return \'real user\';\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\ncontroller.test.ts:
import { Controller } from \'./controller\';\nimport { getCustomRepository } from \'typeorm\';\nimport { mocked } from \'ts-jest/utils\';\nimport { UserRepository } from \'./userRepo\';\n\njest.mock(\'typeorm\', () => ({ getCustomRepository: jest.fn() }));\n\ndescribe(\'61693597\', () => {\n it(\'should pass\', async () => {\n const userRepo = { findUser: jest.fn().mockResolvedValueOnce(\'fake user\') };\n mocked(getCustomRepository).mockReturnValueOnce(userRepo);\n const controller = new Controller();\n const actual = await controller.init();\n expect(actual).toBe(\'fake user\');\n expect(getCustomRepository).toBeCalledWith(UserRepository);\n expect(userRepo.findUser).toBeCalledWith(1);\n });\n});\nRun Code Online (Sandbox Code Playgroud)\n\n带有覆盖率报告的单元测试结果:
\n\n PASS stackoverflow/61693597/controller.test.ts (13.53s)\n 61693597\n \xe2\x9c\x93 should pass (9ms)\n\n---------------|---------|----------|---------|---------|-------------------\nFile | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s \n---------------|---------|----------|---------|---------|-------------------\nAll files | 92.31 | 100 | 80 | 90.91 | \n controller.ts | 100 | 100 | 100 | 100 | \n userRepo.ts | 80 | 100 | 50 | 75 | 3 \n---------------|---------|----------|---------|---------|-------------------\nTest Suites: 1 passed, 1 total\nTests: 1 passed, 1 total\nSnapshots: 0 total\nTime: 15.596s\nRun Code Online (Sandbox Code Playgroud)\n\n源代码:https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/61693597
\n| 归档时间: |
|
| 查看次数: |
13123 次 |
| 最近记录: |