Change connection information when testing

Hello !

I have 2 APIs (one for Auth another for the data itself).

I’m testing the search for data (GET), but before that I need to generate a jwtToken on a different API.

How may I CHANGE the port of the request using adonis test? Tried to use some sort of .listen() but didn’t work.

trait('Test/ApiClient')

// Get jwtToken
before(async () => {  
   const jwtToken = await client.post('/my-url')  
   .header('Content-Type', 'application/json')
   .header('Accept', 'application/json').end()    


test('do my test here', async ({client, assert}) => {
     // Execute my test here using jwtToken from above /\
})

How to change the port here? ONLY when I’m executing the tests.
THanks!

2 Likes

Hello,

Update url with full url. Like:

await client.post('http://127.0.0.1:1234/your-url')  
... 
.end()

It’s important that the url is complete (http://<host>:<port>/)

1 Like

Hello. Thanks again <3

I added one more thing to the callback and I’m getting an error now. (added {client}

Code:

// Get jwtToken
before(async ({client}) => {  
   const jwtToken = await client.post('http://127.0.0.1:1234/my-url')  
   .header('Content-Type', 'application/json')
   .header('Accept', 'application/json').end()

ERROR:

1. Get Data
  Error: Method overload, async functions and making use of "done()" is not allowed together
    at Hook._parseError...

Any idea why the {client} is causing this?

1 Like

You’re welcome :smiley:.Can you share all the file test code?

1 Like

Sure!


'use strict'

const { test, trait, before} = use('Test/Suite')('Get Data')

trait('Test/ApiClient')

//Get jwtToken  
before(async ({client}) => {  
   const jwtToken = await client.post('http://127.0.0.1:1234/my-url')
   .header('Content-Type', 'application/json')
   .header('Accept', 'application/json')
   .send({
      field1: 'value1',
      field2: 'value2'
   }).end()
   console.log(jwtToken)
}) 

test('Check if there is any data', async ({ client, assert }) => { 
   //Code commented here 'till I get the token above
})
1 Like

Thanks. After testing I noticed that it is not possible to use client as a function parameter (for before, beforeEach, …).
You have to use superagent manually. Like:

...
const superagent = require("superagent");

before(async () => {

  var req = superagent.get('http://127.0.0.1:1234/my-url');
  req.set("Content-Type", "application/json");
  req.set("Accept", "application/json"); // Change `header` to `set`
  req.query({
    field1: "value1",
    field2: "value2"
  });
  var jwtToken = await req; // Send query

  console.info(jwtToken);
}
...

superagent - documentation

2 Likes

Thanks a bunch.

Which approach you think is “more correct”?

  1. Generate the token inside each test:
test('Validate error message', async({client, assert}) => {
  const jwtResponse = await client.post('http://127.0.0.1:3333/my-url')
    .header('Content-Type', 'application/json')
    .header('Accept', 'application/json')
    .send({
      username: 'abc',
      password: '123'
    }).end()
    
    //use token latter    
})
  1. Use the superagent you showed inside before callback.

I believe the 2nd option would be better, because it would avoid code repetition. BUt I don’t know if there is any sort of “down side” when using superagent.

1 Like

You’re welcome :slight_smile:

Which approach you think is “more correct”?

^ I think the second option would be the most interesting because the before function is used to set up the environment.

BUt I don’t know if there is any sort of “down side” when using superagent.

^ client uses superagent. Of course you have to read the documentation and implement it differently than if you were using client, but the result is the same.
You can also use other libraries (like axios, …)

(It’s a personal opinion. If other people have other suggestions…)

1 Like