相关文章推荐
坏坏的红茶  ·  java打印出当前时间的毫秒、微秒、纳秒 ...·  3 月前    · 
暴走的小熊猫  ·  测试踩坑——JAVA13位时间戳处理,别小瞧 ...·  3 月前    · 
悲伤的甜瓜  ·  HarmonyOS如何获取毫秒级时间戳?_编 ...·  3 月前    · 
坚强的大熊猫  ·  針對顯示兩部伺服器之間不同效能的查詢進行疑難 ...·  2 周前    · 
豁达的木耳  ·  将增量刷新与数据流配合使用 - Power ...·  1 年前    · 
行走的蘑菇  ·  使用C++编写Python扩展 - ...·  2 年前    · 
稳重的警车  ·  left join on ...·  2 年前    · 
长情的弓箭  ·  再写if-else,就把你消灭-虎嗅网·  2 年前    · 
会搭讪的大葱  ·  AnalyticDBMySQL支持的字符串函 ...·  2 年前    · 
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);
 
推荐文章
坏坏的红茶  ·  java打印出当前时间的毫秒、微秒、纳秒 - jarsing
3 月前
暴走的小熊猫  ·  测试踩坑——JAVA13位时间戳处理,别小瞧了“毫秒“老弟
3 月前
悲伤的甜瓜  ·  HarmonyOS如何获取毫秒级时间戳?_编程语言-CSDN问答
3 月前
坚强的大熊猫  ·  針對顯示兩部伺服器之間不同效能的查詢進行疑難解答 - SQL Server | Microsoft Learn
2 周前
豁达的木耳  ·  将增量刷新与数据流配合使用 - Power Query | Microsoft Learn
1 年前
行走的蘑菇  ·  使用C++编写Python扩展 - AdrianCharles - 博客园
2 年前
稳重的警车  ·  left join on 和where条件的放置_left join on 加条件和where加条件-CSDN博客
2 年前
长情的弓箭  ·  再写if-else,就把你消灭-虎嗅网
2 年前
会搭讪的大葱  ·  AnalyticDBMySQL支持的字符串函数_云原生数据仓库AnalyticDB MySQL版(AnalyticDB for MySQL)-阿里云帮助中心
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号