繁体   English   中英

开玩笑单元测试 function 调用第二个返回 promise

[英]Jest Unit Testing function that calls a second one that returns a promise

应用了 vazsonyidl 建议的已编辑问题

我必须为类似于此的 function 编写单元测试:

import {External} from 'ExternalModule';

async functionA(){
    this.functionB().then((data) => {
        External.functionC(options);
        console.log("Reached1");

    }).catch((data) => {
        const { OnError = "" } = data || {}
        if(OnError) {
            External.functionC(anotherOptions);
            console.log("Reached2");
        }
    })
}

functionB() {
    return new Promise(() => {
    });
  }

由于 functionC 属于另一个模块,我将它的模拟放在_mocks_文件夹中:

//_mocks_/ExternalModule.ts

export var External: ExternalClass = {
  functionC(){}
}

class ExternalClass{
  constructor(){};
  functionC(){};
}

我以两种不同的方式模拟了 functionB 来测试thencatch

it("should test then block", () => {
        functionB = jest.fn(() => {return Promise.resolve()});

        const functionSpy = jest.spyOn(ExternalModule.External, 'functionC');
        void functionA().then(() => {
          expect(functionSpy).not.toHaveBeenCalled();
        });
    })

it("should test catch block", () => {
        const err = { OnError: "Error" };
        functionB = jest.fn(() => {return Promise.reject(err)});

        const functionSpy = jest.spyOn(ExternalModule.External, 'functionC');
        void functionA().then(() => {
          expect(functionSpy).not.toHaveBeenCalled();
        });
    })

我想要做的是期望 functionC 被调用并使用正确的参数调用,但是即使我测试 functionC 是否没有被调用,测试也总是通过。

我究竟做错了什么?

我认为当这个 function 捕获错误时,这个错误应该有一个 'OnError' 属性,以便函数 C 可以运行。

const { OnError = "" } = data || {}
if(OnError) {
    ExternalClass.functionC(anotherOptions);
}

更改您的响应错误数据以return Promise.reject({OnError: '404'})可能会解决此问题。

因为您没有将它提供给您的 class。 以下代码对我有用:

 class A { async functionA() { this.functionB().then((data) => { this.functionC(); // It woll log aaa here, you need this one. }).catch((data) => { const {OnError = ''} = data || {}; if (OnError) { console.log('onerror'); } }); } functionB() { return new Promise(() => { }); } functionC() { return 2; } } describe('a', () => { it('test', () => { const a = new A(); a.functionB = jest.fn(() => Promise.resolve()); const functionBSpy = jest.spyOn(a, 'functionC'); void a.functionA().then(() => { expect(functionBSpy).toHaveBeenCalledTimes(1); }); }); });

希望这会有所帮助,任何评论表示赞赏。

由于您没有提供有关您的功能 B 的信息,我嘲笑了一些可能适合您的东西

您最初的问题是 Jest 不会等待您的回调解决。 尽管它会进行断言,但即使您的 function 调用稍后发生,Jest 也不会识别它们并说从未发生过调用。

有几个可用的文档,例如 Jest's one here

Jest 在进行断言之前不会等待异步代码完成。

您可以使用以下 function:

const waitForPromises = () => new Promise(setImmediate);

强制 Jest 在继续之前等待承诺完成:


it("does something", async () => {
  promiseCall();
  await waitForPromises();
  expect(something).toBe(something)
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM