【发布时间】:2019-12-27 10:21:09
【问题描述】:
我在测试 Nestjs 时遇到了奇怪的问题。
当我运行npm run test 时,大多数测试都失败了。一些变量变得未定义,嵌套无法解决依赖关系等。
但是当我从 WebStorm 面板运行 ALL TESTS 时,一切正常。
https://gist.github.com/hejkerooo/5a56ee2a7d78ce41cb390d1ece648933
Nest can't resolve dependencies of the TrendService (?). Please make sure that the argument at index [0] is available in the _RootTestModule context.
at Injector.lookupComponentInExports (node_modules/@nestjs/core/injector/injector.js:180:19)
Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
at mapper (node_modules/jest-jasmine2/build/queueRunner.js:25:45)
TypeError: Cannot read property 'clear' of undefined
17 | beforeEach(async () => {
18 | jest.restoreAllMocks();
> 19 | await trendsRepository.clear();
| ^
20 | });
21 |
22 | afterAll(async () => {
ReferenceError: You are trying to `import` a file after the Jest environment has been torn down.
(node:2633) UnhandledPromiseRejectionWarning: TypeError: Caught error after test environment was torn down
【问题讨论】:
-
您能否提供更多有关您的错误的背景信息?就像您定义测试文件的方式一样,
TrendService在哪里被导入和使用,以及您如何在测试文件中模拟它。最好提供一个有问题的存储库,以便我们可以重现并更好地帮助您
标签: node.js typescript jestjs nestjs