-1

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

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • I may be very wrong here, but why did you design your function in such a way that it returns you different objects for the same inputs? This seems strange to me in a unit test. – Jonas Castanheira Apr 27 '22 at 15:18
  • I am testing different implementations of json patch. some of them will for example send a `[{ op: 'replace', path: '', value: [1] }]` and others will send `[{ op: 'replace', path: '', value: [1] }]` and both of them should be valid. You will see the different functions gets passed through here: https://github.com/jpbnetley/test-json-patch/blob/bugfix/update-tests/src/__tests__/json-patch.test.js#L5 And the functions all execute the same tests here https://github.com/jpbnetley/test-json-patch/blob/bugfix/update-tests/src/__tests__/json-patch.test.js#L285 – jpbnetley Apr 27 '22 at 16:27
  • Maybe a better test would be that the result of applying whatever patch you receive to a known object gives the expected result, then? That's less dependent on the specific implementation. – jonrsharpe Apr 27 '22 at 16:44
  • Probable dupe: https://stackoverflow.com/q/51519041/3001761. But I'd encourage exploring the route above, as it tests the _behaviour_, rather than the _implementation_. – jonrsharpe Apr 27 '22 at 20:16

1 Answers1

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)
      )
    })
  • Do you know what `({...correct, ...correct2})` actually _is_? You're spreading _arrays_ into an _object_, and you can only have one value for a given key (0, in this case)... – jonrsharpe Apr 27 '22 at 19:59
  • Yea you have good point . Thanks – jpbnetley Apr 27 '22 at 20:09
  • To put it another way, this is exactly the same as `expect(result).toEqual(correct2)`, and therefore doesn't answer the question. – jonrsharpe Apr 27 '22 at 20:15