Stefan H.
Stefan H.

Reputation: 301

Jest Test "Compared values have no visual difference."

I'm doing a comparison between two objects that are quite complex and attempting to use the .toEqual method in expect.

Here is my test:

it('check if stepGroups data in controlData matches data in liveData', () =>    {
    var controlStore = data.controlStore
    var liveStore
    return getData().then(result => {
        liveStore = new Store()
        liveStore.loadData(JSON.parse(result))
        expect(controlStore).toEqual(liveStore)  
    })
})

I did a diff between the expected and the the received output and they both appear to be the same. What would still be causing this test to fail? I was reading up on pretty-format (https://github.com/facebook/jest/issues/1622). Have you run into similar situations?

Upvotes: 30

Views: 26838

Answers (5)

Clemens
Clemens

Reputation: 1432

I would not recommend using JSON.stringify as it might hide an underlying problem. In my experience, this error mostly occurs when an array is assigned a key that is not a positive integer. Example:

  const arr = [1]
  arr.a = 'b'
  expect(arr).toEqual([1])

The test fails with the error Compared values have no visual difference. To understand what is going on I recommend to log the object and then adapt the test case as necessary.

console.log(arr)
// logs [ 1, a: 'b' ]

Upvotes: 5

Tadas Talaikis
Tadas Talaikis

Reputation: 124

If you compare objects, that should be deep equal not just equal, for example:

import chai from 'chai'
chai.should()

controlStore.should.be.deep.equal(liveStore)

Upvotes: 1

user2584621
user2584621

Reputation: 2723

use expect(JSON.stringify(controlStrore)).toEqual(JSON.stringify(liveStore))

Upvotes: 29

You probably trying to set a method via a lambda-function in your Store class. So jest is trying to compare to different functions, which were generated separately each time you do new Store(). As a result it rises an error, though it cant display difference, it just cant print functions.

Upvotes: 3

eur00t
eur00t

Reputation: 1548

In your code sample you are comparing two instances of Store, which encapsulate some data. Thus even if data (result json in your case) is the same, it doesn't necessarily imply that both container instances can be considered equal.

It should be possible to do something like expect(controlStore.getState()).toEqual(liveStore.getState()).

Upvotes: 12

Related Questions