简体   繁体   中英

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

// 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...

What am I missing?

你必须像这样嘲笑它:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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