Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test async code with mocha using await

How do I test async code with mocha? I wanna use multiple await inside mocha

var assert = require('assert');  async function callAsync1() {   // async stuff }  async function callAsync2() {   return true; }  describe('test', function () {   it('should resolve', async (done) => {       await callAsync1();       let res = await callAsync2();       assert.equal(res, true);       done();       }); }); 

This produces error below:

  1) test        should resolve:      Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.       at Context.it (test.js:8:4) 

If I remove done() I get:

  1) test        should resolve:      Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/tmp/test/test.js) 
like image 709
deathangel908 Avatar asked Oct 04 '18 07:10

deathangel908


People also ask

How do you test async function in Mocha?

Writing Asynchronous Tests with Mocha. To indicate that a test is asynchronous in Mocha, you simply pass a callback as the first argument to the it() method: it('should be asynchronous', function(done) { setTimeout(function() { done(); }, 500); });

How do you test your promise in Mocha?

If a function returns a promise that you want to assert on the result of the function, all you need to do for it to work effectively with Mocha is return the promise in your it block. Then you can assert on the result of the promise in the chained . then of your promise.

How do you test async method?

This illustrates the first lesson from the async/await conceptual model: To test an asynchronous method's behavior, you must observe the task it returns. The best way to do this is to await the task returned from the method under test.


1 Answers

You can return the Promise:

Mocha supports Promises out-of-the-box; You just have to return the Promise to it()'s callback. If that Promise resolves then the test passes otherwise it fails.

Since async functions always implicitly return a Promise you can just do:

async function getFoo() {   return 'foo' }  describe('#getFoo', () => {   it('resolves with foo', () => {     return getFoo().then(result => {       assert.equal(result, 'foo')     })   }) }) 

You don't need done nor async for your it.

If you still insist on using async/await:

async function getFoo() {   return 'foo' }  describe('#getFoo', () => {   it('returns foo', async () => {     const result = await getFoo()     assert.equal(result, 'foo')   }) }) 

In either case, do not declare done as an argument

If you use any of the methods described above you need to remove done completely from your code. Passing done as an argument to it() hints to Mocha that you intent to eventually call it.

Using both Promises and done will result in:

Error: Resolution method is overspecified. Specify a callback or return a Promise; not both

The done method is only used for testing callback-based or event-based code.

like image 178
nicholaswmin Avatar answered Sep 28 '22 09:09

nicholaswmin