lundi 19 novembre 2018

Frisby.js testing with optional array of objects

How can you use Frisby.js to test an optional array that contains objects? For example, say we have an API call that returns something like this:

{
  "id": "123",
  "type": "A",
  "list": [
      {
         "id": "111",
         "size": 1
      }, 
      {
         "id": "222",
         "size": 2
      }
  ]
}

However, it may also return something like this:

{
  "id": "456",
  "type": "B",
}

Currently, I'm trying:

const frisby = require('frisby');
const Joi = frisby.Joi;

test('myTest', () => {
    return frisby
    .get(myUrl)
    .expect('status', 200)
    .expect('jsonTypes', {
        id: Joi.string().required(),
        type: Joi.string().required().
        list: Joi.array().optional()
    })
    .expect('jsonTypes', 'list.*', {
        id: Joi.string().required(),
        size: Joi.number().required()
    });
});

This won't work, however, since the path (list.*) won't be defined if the list attribute doesn't exist. Any ideas?

Aucun commentaire:

Enregistrer un commentaire