在相对路径上嘲笑

时间:2017-06-26 04:16:13

标签: javascript testing ecmascript-6 mocking jestjs

我正试图让人开玩笑地在相对路径上工作。相同的代码,但模拟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()从未被调用过,尽管我调试它时可以看到它已经达到了这个功能...

我错过了什么?

1 个答案:

答案 0 :(得分:9)

你必须像这样嘲笑:

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