如何使用玩笑测试javascript类?

时间:2018-09-18 15:26:15

标签: javascript typescript unit-testing jestjs

尝试使用类的抛出声明异常来测试类,在以下情况和测试承诺链中测试类的正确方法是什么?

main.ts

export class ModuleExecutor {
    public execute(moduleName: string): (param1, param2) => any {
        const self = this;
        return function(params: any, responseCallback: (param: any, param2: any) => any) {
            let _mod;
            let _httpRequest;
            let _params;
            Promise.resolve(getApiModule(self.identity, moduleName))
                .then((mod: ModuleBase < any, any > ) => {
                    _mod = mod;
                    mod.ExecStage = ExecStage.Init;
                    // @ts-ignore - red squiggly for the return mismatch which are resolved in Grunt
                    return mod.init(getHttpModule(self.identity), params);
                });
        }
    }
}

main.spec.ts

  describe("Test promise chaining with Mock functions", () => {
        const myMockFuncExecute = jest.fn((mod: ModuleBase<any, any>) => {
            _mod = mod;
            _mod.ExecStage = ExecStage.Init;
            const ret = _mod.Init(getHttpModule(Identity.node), params);
            Promise.resolve(ret);
        });

        jest.mock("./main.ts");
        const executerClass = require("./main.ts");
        executerClass.mockImplementation(() => {
            return {
                init: myMockFuncExecute
            };
        });

        it('it should get Modulebase', async () => {
            const _result = new executerClass(Identity.node);
            _result.execute("Payments/url").then(myMockFuncExecute())
            .then((module) => {
                const mod = module;
                expect(mod).toEqual(ModuleBase);
            });
        });

    });

0 个答案:

没有答案