ApiClient trait does not work - ReferenceError: client is not defined


#1

I am trying to write a functional test for an API, my test fail by throwing a ReferenceError. I am trying the example available on official guide.

I couldn’t figure out why this should not work, It looks like a pretty straight forward process. I am using the api version of Adonis and have vow installed. My best guess is somehow the trait is not working.

I am using Adonis 4.x with Vow (1.0.15) installed.

Here’s my code:

'use strict'

const { test, trait } = use('Test/Suite')('Auth')
const Auth = use('App/Models/Auth')

trait('Test/ApiClient')

test('user should be able to login with proper credentials', async ({ assert }) => {

  const response = await client.post('api/v1/login')
    .field('username', 'test')
    .field('password', 'test')
    .end()

  response.assertStatus(200)

})

Below is a subset of my StackTrace:

  1. user should be able to login with proper credentials
  ReferenceError: client is not defined
    at Callable.test [as _fn] (/project/test/functional/auth.spec.js:10:20)

#2

Hi,

You need to pass in the client as it’s shown here

Ex.

test('user should be able to login with proper credentials', async ({ client }) => {
    // logic
})

Hope this helps


#3

I had the same mistake and I totally missed this part:

const { test, trait } = use('Test/Suite')('Post')
...

Found here: https://adonisjs.com/docs/4.1/api-tests#_basic_example

Hope it helps!