【问题标题】:Does jest auto mock missing dependencies even when auto mocking is disabled?即使禁用自动模拟,jest 是否会自动模拟缺少依赖项?
【发布时间】:2017-11-23 20:01:51
【问题描述】:

I wrote a decorator simplify smart/dumb components separation in ReactJS,有一次我试图将测试代码分成多个文件并移动了colaborator classes to the __mocks__ directory

文件结构变成:

src
  __mocks__
    SharedComponent.js
    SomeComponent.js
    SomePresenter.js
    SpecificPresenter.js
  __tests__
    index.js
  index.js 

在测试文件index.js中,加载SomeComponent我这样做:

import SomeComponent from 'SomeComponent'

该类仅存在于 mocks 目录中,它仅用于测试装饰器。 You can see here 我并没有在 manual says that auto mocking is disabled by default. 中明确地嘲笑该类

即使禁用自动模拟,jest 是否会自动模拟缺少依赖项?

【问题讨论】:

    标签: reactjs decorator jestjs enzyme


    【解决方案1】:

    Thanks to Michał Pierzchała in this comment:

    当给定模块存在手动模拟时,Jest 的模块系统将在显式调用 jest.mock('moduleName') 时使用该模块。 但是,即使 jest.mock('moduleName') 没有被调用,手动模拟将优先于节点模块。要退出此行为,您需要显式调用 jest.unmock('moduleName' ) 在应该使用实际模块实现的测试中。

    【讨论】:

      猜你喜欢
      • 2018-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 2018-07-23
      • 2021-12-01
      • 2018-01-21
      相关资源
      最近更新 更多