node.js - 为什么 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
我发现绕过这个问题的唯一方法是为本地模块创建一个包装器并模拟包装器。
有没有人有更好的方法来处理这个问题?
解决方案
推荐阅读
- r - RStudio 中用于推文先验分析的事务问题
- c++ - C++20 模块种类/大小之间的区别?
- php - 为什么我得到错误的 URI?
- visual-studio - 单个 .Net 项目的 BitBucket 管道
- deep-learning - BertTokenizer 和词嵌入类似吗?
- android - 哪种说法最好?
- java - 如何修改 Soap 主体以在使用 org.springframework.ws.soap.soapmessage 的 java 中添加一个额外的标签?
- java - 打开失败:EROFS(只读文件系统)[Android Studio Java]
- docker - 为什么 dockerhub 不在不同的存储库之间共享基础镜像?
- html - 是否可以找到react项目的监听事件