繁体   English   中英

在相对路径上嘲笑

[英]jest mocking on relative path

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

我错过了什么?

你必须像这样嘲笑它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM