Javascript 如何在模拟函数的回调中调用resolve?

Javascript 如何在模拟函数的回调中调用resolve?,javascript,unit-testing,testing,jestjs,Javascript,Unit Testing,Testing,Jestjs,以下是我尝试测试的函数: index.js: 从“第三方”导入第三方; 函数Main(){} Main.prototype.getStuff=函数(){ 返回新承诺((解决、拒绝)=>{ this.getOtherStuff().then((数据)=>{ //业务逻辑。。。 const tpinstance=新的第三方(); tpinstance.createThing().nestedFunction(null,()=>{ //这就是我解析外部函数的地方 解析({newdata:goodstu

以下是我尝试测试的函数:

index.js

从“第三方”导入第三方;
函数Main(){}
Main.prototype.getStuff=函数(){
返回新承诺((解决、拒绝)=>{
this.getOtherStuff().then((数据)=>{
//业务逻辑。。。
const tpinstance=新的第三方();
tpinstance.createThing().nestedFunction(null,()=>{
//这就是我解析外部函数的地方
解析({newdata:goodstuff});
});
});
}
}
Main.prototype.getOtherStuff=函数(){
返回新承诺((解决、拒绝)=>{
解决();
})
}
我无法解析来自最外层函数的承诺,因此出现以下错误:

超时-在jest.setTimeout.Error指定的5000ms超时内未调用异步回调:

我的测试

//index.spec.js
import ThirdParty from 'third-party';

jest.mock('third-party');

describe('Main', () => {
   describe('#getStuff', () => {
      test('Want this to pass', async () => {

         jest
           .spyOn(Main.prototype, "getOtherStuff")
           .mockImplementationOnce(() => Promise.resolve({ data: "value" }));

         const mockedThing = {
            // This implementation seems wrong to me.
            nestedFunction: jest.fn().mockImplementation(() => Promise.resolve())
         }

         jest
           .spyOn(ThirdParty.prototype, "createThing")
           .mockImplementation(() => (mockedThing))

         let instance = new Main();
         await instance.getStuff();

         //assertions -> I never get here cause it timesout
         expect(Main.prototype.getOtherStuff).toHaveBeenCalled();
      });
   });
});

如何模拟
nestedFunction
,在我传递给
nestedFunction
的回调中解析外部函数?

以下是单元测试解决方案:

index.js

从“/第三方”导入第三方;
导出默认函数Main(){}
Main.prototype.getStuff=函数(){
返回新承诺((解决、拒绝)=>{
this.getOtherStuff().then(数据=>{
const tpinstance=新的第三方();
tpinstance.createThing().nestedFunction(null,()=>{
const goodstuff=数据;
解析({newdata:goodstuff});
});
});
});
};
Main.prototype.getOtherStuff=函数(){
返回新承诺((解决、拒绝)=>{
解决();
});
};
第三方.js

导出默认函数ThirdParty(){}
ThirdParty.prototype.createThing=函数(){
log(“真正创建的东西”);
归还这个;
};
ThirdParty.prototype.nestedFunction=函数(arg,cb){
log(“实嵌套函数”);
};
index.spec.js

从“/”导入Main;
从“/第三方”导入第三方;
jest.mock(“./third-party.js”,()=>{
常量MTHIRDPATH={
createThing:jest.fn().mockReturnThis(),
nestedFunction:jest.fn()
};
return jest.fn(()=>mThirdParth);
});
描述(“主”,()=>{
描述(“#getStuff”,()=>{
之后(()=>{
开玩笑。恢复记忆();
jest.resetAllMocks();
});
它(“应该通过”,异步()=>{
开玩笑
.spyOn(Main.prototype,“getOtherStuff”)
.mockResolvedValueOnce({data:“value”});
让我们回拨;
const tpinstance=新的第三方();
tpinstance.createThing().nestedFunction.mockImplementation((arg,cb)=>{
回调=cb;
cb();
});
const instance=new Main();
const pending=instance.getStuff();
控制台日志(待定);
const actual=等待挂起;
expect(实际).toEqual({newdata:{data:{value:}});
expect(Main.prototype.getOtherStuff).toBeCalledTimes(1);
expect(tpinstance.createThing).tohaveBeenCall();
expect(tpinstance.createThing().nestedFunction).toBeCalledWith(
无效的
回拨
);
});
});
});
单元测试结果和覆盖率报告:

PASS src/stackoverflow/59150545/index.spec.js
主要
#getStuff
✓ 应通过(12毫秒)
console.log src/stackoverflow/59150545/index.spec.js:31
承诺{}
----------|----------|----------|----------|----------|-------------------|
文件|%Stmts |%Branch |%Funcs |%Line |未覆盖行|s|
----------|----------|----------|----------|----------|-------------------|
所有文件| 84.62 | 100 | 71.43 | 83.33 ||
index.js | 84.62 | 100 | 71.43 | 83.33 | 18,19|
----------|----------|----------|----------|----------|-------------------|
测试套件:1个通过,共1个
测试:1项通过,共1项
快照:共0个
时间:3.529秒
源代码: