【问题标题】:jest mocking on relative path在相对路径上开玩笑
【发布时间】:2017-11-28 21:35:22
【问题描述】:

我试图让笑话在相对路径上工作。相同的代码但模拟 fs 效果很好,所以我不确定为什么尝试模拟我自己的模块不起作用

// myFile.js
const { cacheFile } = require('./cacheHandler.js')

const myFunc = () => {
  cacheFile(file_name)
}

// myFile.spec.js

const myFile = require('./myFile.js')
const cacheHandler = require('./cacheHandler.js')

jest.mock('./cacheHandler.js')

describe("my failing test :( ", () =>{
  it("should be able to spy on the function", () => {
    cacheHandler.cacheFile = jest.fn()

    myFile.myFunc()

    expect(cacheHandler.cacheFile).toHaveBeenCalledTimes(1)   
  }
}

jest 声称 cacheFile() 从未被调用,尽管当我调试它时,我可以看到它到达了这个函数......

我错过了什么?

【问题讨论】:

    标签: javascript testing ecmascript-6 mocking jestjs


    【解决方案1】:

    你必须像这样模拟它:

    jest.mock('./cacheHandler.js', ()=>({cacheFile: jest.fn()}))
    

    【讨论】:

      猜你喜欢
      • 2022-12-08
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 2016-05-24
      • 2020-03-25
      • 2019-12-05
      • 2021-08-10
      • 2022-12-09
      相关资源
      最近更新 更多