Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Timeout of 2000ms exceeded #2958

Closed
cemiltokatli opened this issue Aug 13, 2017 · 18 comments
Closed

Error: Timeout of 2000ms exceeded #2958

cemiltokatli opened this issue Aug 13, 2017 · 18 comments
Labels
status: waiting for author waiting on response from OP - more information needed type: question support question

Comments

@cemiltokatli
Copy link

When I try to use promises with mocha, I'm getting the error below:

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

Actually, I'm trying to retireve data from a remote server. Mocha won't wait until the promise has been resolved or rejected.


function fn(){
  return new Promise((resolve, reject) => {
       setTimeout(() => resolve({foo:'bar'}),7000);
  })
}

 it('should retrieve data',function(done){
            fn().then((res) => done()).catch((err) => done(err));
 });

After 2 seconds, this error occurs.
I've been planning to check the returning data within then and call the assert.

it('should retrieve data',function(done){
   fn().then(() => {
       assert(true)
       done()
   });
});

What am I doing wrong? I want to check the returning data if it has proper keys, etc.

@ScottFreeCode
Copy link
Contributor

ScottFreeCode commented Aug 13, 2017

If it is expected/acceptable for your test to take longer than the timeout specified in the error message, you can override the amount of time for the timeout using this.timeout(<maximum acceptable time>) or --timeout <maximum acceptable time>.

