'Converting a mocha "done()" test to "async" when "done()" is called in an inner function

I would like to add a call to a sync function to the test below:

describe('the server', function() {
  it('should respond to a udp packet', function(done) {

    // TODO: call async function here

    const udp = dgram.createSocket('udp4');

    udp.on('error', function(error) {
      console.log(error);
    });

    udp.on('message', function(msg, rinfo) {
      console.log('msg', msg);
      console.log('rinfo', rinfo);
      // TODO: check the reply
      udp.close();
      done();
    });
    udp.bind();

    // send a request
    const bytes = Buffer.from("421a0800117860bc457f0100001a0653455256455222054d54303031", 'hex');
    udp.send(bytes, SERVER_PORT, SERVER_ADDR)
  });
});

If I just add async, to make:

it('should respond to a udp packet', async function(done) { 

I get an error:

1) the server
     should respond to a udp packet:
   Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both.

but I need the done as the test is only over when a packet is received from the server.

Is there a way of only "returning" from an async function from within an inner function?



Solution 1:[1]

You can also use mocha --exit which is far simpler than cleaning up after yourself.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 fadedbee