【发布时间】:2019-11-07 17:35:27
【问题描述】:
我的文件夹结构:
├──wrapper.js
├──lib
| └──zwportal.js
├── __tests__
│ └── wrapper.test.js
── __mocks__
│ └── zwportal.js
|
在wrapper.js:
const portal = require('./lib/zwportal.js')
由于 zwportal.js 非常复杂,我使用一个放在 mocks 文件夹中的模拟。所以在我的测试中:
'use strict';
// Mocking dependencies
jest.mock('../lib/zwportal.js');
zwportal = require('../lib/zwportal.js');
但是,当我跑步时:
jest
上面写着:
FAIL __tests__/wrapper.test.js
● Test suite failed to run
Cannot find module 'abstract-socket' from 'avro-utils.js'
实际上,这些文件abstract-socket 和avro-utils 位于原始的“非模拟”zwportal.js 中。看来 jest 并没有模拟这个文件。为什么?
编辑
在我的测试套件中,如果我写:
jest.mock('../lib/zwportal.js', () => {});
zwportal = require('../lib/zwportal.js');
没有问题发生。但是,我需要模拟一些行为来模拟。
【问题讨论】:
-
您找到解决方案了吗?我遇到了类似的问题。
标签: jestjs