相关文章推荐
正直的手电筒  ·  在c#中获取上个月的第一天和最后一天的日期 ...·  11 月前    · 
稳重的钥匙扣  ·  qt中实现sftp功能 - CSDN文库·  1 年前    · 
瘦瘦的竹笋  ·  绘制简单的CAD样式网格系统 - CSDN文库·  1 年前    · 
千年单身的手套  ·  excel怎么设置左侧固定,右侧能滚动-掘金·  1 年前    · 
销魂的钥匙扣  ·  富文本编辑器 CKeditor 配置使用 ...·  1 年前    · 
Code  ›  javascript - 出现错误“错误:超时超过 2000 毫秒。对于异步测试和挂钩,确保调用了“done()”;” -
毫秒
https://segmentfault.com/q/1010000043074172
帅呆的金鱼
2 年前
segmentfault segmentfault
注册登录
问答 博客 标签 活动
发现
✓ 使用“Bing”搜本站 使用“Google”搜本站 使用“百度”搜本站 站内搜索
注册登录
  1. 首页
  2. 问答
  3. Stack Overflow 翻译
  4. 问答详情

出现错误“错误:超时超过 2000 毫秒。对于异步测试和挂钩,确保调用了“done()”;”

社区维基
1
发布于
2022-12-14
新手上路,请多包涵

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

这是 code 用于测试亚马逊 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);;

谁能解释一下发生了什么

原文由 made_in_india 发布,翻译遵循 CC BY-SA 4.0 许可协议

Stack Overflow 翻译 javascript node.js mocha.js chai
阅读 245
2 个回答
得票 最新
社区维基
1
发布于
2022-12-14
✓ 已被采纳

您的测试花费的时间比 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() :

 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

原文由 peteb 发布,翻译遵循 CC BY-SA 3.0 许可协议

社区维基
1
发布于
2022-12-14

帮助将 .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);
 
推荐文章
正直的手电筒  ·  在c#中获取上个月的第一天和最后一天的日期 - 腾讯云开发者社区 - 腾讯云
11 月前
稳重的钥匙扣  ·  qt中实现sftp功能 - CSDN文库
1 年前
瘦瘦的竹笋  ·  绘制简单的CAD样式网格系统 - CSDN文库
1 年前
千年单身的手套  ·  excel怎么设置左侧固定,右侧能滚动-掘金
1 年前
销魂的钥匙扣  ·  富文本编辑器 CKeditor 配置使用 (带附件) - menu520 - 博客园
1 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号