簡體   English   中英

在基於Promise的情況下期望,然后在笑話中發揮作用而不會失敗

[英]expect in Promise based then function in jest not failing

因此,我花了幾分鍾來調試我的jest測試,並想知道為什么它沒有失敗,而且看來jest正在使用Expect拋出的異常來處理失敗,並且我的函數不會像應該這樣拋出未處理的異常:

這是我通過的測試:

    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));
    });

並且catch.print中的console.log代碼失敗,但是摘要表明所有測試都通過了。

這是我的exec函數,一切正常嗎?

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);
                }
            }
        })();
    });
}

諾言得以解決。 期望執行時會拋出異常,但不會傳播。

您需要從測試中return您的諾言鏈,以便測試運行器可以查看其結果(並等待其完成)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM