Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocha change timeout for afterEach

I am writing a node application with mocha and chai. Some of the tests call an external API for integration tests, which might take up to 90sec to perform the action.

In order to cleanup properly, I defined an afterEach()-block, which will delete any generated remote resources, in case an expect fails and some resources weren't deleted.

The tests themselves have an increased timeout, while the rest of the tests should retain their default and small timeout:

it('should create remote resource', () => {...}).timeout(120000)

However, I can't do the same with afterEach().timeout(120000), because the function does not exist - nor can I use the function ()-notation due to the unknown resource names:

describe('Resources', function () {
  beforeEach(() => {
    this._resources = null
  })

  it('should create resources', async () => {
    this._resources = await createResources()
    expect(false).to.equal(true)   // fail test, so...
    await deleteResources()        // will never be called
  })

  afterEach(async() => {
    this._resources.map(entry => {
      await // ... delete any created resources ...
    })
  }).timeout(120000)
})

Any hints? Mocha is version 4.0.1, chai is 4.1.2

like image 941
Lars Avatar asked Nov 14 '17 09:11

Lars


People also ask

How do I change the timeout on my mocha?

Whenever you run Mocha at the command line, it will read this file and set a timeout of 5 seconds by default. Another way which may be better depending on your situation is to set it like this in a top level describe call in your test file: describe("something", function () { this. timeout(5000); // tests... });

What is Mocha done?

Asynchronous Mocha Tests To correct this, Mocha can provide a “done” callback method that you can call from the beforeEach call, afterEach call, it calls, and more. This “done” parameter, when present in your callback function, tells Mocha that you are writing an asynchronous test.


2 Answers

The rules are same for all Mocha blocks.

timeout can be set for arrow functions in Mocha 1.x with:

  afterEach((done) => {
    // ...
    done();
  }).timeout(120000);

And for 2.x and higher it, beforeEach, etc. blocks are expected to be regular functions in order to reach spec context. If suite context (describe this) should be reached, it can be assigned to another variable:

describe('...', function () {
  const suite = this;

  before(function () {
    // common suite timeout that doesn't really need to be placed inside before block
    suite.timeout(60000); 
  }); 
  ...
  afterEach(function (done) {
    this.timeout(120000);
    // ...
    done();
  });
});

Mocha contexts are expected to be used like that, since spec context is useful, and there are virtually no good reasons to access suite context inside specs.

And done parameter or promise return are necessary for asynchronous blocks.

like image 119
Estus Flask Avatar answered Nov 14 '22 21:11

Estus Flask


If you need to use dynamic context you have to use normal function.

describe('Resources', function () {
  // ...
  afterEach(function (){
    this.timeout(120000)  // this should work
    // ... delete any created resources ...
  })
})
like image 31
Yury Tarabanko Avatar answered Nov 14 '22 21:11

Yury Tarabanko