Search code examples
node.jstestingpromiseasync-awaitava

Test finished without running any assertion (ava-nodejs)


Why does this test return Test finished without running any assertion? Something wrong with promise handling? If i run this code on server everything works properly.

test.serial('test with req' , async (assert) => {
  var promise = new Promise(function(resolve, reject) {
  request('https://www.random.org/integers/?num=1&min=1&max=100&col=5&base=10&format=plain&rnd=new',
    (error, response, body) => {
      console.log(response)
      if(!error){
        resolve(body)
      } else {
        reject(error)
      }
    })
  })

  promise.then((result) => {
    console.log(result);
    assert.true(result !== undefined)
  }, (err) => {
    console.log(err)
    assert.true(err !== undefined)
  })
})

Solution

  • Given request already has promise support, you can rewrite this full test in 2 lines using async / await

    test.serial('test with req' , async (assert) => {
      const result = await request('https://www.random.org/integers/?num=1&min=1&max=100&col=5&base=10&format=plain&rnd=new');
      assert.true(result !== undefined);
    })