gpt4 book ai didi

javascript - 在相对路径上开 Jest

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:56 25 4
gpt4 key购买 nike

我正在尝试让 jest mocking 在相对路径上工作。相同的代码但使用模拟 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()}))

关于javascript - 在相对路径上开 Jest ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44753042/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com