簡體   English   中英

摩卡異步測試超時

[英]mocha async test is timing out

嗨,我是 Mocha 測試的新手,更不用說異步測試了。 運行此測試時,我不斷收到以下錯誤。 我花了很多時間在網上研究解決方案,但沒有運氣。

錯誤:超時超過 2000 毫秒。 對於異步測試和掛鈎,確保調用“done()”; 如果返回 Promise,請確保它已解析。

it('Should fail to create new user due to missing email', (done) => {
  const user_empty_email = {
    name: "First Name",
    email: "",
    password: "password",
    isAdmin: false
  }
  chai.request(app).post('/v1/users')
    .send(user_empty_email)
    .then((res) => {
      expect(res).to.have.status(400);
      done();
    }).catch(done)
})

下面是我從 /v1/users 得到的響應示例

{
  "user": {
      "_id": "5de4293d3501dc21d2c5293c",
      "name": "Test Person",
      "email": "testemail@gmail.com",
      "password": "$2a$08$8us1C.thHWsvFw3IRX6o.usskMasZVAyrmccTNBjxpNQ8wrhlBt6q",
      "isAdmin": false,
      "tokens": [
          {
              "_id": "5de4293d3501dc21d2c5293d",
              "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGU0MjkzZDM1MDFkYzIxZDJjNTI5M2MiLCJpYXQiOjE1NzUyMzM4NTN9.mi4YyYcHCvdYrl7OuI5eDwJ8xQyKWDcqgKsXRYtn0kw"
          }
      ],
      "__v": 1
  },
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGU0MjkzZDM1MDFkYzIxZDJjNTI5M2MiLCJpYXQiOjE1NzUyMzM4NTN9.mi4YyYcHCvdYrl7OuI5eDwJ8xQyKWDcqgKsXRYtn0kw"
}

你為什么不嘗試增加超時(毫秒),測試運行緩慢是正常的,尤其是當你的測試網絡請求時。

包.json

"test": "mocha --timeout 10000"

您的端點實際運行時間是否有可能超過 2 秒? 如果是這樣,您可能希望在運行 Mocha 時增加超時: Change default timeout for mocha

另外,您的端點是否返回響應? 如果不是,增加超時將無濟於事。 您能否將/v1/users端點的代碼添加到您的問題中以研究這種可能性?

不確定這一點。 但據我所知,混合使用 Promises 和回調樣式( done -callback)可能會在 mocha 中導致此類問題。

嘗試僅使用 Promises:

  • 從測試中刪除所有done
  • 實際上return Promise( return chai.request...

問題是這個測試: mongoDB-connect.js

它跑在其他人之前。 mongoDB 連接被關閉導致其他測試超時。 當我刪除關閉命令時,所有測試都按預期通過了。

"use strict";
// NPM install mongoose and chai. Make sure mocha is globally
// installed
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const chai = require('chai');
const expect = chai.expect;
// Create a new schema that accepts a 'name' object.
// 'name' is a required field
const testSchema = new Schema({
  name: { type: String, required: true }
});

// mongoose connect options
var options = {
    useNewUrlParser: true,
    useUnifiedTopology: true
}

//Create a new collection called 'Name'
const Name = mongoose.model('Name', testSchema);
describe('Database Tests', function() {
  //Before starting the test, create a sandboxed database connection
  //Once a connection is established invoke done()
  before(function (done) {    
    // mongoose.connect('mongodb://localhost:27017',options);
    mongoose.connect('mongodb://ip/testDatabase',options);
    const db = mongoose.connection;
    db.on('error', console.error.bind(console, 'connection error'));
    db.once('open', function() {
      console.log('We are connected to test database!');
      done();
    });
  });
  describe('Test Database', function() {
    //Save object with 'name' value of 'Mike"
    it('New name saved to test database', function(done) {
      var testName = Name({
        name: 'Mike'
      });

      testName.save(done);
    });
    it('Dont save incorrect format to database', function(done) {
      //Attempt to save with wrong info. An error should trigger
      var wrongSave = Name({
        notName: 'Not Mike'
      });
      wrongSave.save(err => {
        if(err) { return done(); }
        throw new Error('Should generate error!');
      });
    });
    it('Should retrieve data from test database', function(done) {
      //Look up the 'Mike' object previously saved.
      Name.find({name: 'Mike'}, (err, name) => {
        if(err) {throw err;}
        if(name.length === 0) {throw new Error('No data!');}
        done();
      });
    });
  });
  //After all tests are finished drop database and close connection
  after(function(done){
    mongoose.connection.db.dropDatabase(function(){
      mongoose.connection.close(done);
    });
  });
});

暫無
暫無

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

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