Home > Blockchain >  how to equal to object with excluding one field in Cypress test
how to equal to object with excluding one field in Cypress test

Time:10-21

I have an object:

var object_1 = {
     random_id: 'random_id_1';
     name: name;
     surname: surname;
}

and

var object_2 = {
     random_id: 'random_id_2';
     name: 'name';
     surname: 'surname';
}

how to equal this 2 objects with excluding random_id

I tried to do it with plugin Chai:

expect(object_1).excluding('random_id').to.deep.equal(object_2)

but without success.

CodePudding user response:

You can still use Chai in the callback functions for Cypress.

var object_1 = {
     random_id: 'random_id_1';
     name: name;
     surname: surname;
}

var object_2 = {
     random_id: 'random_id_2';
     name: 'name';
     surname: 'surname';
}

cy.wrap(object_1).should((object1) => {
  expect(object1).excluding('random_id').to.deep.equal(object_2);
});

Have you installed the chai-exclude library and configured it correctly?

CodePudding user response:

The way to add chai-exclude is

import chaiExclude from 'chai-exclude';
chai.use(chaiExclude);

var object1 = {
  random_id: 'random_id_1',
  name: 'name',
  surname: 'surname'
}

var object2 = {
  random_id: 'random_id_2',
  name: 'name',
  surname: 'surname'
}

expect(object_1).excluding('random_id').to.deep.equal(object_2)

You can instead use Cypress._.omit()

expect(Cypress._.omit(object_1, ['random_id']))
  .to.deep.equal(Cypress._.omit(object_2, ['random_id']))

Or with destructing

let {random_id, ...obj1} = object_1
cy.then(() => {
  let {random_id, ...obj2} = object_2
  expect(obj1).to.deep.eq(obj2)
})
  • Related