0

Background

I am creating my test suite and I am using sinon for stubs and chai as an assertion framework.

To combine the two, I am using sinon-chai.

Objective

I want to make sure that my stub is being called with the correct parameters. To do that I have to following line:

expect(myStub).to.be.calledWith(jsonObj);

The json object I am using is the following:

{
    "info": [
        {
            "amenities": {
                "air": true,
                "heating": true,
                "pets": true,
                "pool": false,
                "terrace": false,
                "wifi": true
            },
            "apartmentName": {
                "br": "Incrível apartamento. No centro da cidade",
                "de": "Umfangreiche Wohnung in Mitte. In Barcelona",
                "en": "Central apartment. Simply charming!",
                "es": "Gran apartamento en buena zona ¡Genial!",
                "fr": "Totalement imposant! Près de toutes les attractions de Barcelone",
                "it": "Straordinario appartamento con aria condizionata, a Barcellona",
                "nl": "Welkom bij Barcelona! Comfortabel appartement"
            },
            "bedrooms": 2,
            "city": 1,
            "cityName": {
                "en": "Barcelona",
                "it": "Barcellona"
            },
            "coordinates": {
                "lat": 38.7223,
                "lng": -9.1393
            },
            "id": 17,
            "mfc": 150,
            "numReviews": 0,
            "partnerId": 23,
            "partnerName": "Booking.com",
            "photo": "http://images.friendlyrentals.com/FR_imgs/Property_4089/PrImg_4089_1_lg.jpg",
            "photos": [
                22174376,
                22174377,
                22174378,
                22174379,
                22174380
            ],
            "rating": 0,
            "ref": 4089,
            "sameApartments": [
                630534,
                3136848
            ],
            "sleeps": 5,
            "sqm": 95
        },
        {
            "amenities": {
                "air": true,
                "heating": true,
                "pets": true,
                "pool": false,
                "terrace": false,
                "wifi": false
            },
            "apartmentName": {
                "br": "Incrível apartamento. No centro da cidade",
                "de": "Umfangreiche Wohnung in Mitte. In Barcelona",
                "en": "Central apartment. Simply charming!",
                "es": "Gran apartamento en buena zona ¡Genial!",
                "fr": "Totalement imposant! Près de toutes les attractions de Barcelone",
                "it": "Straordinario appartamento con aria condizionata, a Barcellona",
                "nl": "Welkom bij Barcelona! Comfortabel appartement"
            },
            "bedrooms": 2,
            "city": 1,
            "cityName": {
                "en": "Barcelona",
                "it": "Barcellona"
            },
            "coordinates": {
                "lat": 38.7223,
                "lng": -9.1393
            },
            "id": 19,
            "mfc": 150,
            "numReviews": 0,
            "partnerId": 1,
            "partnerName": "FriendlyRentals",
            "photo": "http://images.friendlyrentals.com/FR_imgs/Property_4089/PrImg_4089_1_lg.jpg",
            "photos": [
                22174376,
                22174377,
                22174378,
                22174379,
                22174380
            ],
            "rating": 4,
            "ref": 4089,
            "sameApartments": [
                61414
            ],
            "sleeps": 5,
            "sqm": 95
        }
    ],
    "requestId": "kjdhsabkdhbvashdfv"
}

Problem

By using http://www.jsondiff.com/ I know for a fact that the object I am receiving and the one I am comparing are the same JSON object.

However no matter what I do, the test always fails, as if the two objects were different.

To fix this I tried using chai-json-equal but it also fails.

The only solution so far, is to use callsFake on my stub, manually convert the JSON to a Javascript object via JSON.parse and then compare. That way the test passes but this solution is horrible when I should just be able to compare two JSON objects to begin with.

I also checked sinon-chai documentation and there isn't any special method for comparing JSON objects.

Questions

  1. Is it possible to compare JSON objects using sinonChai at all? If yes, how do I do it?
  2. How do I compare JSON objects using sinon and chai?
Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266

1 Answers1

0

The problem here is that since JSON files are technically strings representing objects, the order matters.

A JSON file containing { id: 1, phrase: "hello" } will not be the same as one containing { phrase: "hello", id: 1 }.

Even though both files would be semantically identical, the string itself wouldn't so the tests fail.

As of this moment, there is no solution for this. The best option I found was to create a custom sinon matcher:

How to personalize error message sinon matchers

Hope it helps someone else in the future!

Flame_Phoenix
  • 16,489
  • 37
  • 131
  • 266