jcubic
jcubic

Reputation: 66490

expect in Promise based then function in jest not failing

So I've spend few minutes debugging my jest test and wondering why it was not failing and it seems that jest is using exceptions thrown by expect to handle failures and somehow my function don't throw unhandled exception like it should:

this is my test that pass:

    fit('should evalute let over lambda', function() {
        lips.exec(`(define x 10) (* x x)`).then(result => {
            expect(result).toEqual([undefined, 20]);
        }) //.catch(e => console.log(e.message));
    });

and the console.log in catch prints that's the code was failing but summary says that all tests pass.

Here is my exec function, is everything ok with it?

function exec(string, env, dynamic_scope) {
    if (env === true) {
        env = dynamic_scope = global_env;
    } else {
        env = env || global_env;
    }
    var list = parse(tokenize(string));
    return new Promise((resolve, reject) => {
        var results = [];
        (function recur() {
            function next(value) {
                console.log('next');
                results.push(value);
                recur();
            }
            var code = list.shift();
            if (!code) {
                resolve(results);
            } else {
                try {
                    var result = evaluate(code, env, dynamic_scope);
                } catch (e) {
                    return reject(e);
                }
                if (result instanceof Promise) {
                    console.log('promise');
                    result.then(next).catch(reject);
                } else {
                    next(result);
                }
            }
        })();
    });
}

the promise is resolved. the expect is executed it throw exception but it not propagate.

Upvotes: 0

Views: 550

Answers (1)

SLaks
SLaks

Reputation: 887433

You need to return your promise chain from the test so the test runner can see its result (and wait for it to finish).

Upvotes: 2

Related Questions