Jest如何mock模块创建阶段就调用的internalComponentFactory函数?
问题根因
你遇到的调用次数统计为0的核心原因是被测试模块(MyComponent.js)的顶层代码执行时机早于mock的生效时机,再叠加Jest的模块缓存机制导致模块只会加载一次。即使jest.mock会被Jest自动提升到测试文件所有导入语句之前,如果你在测试文件顶部静态导入了MyComponent,或者没有清除历史模块缓存,都会出现统计不到调用、偶现失败的问题。
正确实现方案
直接按如下写法修改测试文件即可:
// MyComponent.test.js import {internalComponentFactory} from './factories'; // 声明mock,会被Jest自动提升到文件最顶部 jest.mock('./factories', () => ({ internalComponentFactory: jest.fn(() => { // 返回一个模拟组件,避免后续渲染报错 return jest.fn(() => null); }) })); describe('src/MyComponent', () => { beforeEach(() => { // 每次测试前清空模块缓存,避免历史加载结果干扰 jest.resetModules(); // 重置mock函数的调用统计 internalComponentFactory.mockClear(); }); it('正确调用internalComponentFactory并传入预期参数', () => { // 动态导入被测试模块,确保此时mock已经完全生效 const { MyComponent } = require('./MyComponent'); expect(internalComponentFactory).toHaveBeenCalledTimes(1); expect(internalComponentFactory).toHaveBeenCalledWith([1,2,3]); }); // 可选:如果需要验证组件渲染时传入InternalComponent的参数,可以加这个用例 it('给生成的InternalComponent传入正确props', async () => { const { MyComponent } = require('./MyComponent'); const { render } = require('@testing-library/react'); render(<MyComponent />); // 拿到工厂函数返回的模拟组件 const MockInternalComponent = internalComponentFactory.mock.results[0].value; expect(MockInternalComponent).toHaveBeenCalledWith({locale: 'en-us'}, {}); }); })
关键注意点
- 不要在测试文件顶部静态导入
MyComponent,必须在mock初始化完成、缓存清空后再动态导入,保证模块顶层的internalComponentFactory调用用的是你mock的函数 - 必须加
jest.resetModules(),彻底解决不同测试用例之间的缓存干扰问题,消除之前的偶现失败 - mock的
internalComponentFactory需要返回一个合法的函数(模拟React组件),否则MyComponent渲染时会因为InternalComponent不是有效组件抛出错误 - 如果你习惯用ES模块的动态
import()语法,记得给测试用例加async、await动态导入的结果。
内容的提问来源于stack exchange,提问作者Switch386
相关产品推荐
相关产品推荐

