Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

495
Visualizações
How can I test part of object using Jest?

I would like to test that time is parsed correctly and I am only interested in checking some of the properties and not the entire object. In this case hour and minutes.

I tried using expect(object).toContain(value) but as you can see in the snippet below it fails although the object contains the properties I am interested in and they have the correct value.

● Calendar > CalendarViewConfig › it should parse time

expect(object).toContain(value)

Expected object:
  {"display": "12:54", "full": 774, "hour": 12, "hours": 12, "minutes": 54, "string": "12:54"}
To contain value:
  {"hours": 12, "minutes": 54}

  67 |   it('it should parse time', () => {
  68 |     ...
> 69 |     expect(parseTime('12:54')).toContain({ hours: 12, minutes: 54})
  70 |   })

  at Object.<anonymous> (src/Components/Views/Calendar/CalendarViewConfig.test.js:69:32)
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

To check if expected object is a subset of the received object you need to use toMatchObject(object) method:

expect(parseTime('12:54')).toMatchObject({ hours: 12, minutes: 54})

or expect.objectContaining(object) matcher:

expect(parseTime('12:54')).toEqual(expect.objectContaining({ hours: 12, minutes: 54}))

they works in slightly different ways, please take a look at What's the difference between '.toMatchObject' and 'objectContaining' for details.

toContain() is designed to check that an item is in an array.

over 4 years ago · Santiago Trujillo Relatório

0

If you want to test for part of an object inside an array of objects, use objectContaining within arrayContaining. Here's an example:

test( 'an object in an array of objects', async () => {
  const bookData = [
    {
      id: 1,
      book_id: 98764,
      book_title: 'My New International Book',
      country_iso_code: 'IN',
      release_date: '2022-05-24'
    },
    {
      id: 2,
      book_id: 98764,
      book_title: 'My New International Book',
      country_iso_code: 'GB',
      release_date: '2022-05-31'
    },
    {
      id: 3,
      book_id: 98764,
      book_title: 'My New International Book',
      country_iso_code: 'US',
      release_date: '2022-06-01'
    }
  ];

  expect( bookData ).toEqual( 
    expect.arrayContaining([ 
      expect.objectContaining(
        {
          country_iso_code: 'US',
          release_date: '2022-06-01'
        } 
      )
    ])
  );
} );

I got this from this Medium article.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda