代码之家  ›  专栏  ›  技术社区  ›  made_in_india

Getting error“错误:超时超过2000ms。对于异步测试和挂钩,请确保调用了“done()”

  •  5
  • made_in_india  · 技术社区  · 7 年前

    我看到以下使用 mocha chai 测试用例库。 Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

    这里是 code 用于测试amazon lambda函数的处理程序。(目前,我没有使用 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);;
        })
     }
    

    有人能解释一下发生了什么事吗

    3 回复  |  直到 2 年前
        1
  •  14
  •   peteb    7 年前

    您的测试时间比Mocha预计的要长,并且超时。默认情况下,所有回调函数在2000ms后超时。您需要使用 this.timeout() .

    您可以在 describe() :

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

    可以在如下钩子中指定 before() :

    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
      })
    })
    
        2
  •  10
  •   Alexander Tunick    6 年前

    帮助添加 .timeout(10000) 到函数末尾 it()

    describe('Reverse String Test', () => {
    
     it('Compare first data',  async function () {
         try {
            await sql.connect(config);
    
            let request = await new sql.Request()
             .query('SELECT count(*) FROM dbo.valJob');
                 console.log(request);
            await sql.close();
    
        } catch (err) {
            console.log(err)
        }
    
     }).timeout(10000);
    });
    
        3
  •  4
  •   Ikram Ud Daula    6 年前

    另一种防止这种情况的方法。您只需修改 Mocha 默认超时 2000 ms收件人 10000 太太 添加 --timeout 在您的 package.json 例如:

    "scripts": {
       // rest of your scripts
       "test": "mocha server/**/*.test.js --timeout 10000"
    },