0

trying to write unit test using jest it never get into test case where i retruned promise so i can get the response after then.

if there is any better way of writing test cases for promises i would appreciate that input.

executer.ts

export class Executor {
    private passedParam: ILogParams = {} as ILogParams;
    constructor(public identity: Identity) {
        this._ajv = new Ajv();
    }
    public execute(moduleName: string): (param1, param2) => any {
        const self = this;
        // getting rid of the tslint issue with Function
        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;
                    return mod.init(getHttpModule(self.identity), params);
                })
                .then((httpRequest: HttpRequestBase) => {
                    _httpRequest = httpRequest;
                    if (_mod.Response().Summary.Header) {
                        throw _mod.Response().Summary;
                    }
                    return httpRequest;
                })
        };
    }
}

executer.spec.ts

import {ModuleExecutor} from "../../src/ts/common/executor";


    it('should transition with the correct event', (done) => {
                const executer = new ModuleExecutor(Identity.node);
                const executerSpy = executer.execute("Payments/accountBalance/GetAccountBalance");
                new Promise((resolve, reject) => {
                    resolve(true);
                }).then(((mod: ModuleBase<any, any>) => {
                    done();
                }))
                    .catch((error) => {
                        done();
                    });
            });
skyboyer
  • 22,209
  • 7
  • 57
  • 64
hussain
  • 6,587
  • 18
  • 79
  • 152
  • maybe the docs can help - https://jestjs.io/docs/en/asynchronous - I think you have to return the promise `return new Promise...` according to the docs: `Be sure to return the promise - if you omit this return statement, your test will complete before fetchData completes.` – Abdul Ahmad Sep 14 '18 at 18:47
  • 1
    https://stackoverflow.com/questions/43037566/best-way-to-test-promises-in-jest – samnu pel Sep 14 '18 at 18:50
  • @duxfox--can you please provide any example using my code its been hours struggling with this issue – hussain Sep 14 '18 at 18:55

0 Answers0