Search code examples
ava

Assertion error using ava


First my english not good but this test fail for me.

test('get user', async t => {
  let db = t.context.db

  t.is(typeof db.getUser, 'function', 'getUser is a function')

  let user = fixtures.getUser()
  let created = await db.saveUser(user)
  let result = await db.getUser(user.username)

  t.deepEqual(created, result)

  // t.throws(db.getUser('foo'), /not found/)
})

and return this

1 failed

db-test » get user

Test finished, but an assertion is still pending

If I delete the line

t.throws(db.getUser('foo'), /not found/)

The test runs without any problem.

The function tested is this

getUser (username, callback) {
  if (!this.connected) {
    return Promise.reject(new Error('not connected')).asCallback(callback)
  }

  let connection = this.connection
  let db = this.db

  let tasks = co.wrap(function * () {
    let conn = yield connection

    yield r.db(db).table('users').indexWait().run(conn)
    let users = yield r.db(db).table('users').getAll(username, {
      index: 'username'
    }).run(conn)

    let result = null

    try {
      result = yield users.next()
    } catch (e) {
      return Promise.reject(new Error(`user ${username} not found`))
    }

    return Promise.resolve(result)
  })

  return Promise.resolve(tasks()).asCallback(callback)
}

What the function does is that if it does not get the user in the database then it returns a reject promise and it actually happens but then after that I do not know what happens


Solution

  • I assume db.getUser('foo') returns a Promise, so you need to await the t.throws assertion:

    await t.throws(db.getUser('foo'), /not found/)