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

169
Visualizações
Result should contain one of the correct answers

I am trying to write a test to see that the result returned from a function I am testing, is one of the correct results.

The result is an array of objects, for example [{ op: 'replace', path: '', value: { 0: 1 } }]

I want to see if this result is one of the correct answers, the test should pass.

I tried changing one of the tests as mentioned here, but I can't seem to get it to work.

Here is a code snippet:

 test(`For arrays, if data gets added, replace operation is sent`, () => {
      const data = []
      const update = [1]

      const result = functionToTest(data, update)

      const correct = [{ op: 'replace', path: '', value: { 0: 1 } }]
      const correct2 = [{ op: 'replace', path: '', value: [1] }]
      
//the result should include one of there answers. So if correct or correct 2 
//matches, it should pass.

//here I add all the correct answers to an array, to its essensially an 
//array of arrays
// [ 
//   [{ op: 'replace', path: '', value: { 0: 1 } }], 
//   [{ op: 'replace', path: '', value: [1] }]
// ]`
      const correctResults = [correct, correct2]

//the result of the function should be contained in one of the correct results
      expect(result).toEqual(
        expect.arrayContaining(correctResults)
      )
    })

I will write a code sandbox or something to test it in the browser. Here is the repo for now: https://github.com/jpbnetley/test-json-patch/tree/bugfix/update-tests

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

0

I have found that this seems to fix the problem.

    test(`For arrays, if data gets added, replace operation is sent`, () => {
      const data = []
      const update = [1]

      const result = functionToTest(data, update)

      const correct = [{ op: 'replace', path: '', value: { 0: 1 } }]
      const correct2 = [{ op: 'replace', path: '', value: [1] }]
      const correctResults = {...correct, ...correct2}

      expect(result).toEqual(
        expect.objectContaining(correctResults)
      )
    })
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