【发布时间】:2025-12-29 22:20:21
【问题描述】:
我有一个名为 helper.js 的文件,其中包含两个函数
export const funcA = (key) => {
return funcB(key)
};
export const funcB = (key,prop) => {
return someObj;
};
我有我的 helper.spec.js 来测试 helper.js 文件的功能。
import {funcA,funcB} from 'helper';
describe('helper', () => {
test('testFuncB', () => {
}
test('testFuncA', () => {
}
}
funcB 的测试非常简单,我只是调用它并期待 someObj
问题是测试funcA,为了测试它我想模拟funcB的响应。
我希望 testFuncB 调用实际的 funcB 和 testFuncA 调用模拟的 funcB
如何在我的两个测试中实现 funcB 被模拟和原创?
这不是重复的。这是一个不同的情况:他们只模拟内部调用的函数,如果我删除 testFuncB 那么它将是相同的,但我也必须对 testFuncB 执行测试。
【问题讨论】:
-
答案只是我问题的一部分。更新了问题。
-
您可以尝试仅在 testFuncA 内部模拟 funcB。
-
我该怎么做?有代码示例吗?
标签: javascript jestjs