首页 > 解决方案 > 为什么 testDouble 不能模拟 3rd 方函数?

问题描述

我有一个本地模块,该模块具有我想使用 tetDouble for nodeJS 模拟的功能

这是我要测试的功能:

import {supportsAPL} from "skills-lib"

export function example(thing: any): boolean {
    if (!supportsAPL(thing)) {
        throw new Error("numbers only!")
    }
    return true
}

这是实际的测试:

const td = require("testdouble")
require("testdouble-jest")(td, jest)

const supportsAPL = td.replace("skills-lib")
import {example} from "../lambda/custom/numbers-only"
// const example = require("../lambda/custom/numbers-only")

describe("ex", () => {
    it("ex2", async () => {
        td.when(supportsAPL("a string")).thenReturn(true) // tee-hee, this is silly
        const result = example("a string")
        expect(result).toMatch("true")
    })
})

代码运行良好,但是当我运行测试时,我得到:

supportsAPL is not a function
TypeError: supportsAPL is not a function

我发现绕过这个问题的唯一方法是为本地模块创建一个包装器并模拟包装器。

有没有人有更好的方法来处理这个问题?

标签: node.jstest-doubletestdoublejs

解决方案


推荐阅读