簡體   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