Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!