【问题标题】:Testing using hapijs/lab -- done is not a function使用 hapijs/lab 进行测试——完成不是一个函数
【发布时间】:2026-01-08 08:10:02
【问题描述】:

尝试为我的 hapi 服务器编写一些测试。以下代码来自 https://github.com/hapijs/lab/issues/79 ,但它失败了,因为 done 不是函数...

const Code = require('code');
const Lab = require('lab');
const lab = exports.lab = Lab.script();
lab.test('expect an error from a promise', (done) => {
    return new Promise((resolve, reject) => {

        try {
            resolve(2);
        }
        catch (err) {
            reject(err);
        }
    }).then((result) => {

        console.log('5) resolved');
        done(new Error('promise should be rejected and caught'));
    }).catch((error) => {

        console.log('5) rejected, this does not trigger');
        Code.expect(error).to.exist();
        done(error);
    });
});

我还应该导入什么才能调用完成?

Failed tests:

  1) expect an error from a promise:

      done is not a function

【问题讨论】:

    标签: node.js hapijs


    【解决方案1】:

    lab.test 不再返回 done 回调,因为它与 hapi v17 兼容。 Lab 现在使用 async/await 功能,您可以返回 Promise。 示例见此处:lab docs

    【讨论】:

      【解决方案2】:

      "lab": "^14.3.4" 似乎仍然同时支持 donees6。当然,我无法确认是否支持全套 es6 功能,但它满足了我的需求。

      【讨论】: