Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
Test partial object properties with Chai's Expect interface

I have an object with many properties and want to check some of them. But one of the fields is a random string so that cannot be checked for an exact match.

const expectedObject = {
  a: 'A',
  b: 123,
  c: ['x', 'y', 'z'],
  random: 'dsfgshdfsg'
}

Checking with eql doesn't work due to the random field.

expect(result).to.eql(expectedObject)

Is there a similar method where I can provide an object with only the fields I want to test?

The alternative is to delete the random field for the test, but this is a bit cumbersome.

const resultWithKnownFields = {...result}
delete resultWithKnownFields.random
const expectedObject = {
  a: 'A',
  b: 123,
  c: ['x', 'y', 'z']
}
expect(resultWithKnownFields).to.eql(expectedObject)
expect(result.random.length).to.equal(10)
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

.containSubset() method of chai-subset plugin. Partial matching can be performed on the fields in the object.

const { expect } = require('chai');
const chai = require('chai');
const chaiSubset = require('chai-subset');
chai.use(chaiSubset);

describe('69555042', () => {
  it('should pass', () => {
    const expectedObject = {
      a: 'A',
      b: 123,
      c: ['x', 'y', 'z'],
    };
    const result = {
      a: 'A',
      b: 123,
      c: ['x', 'y', 'z'],
      random: 'dsfgshdfsg',
    };
    expect(result).to.containSubset(expectedObject);
    expect(typeof result.random).to.equal('string');
  });
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda