【发布时间】:2020-07-17 23:19:41
【问题描述】:
我设法在一定程度上模拟了 typeorm,但现在我面临一个奇怪的问题,我将在这里说明。
import { myEntity } from './entity';
import typeorm = require('typeorm');
describe('test suire'), () => {
let myRepository: typeorm.Repository<myEntity>;
test('my test case', () => {
typeorm.Repository = jest.fn().mockReturnValue({
createQueryBuilder: jest.fn().mockReturnValue({
where: jest.fn().mockReturnThis(),
getMany: jest.fn().mockReturnValue([]);
})
});
myRepository = new typeorm.Repository();
expect(myRepository.createQueryBuilder).toHaveBeenCalledTimes(0);
})
})
我有一个这样的包结构:
root/
package.json
project1/
package.json
src/
the_above_test.spec.ts
当我从project1 运行node_modules/.bin/jest path_to_above_test.spec.ts 时,它可以工作。但是当我从root 运行相同的命令时,我得到:
Matcher error: received value must be a mock or spy function
Received has value: undefined at line:
expect(myRepository.createQueryBuilder).toHaveBeenCalledTimes(0);
这里的目的是在 VS Code 中调试测试。但由于 VS 代码在根级别打开,它从那里执行测试。如果我的代码没有错误,我如何告诉 VS Code 从project1 目录运行测试?
【问题讨论】:
-
@AluanHaddad 对不起,我想我不明白。这一切都开始了,因为我想从根级别打开的 Visual Code 运行 Jest。如果我可以告诉 VS Code 中的 Jest 从 project1 运行,我就不会遇到这个问题,但我想不通。
-
这当然可以,但我的目的是能够在 VS Code 中进行调试。
-
@AluanHaddad 我解决了这个问题。
标签: javascript typescript visual-studio-code jestjs