Create an Assert test with dynamic data

I’m learning tests with Adonis and I was wondering if it’s possible to use only the KEYS of JSON when using assertJSONSubset

   const response = await client.get('/pimbas')
   .header('Content-Type', 'application/json')
   .header('Accept', 'application/json')
   .header('Authorization', 'Bearer + jwt tokenXXX')
   .send({
      field1: false,
      field2: false
   }).end()
   
   response.assertJSONSubset({ 
       // HERE I WANT TO VERIFY IF THE RETURN WAS A VALID JSON, using only the KEYS.  
      // I know what will be the return (JSON structure), but not the values.
   })
})  

Documentaion says:

I don’t get it. It says “helpful when values inside an object are NOT determinable”. this is what I Need! But the example is declaring a value for each key. What if I can’t predict the values? Only test if it’s a JSON is the only solution?

2 Likes

Hi @Ghaleon :wave:

assertJSONSubset is when you don’t know all the keys. In the example, title and body are defined and must exist. The response can contain other values { title: ..., body: ..., data: "new entry" }. Like you said, this is not what you want.

Chai JS has a method that might be what you need : containsAllKeys()

...
async ({ assert }) => {
   ...
   assert.containsAllKeys({ status: true, title: "title" }, ['status']); // Success
}

or hasAllKeys()

Welcome to our community :grinning:

2 Likes