Search code examples
jquerytestingqunit

How to compare the output of serializeArray using qunit


I am using qunit and jquery. Latest version of both.

In my code when I submit the form I have the event as e. I call

e.serializeArray() 

Here is my test.

equals(args.data, [ { "name": "user_name", "value": "john" } ], 'input data');

And this is the error message from qunit.

expected: [ { "name": "user_name", "value": "david" } ] result: [ { "name": "user_name", "value": "david" } ]

As you can see to the naked eye the expected and result value is same but qunit is not liking it.

I guess I am missing something.


Solution

  • You may want to look at deepEqual

    module('QUnit');
    test('equal and deepEqual', function () {
        expect(4);
        var a1 = [{ "name": "user_name", "value": "david" }];
        var a2 = [{ "name": "user_name", "value": "david" }];
        var b1 = [{ "name": "user_name", "value": "henry" }];
        equal(a1, a2); //should fail
        equal(a1, b1); //should fail
        deepEqual(a1, a2); //should pass
        deepEqual(a1, b1); //should fail
    });
    

    Further digging yielded (from the qunit.js code):

    // Backwards compatibility, deprecated
    QUnit.equals = QUnit.equal;
    QUnit.same = QUnit.deepEqual;
    

    I assume old on the left, new on the right.