How to test by `done` function

I want to test something with callback, I tested with below code but it does not work. Thank you.

test('always false', ({ done }) => {
  setTimeout(() => {
    assert(false);
    done();
  }, 1000);
});

Hi @Sotatek-DucPham

Can you use async / await?

https://adonisjs.com/docs/4.1/testing#_assertions

test('always false', async ({ assert }) => {
  let data = await yourAsyncMethod()
  assert.equal(data, false)
});

Or can you wrap callback into Promise and then use async / await?
Example + tutorial in this blog post

test('always false', async ({ assert }) => {
  const promiseSetTimeout = () => {
    return new Promise(resolve => {
        setTimeout(() => {
          // Some code
          resolve(false)
        }, 1000);
    })
  }
  let data = await promiseSetTimeout()
  assert.equals(data, false)
});

Also going thro Japa docs might be useful https://github.com/thetutlage/japa

1 Like

Thank you. I want to test adonis.js websocket. But it does not work with socket.io library, I tried @adonisjs/websocket-client but it cannot run because export default does not work with node.js. There is a thread about this problem.