P.S. You can remove done from the tests and return fn().then(... instead, Mocha will treat promise resolution as success and rejection as failure.

@Jo-ravar
Copy link

Jo-ravar commented Sep 12, 2017

Facing the same issue when using chai-http and expect

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

I was planning to run the check if function called is function or not and have not used chai-http any where within test .

Code :--

const app = require('../../app');
const chai = require('chai');
const expect = require('chai').expect;
const chaiHttp = require('chai-http');
const async = require('async');

const sellerPurchase = require('../../lib/seller/purchases');

chai.use(chaiHttp);
describe('SELLER: PURCHASE module', () => {
  describe('"#insertNewOrder()"', () => {
    // 1. Checks whether the variable exported from that file is a function itself or not
    it('should be a function', done => {
      this.timeout(10000);
      expect(sellerPurchase.insertNewOrder).to.be.a('function');
      done();
    });
  });
});

Now the thing is when I am not loading server file app or const app = = require('../../app'); this is working fine but when i am loading it is giving the error.

Note :-- I have tried increasing timeout still its not working.

@ScottFreeCode
Copy link
Contributor

@Jo-ravar I can't really debug that since I don't have your ../../app or ../../lib/seller/purchases. Do you think you could trim down those two modules to the simplest they can be that will still cause the issue, and then post the simplified version? (There are no obvious problems with the code you have posted. In fact, given that it's a synchronous test and all it does is have an assertion check that a property is a function, you'd have to abuse some relatively advanced/hacky JavaScript techniques to slow it down at all, let alone cause it to time out. But the only way to figure out for sure is to have all the code needed to run it and get the issue.)

@Jo-ravar
Copy link

Jo-ravar commented Sep 13, 2017

Here is the simplified version of code

const app = require('../../app');
const chai = require('chai');
const expect = require('chai').expect;
const chaiHttp = require('chai-http');
chai.use(chaiHttp);

describe('"#Check Sum"', function() {
  this.timeout(10000);
  // 1. Checks whether the sum is number or not
  it('should be a number', done => {
    expect(2 + 3).to.be.a('number');
    done();
  });
});

The only thing causing issue is the first line here whenever I am loading the server or app.js file it is giving error and when I am not loading it is working fine.

Note :---

  1. I have tried out timeout function .
  2. I have also tried it after removing arrow function has described in Mocha Arrow Issue . Then too it is not working.

@ScottFreeCode
Copy link
Contributor

I'm still not seeing the code for the app.js file?

@boneskull boneskull added the status: waiting for author waiting on response from OP - more information needed label Oct 6, 2017
@inboxdarpan
Copy link

@Jo-ravar How much timeout did you set? I was testing one upload function, and it depended on size of file I was trying to upload. So, sometimes it didn't give me error on 10 seconds, while for some uploads it even gave me failures for 45 second timeout.

@Pravee08
Copy link

@Jo-ravar i am too getting the same error , have you found the solution?

@amoswachanga
Copy link

Any resolution on this?

@boneskull
Copy link
Contributor

This needs an mcve; I don't believe it's a Mocha problem anyway. Re-open if someone can confirm.

@negarineh
Copy link

I have found the solution for my case here #2025

@mochajs mochajs deleted a comment from selvakumar1994 Aug 29, 2018
@yihaoye
Copy link

yihaoye commented Jun 9, 2019

For the people who still struggling with this problem, just add this.enableTimeouts(false) may solve it. Example:

describe('Test', function () {
  this.enableTimeouts(false)
  // your code here
}

@ldonjibson
Copy link

You can may also try to run the code last. I had the same issue but was able to by pass it using:
after('Test', (done) => { //your code });

@solaar14
Copy link

go here https://mochajs.org/#timeouts

@leonelRos
Copy link

Hey, guys, I was running through the same error. after carefully searching I found out I need to call the "done()" function. It was given some trouble but now it passes the test.

describe('sample_tests', function () {
// this.timeout(10000)
it('test 0', (done) => {
expect(1).to.be.equal(1);
---> done();
});
});

@fitorec
Copy link

fitorec commented Sep 21, 2020

:neckbeard: Other option, you can use de TIMEOUTS suite, check this example:

describe('a suite of 7 seconds', function () {
  this.timeout(7500);
  it('should take less than 5s', function (done) {
    setTimeout(done, 5000);
  });

  it('should take less than 2s as well', function (done) {
    setTimeout(done, 2000);
  });
});

More info: 👉 https://mochajs.org/#timeouts

@ju4n23
Copy link

ju4n23 commented Apr 24, 2021

I am also having the same error. Can anybody help me? This is my code:

import mongoose from 'mongoose'
import AuthRepository from '../../../../src/auth/data/repository/AuthRepository'
import dotenv from 'dotenv'
import { expect } from 'chai'

dotenv.config()

describe ('AuthRepository', () => {
let client : mongoose.Mongoose
let sut: AuthRepository

beforeEach(() => {
    client = new mongoose.Mongoose()
    const connectionStr = encodeURI(process.env.TEST_DB as string)
    client.connect(connectionStr, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })

    sut = new AuthRepository(client)
})

afterEach(() => {
    client.disconnect()
})

it('should return user when email is found', async () => {
    const email = 'juansantiago@gmole.com'
    const password = 'pass'

    const result = await sut.find(email)

    expect(result).to.not.be.empty
    
})

})

@SomeJoker
Copy link

SomeJoker commented Jan 15, 2022

Here is the simplified version of code

const app = require('../../app');
const chai = require('chai');
const expect = require('chai').expect;
const chaiHttp = require('chai-http');
chai.use(chaiHttp);

describe('"#Check Sum"', function() {
  this.timeout(10000);
  // 1. Checks whether the sum is number or not
  it('should be a number', done => {
    expect(2 + 3).to.be.a('number');
    done();
  });
});

The only thing causing issue is the first line here whenever I am loading the server or app.js file it is giving error and when I am not loading it is working fine.

Note :---

  1. I have tried out timeout function .
  2. I have also tried it after removing arrow function has described in Mocha Arrow Issue . Then too it is not working.

I used it solve my question. just add a line this.timeout(10000) in first line

@liudonghua123
Copy link

liudonghua123 commented Feb 23, 2023

I have the same problems, I also need to rewrite the anonymous arrow function to normal function.

image

image

However, I still got the following errors.

SError: ⨯ Unable to compile TypeScript:
test/index.spec.ts:7:5 - error TS2532: Object is possibly 'undefined'.

7     this.timeout(0);
      ~~~~

    at createTSError (D:\code\node\ids_api_demo\node_modules\ts-node\src\index.ts:859:12)
    at reportTSError (D:\code\node\ids_api_demo\node_modules\ts-node\src\index.ts:863:19)
    at getOutput (D:\code\node\ids_api_demo\node_modules\ts-node\src\index.ts:1077:36)
    at Object.compile (D:\code\node\ids_api_demo\node_modules\ts-node\src\index.ts:1433:41)
    at Module.m._compile (D:\code\node\ids_api_demo\node_modules\ts-node\src\index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Object.require.extensions.<computed> [as .ts] (D:\code\node\ids_api_demo\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.exports.requireOrImport (D:\code\node\ids_api_demo\node_modules\mocha\lib\nodejs\esm-utils.js:53:16)
    at async Object.exports.loadFilesAsync (D:\code\node\ids_api_demo\node_modules\mocha\lib\nodejs\esm-utils.js:100:20)
    at async singleRun (D:\code\node\ids_api_demo\node_modules\mocha\lib\cli\run-helpers.js:125:3)
                                         ~~~~~~~~
    An outer value of 'this' is shadowed by this container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting for author waiting on response from OP - more information needed type: question support question
Projects
None yet
Development

No branches or pull requests