如何从Async函数调用Async函数?

问题描述 投票:2回答:1

我有这个Jest测试,它调用一个称为getLD的异步函数,该函数本身就是一个异步函数:

test("Should generate correct lexical density", async () => {
    var ld = await getLD("John is a Smith")
    console.log(ld);
    expect(ld).toEqual('Paul');
    expect.assertions(1);
})

这是它调用的异步函数:

const NonLexWord = require('../models/Word');
exports.getLD = async (sentence) => {
    const nonLexicalWords = await NonLexWord.find({});
    // console.log(nonLexicalWords);
    const words = sentence.split(" ");
    const totalNumberOfWords = words.length;
    let totalNumberOfLexicalWords = 0;
    words.forEach(word => {
        const found = nonLexicalWords.find(element => element.name === word);
        if(!found) {
            totalNumberOfLexicalWords++;
        }
    });
    return parseFloat((totalNumberOfLexicalWords / totalNumberOfWords).toFixed(2));
}

我的问题是测试主体从不运行,并且收到此错误:

:超时-在5000毫秒内未调用异步回调jest.setTimeout.Timeout指定的超时-异步回调不是在jest.setTimeout.Error指定的5000ms超时内调用:

这是Word模型:

// For non lexical words
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// Create Schema
const WordSchema = new Schema({
    name: {
        type: String,
    }
});

module.exports = Word = mongoose.model('word', WordSchema);

当然,我确实尝试过增加this之类的时间阈值。

javascript mongoose promise async-await jestjs
1个回答
0
投票

test("Should generate correct lexical density", async () => {
    var ld = await getLD("John is a Smith")
    console.log(ld);
    expect(ld).toEqual('Paul');
    expect.assertions(1);
}())

你不能那样做吗?

© www.soinside.com 2019 - 2024. All rights reserved.