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

Cannot find element in JSON body #6626

Closed
FransPeterson opened this issue Mar 3, 2020 · 3 comments
Closed

Cannot find element in JSON body #6626

FransPeterson opened this issue Mar 3, 2020 · 3 comments
Labels
stage: wontfix Cypress does not regard this as an issue or will not implement this feature v4.0.0 🐛 Issue present since 4.0.0

Comments

@FransPeterson
Copy link

FransPeterson commented Mar 3, 2020

I noticed that a certain assertion was failing when upgrading from version 3.8.3 towards 4.1.0. I downgraded the version back towards 3.8.3 and it's passing again.

Current behavior:

image

Desired behavior:

Assertion should pass.

Test code to reproduce

describe('API Testing with Cypress', () => {

  var baseURL = "https://pokeapi.co/api/v2/pokemon"

  beforeEach(() => {
      cy.request(baseURL+"/25").as('pikachu');
  });

  it('Print response body',() => {
    cy.get('@pikachu')
    .its('body')
    .then((apivalue) => {
          cy.log(apivalue)
        })
  });

  it('Validate the header', () => {
      cy.get('@pikachu')
          .its('headers')
          .its('content-type')
          .should('include', 'application/json; charset=utf-8');
  });

  it('Validate the status code', () => {
      cy.get('@pikachu')
          .its('status')
          .should('equal', 200);
  });

  it("Validate the pokemon's name", () => {
    cy.get("@pikachu") 
      .its('body')
      .its('forms')
      .should('include', { 
        name: 'pikachu', 
        url: 'https://pokeapi.co/api/v2/pokemon-form/25/' 
      })
      .should('not.include', {
        name: 'johndoe'
      });
  })

Versions

Working in 3.8.3
Not working in 4.1.0

@jennifer-shehane
Copy link
Member

I can confirm this change in behavior starting in 4.0.0.

This expected behavior and is due to our upgrade of Chai from 3.5.0 to 4.2.0 which included breaking changes. Specifically:

Previously, .include was using strict equality (===) for non-negated property inclusion, but deep equality for negated property inclusion and array inclusion.

This upgrade causes include to always use strict equality unless the deep property is set.

There's a bit of a deeper explanation here, but essentially in 3.x, a different equality check was being applied for not.include versus include, which is now fixed

3.8.3

Screen Shot 2020-03-04 at 3 09 37 PM

4.0.0

Screen Shot 2020-03-04 at 3 21 34 PM

To fix:

Update your assertion to use deep.include for deep equality and include for strict equality. The below code passes in 4.0.0, for example.

it("Validate the pokemon's name", () => {
  cy.request('https://pokeapi.co/api/v2/pokemon/25').as('pikachu');
  cy.get("@pikachu")
    .its('body')
    .its('forms')
    .should('deep.include', {
      name: 'pikachu',
      url: 'https://pokeapi.co/api/v2/pokemon-form/25/'
    })
})

@jennifer-shehane jennifer-shehane added v4.0.0 🐛 Issue present since 4.0.0 stage: wontfix Cypress does not regard this as an issue or will not implement this feature labels Mar 4, 2020
@jennifer-shehane
Copy link
Member

I've opened a PR in our docs to more specifically note this in our Migration Guide cypress-io/cypress-documentation#2574

@FransPeterson
Copy link
Author

Thanks for the information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: wontfix Cypress does not regard this as an issue or will not implement this feature v4.0.0 🐛 Issue present since 4.0.0
Projects
None yet
Development

No branches or pull requests

2 participants