我看到以下使用 mochachai 库作为测试用例。Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
这是用于测试亚马逊 lambda 函数处理程序的 code。(目前,我没有使用 super-test npm 模块)

const expect = require('chai').expect;
const mongoose = require('mongoose');
const CONFIG_DEV_MONGODB = require('../../config/config.dev').DB_CONNECTION_URL;


describe('get the foo', () => {

    let handler;
    before(() => {

        process.env.DEPLOYMENT_STAGE = 'test';
        process.env.DB_CONNECTION_URL = CONFIG_DEV_MONGODB;
        handler = require('./get');
    });

    after(() => {

        if (mongoose.connection) {
            mongoose.connection.close();
        }
    });

    it('validate getFeaturedProducts get request with storeid',function(done){
        //request has the sample data for event
        let request = require('../../test/fixtures/featureProductJSON');
        handler.getFeaturedProducts(request, {} , (error, result) => {

             expect(error).to.be.null;
             expect(result).to.be.not.null;
             done()
         })

    })
});

这是处理程序
module.exports.getFeaturedProducts = function (event, context, callback) {
    ..............
    .......
    mongoConnection.then( function() {
         return callback(null, 'test');
     }, function (error) {

        return return callback(true, null);;
    })
 }

任何人都可以解释发生了什么wne

最佳答案

您的测试花费的时间比 Mocha 预期的要长,并且会超时。默认情况下,所有回调函数在 2000 毫秒后超时。您需要使用 this.timeout() 调整测试套件的超时时间。

您可以在 describe() 的套件级别指定此项:

describe('get the foo', function () {
  this.timeout(10000) // all tests in this suite get 10 seconds before timeout

  // hooks & tests
})

您可以在像 before() 这样的钩子(Hook)中指定它:
describe('get the foo', function() {
    before(function() {
      this.timeout(10000) // 10 second timeout for setup
    })

    // tests
})

您也可以在it()的测试级别执行此操作
describe('get the foo', function () {
  it('validate getFeaturedProducts get request with storeid', function (done) {
    this.timeout(10000) // 10 second timeout only for this test

    // assertions
  })
})

10-06 14:18