简体   繁体   English

在相对路径上嘲笑

[英]jest mocking on relative path

I'm trying to get jest mocking to work on a relative path. 我正试图开玩笑地嘲笑相对路径。 The same code but with mocking fs worked great, so I'm not sure why trying to mock my own modules doesn't work 相同的代码但使用模拟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 claims that the cacheFile() was never called, eventhough when I debug this I can see that it reached this function... jest声称cacheFile()从未被调用过,尽管我调试这个时我可以看到它已经达到了这个功能......

What am I missing? 我错过了什么?

你必须像这样嘲笑它:

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

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

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