我有一个模块,导出一个类和2个函数,该模块被导入到一个正在测试的文件中。
someFile.js
const {theclass, thefunction} = require("theModule");
const getSomeFileData = () => {
let obj = new theclass();
//some logic
return obj.getData();
}
在测试文件中,我想要模拟模块“TheModule”,并在调用函数obj.getData()时返回一个已知值。在测试文件“somefile.js”时,我如何去模仿这个模块(“themodule”)呢?
您可以创建__mocks__/themodule.js
文件并在此描述您的模拟,如下所示:
const cn = {};
cn.theclass = function () {
this.getData = function () {
return 1;
};
};
module.exports = cn;