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

229
Visualizações
Jest test assertion, expect a nested object

When I do the below expect in jest with native test

expect(mockOnChange).toHaveBeenCalledWith({
     order_type: expect.arrayContaining(['1']),
     number: '',
     ouser_ids: expect.arrayContaining(['user_id1']),
     payment_type: expect.arrayContaining(['2']),
     start_date: old_Date,
     end_date: current_date,
     state: 'all',
    });

I'm getting the following error

   Object {
                "end_date": "2022/06/07",
                "number": "",
            -   "order_type": ArrayContaining [
            +   "order_type": Array [
                  "1",
                ],
            -   "ouser_ids": ArrayContaining [
            +   "ouser_ids": Array [
                  "user_id1",
            -   ],
            -   "payment_type": ArrayContaining [
            -     "2",
                ],
            +   "payment_type": Array [],
                "start_date": "2022/06/01",
                "state": "all",
              },

I have also tried without expect.arrayContaining() still getting the same error I'm I doing this wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can't use expect.arrayContaining inside the properties of an object that you are expecting. You will need to use an alternative approach as follows:

const objectCalledWith = mockOnChange.mock.calls[0][0];
expect(objectCalledWith.order_type).toEqual(expect.arrayContaining(['1']));
// and so on for the other property values...

Explanation:

mock.calls contains all of the calls made on mockOnChange, where we specifically choose to access the first call via [0]. The second [0] retrieves the first argument that the mockOnChange function was called with and we assign it to objectCalledWith variable. We can then use this variable to inspect and validate its properties using the expect matchers as necessary.

about 4 years ago · Juan Pablo Isaza 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