Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.expect('jsonTypes', '*', {}) does not allow an empty array #462

Open
koooge opened this issue Apr 16, 2018 · 2 comments
Open

.expect('jsonTypes', '*', {}) does not allow an empty array #462

koooge opened this issue Apr 16, 2018 · 2 comments

Comments

@koooge
Copy link
Collaborator

koooge commented Apr 16, 2018

Hi there. 😸
.expect('jsonTypes', '*', {}) does not allow an empty array. But API which returns array of objects usually returns [] even when the array has no object.
So, I think .expect('jsonTypes', '*', {}) should allow [].
what do you think about this?

test code

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

it('should pass', (done) => {
  frisby.fromJSON([])
    .inspectBody()
    .expect('jsonTypes', '*', {
      foo: Joi.string()
    })
    .done(done);
});
> jest

 FAIL  ./hoge.spec.js
  ✕ should pass (25ms)

  ● should pass

    Expected '*' not found (path '*')

      at _.each.segment (node_modules/frisby/src/frisby/utils.js:48:13)
      at arrayEach (node_modules/lodash/lodash.js:508:11)
      at Function.forEach (node_modules/lodash/lodash.js:9334:14)
      at Object.withPath (node_modules/frisby/src/frisby/utils.js:16:5)
      at FrisbySpec.jsonTypes (node_modules/frisby/src/frisby/expects.js:108:11)
      at response (node_modules/frisby/src/frisby/spec.js:376:23)
      at _fetch._fetch.then.response (node_modules/frisby/src/frisby/spec.js:217:34)

  console.log node_modules/frisby/src/frisby/spec.js:326

    Body: []

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total

fyi) my workaround

it('should pass', (done) => {
  frisby.fromJSON([])
    .inspectBody()
    .expect('jsonTypes', Joi.array().items({
      foo: Joi.string()
    })
    .done(done);
});
@koooge koooge changed the title .expect('jsonTypes', '*', {}) does not an empty array .expect('jsonTypes', '*', {}) does not allow an empty array Apr 16, 2018
@vlucas
Copy link
Owner

vlucas commented Apr 16, 2018

The error message could be improved, but technically the test is not wrong. There are no objects that exist when you are trying to run assertions on them.

Also on a side note: It is usually not a good idea to return an array as the root element in an API response, because you can never change it or add any other keys once it is done. I always return an object with a data key instead, like this: { data: [] }. It is much more flexible (I have added a meta key with request dates and info for caching, a links key with pagination URLs for next/prev, etc.).

@H1Gdev
Copy link
Collaborator

H1Gdev commented Apr 19, 2018

This error message outputs if no object exists in path.
(It is not an error message specialized for Array.)

format is

Expected '<SEGMENT>' not found (path '<PATH>')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants