4

I don't understand what's happening. I work in node js, here is my code snippet:

var body = Buffer.concat(responseBody).toString();
var expectBody = fs.readFileSync(expectfile);
if(expectBody != body) {
    console.log("Response body != expected file");
    console.log("Response body: " + body);
    console.log("Expected body: " + expectBody);
}

And here is the output I am seeing:

Response body != expected file
Response body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}
Expected body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}

As far as I can see, the strings are identical, but node js thinks otherwise.

I took the printed strings and saved them into two files, then did a diff - got nothing!

Does this have something to do with the way the files are read?

As far as I understand, != is for non-strict comparison, so should only check the actual text in the variables, right?

=== update: ===

Following your suggestions, I tried this:

if(JSON.stringify(expectBody) != JSON.stringify(body)) {
       console.log(" stringify, not equal!");
}

and

if(expectBody.toString() != body.toString()) {
    console.log(" to string, not equal!");
}

I'm still getting "stringify, not equal" and "to string, not equal" printed out :(

===== solved: ======

This is what worked for me in the end:

var filecheck = require('./file_checks.js');
var expectjson = JSON.parse(expectBody);
var receivedjson = JSON.parse(body);
if(filecheck.jsonCompareSync(expectjson, receivedjson)) {
    // not equal
}

Thanks everyone for helping!

10
  • Compare the length of the strings. Commented Aug 30, 2018 at 10:53
  • @Teemu I do this before this check with if(expectBody.size == body.size), and this returns true. Commented Aug 30, 2018 at 10:56
  • Then check the type of the "operands". Commented Aug 30, 2018 at 10:58
  • stringify the both value before compare. Commented Aug 30, 2018 at 10:59
  • 1
    That's true, but the size of a string is undefined. If the size isn't undefined you're prpobably dealing with objects and not strings. Try doing a .toString() on expectedBody in the comparison. Commented Aug 30, 2018 at 11:04

6 Answers 6

2

The reason for this behavior is that internally JavaScript has two different approaches for validating equality.

The latest ECMAScript standard defines seven data types:

Six data types that are primitives:

  • Boolean
  • Null
  • Undefined
  • Number
  • String
  • Symbol (new in ECMAScript 6)

and then there is a data type as

  • Object

Primitives are compared by their value, while objects are compared by their reference.

example 01

let a = { "one":"a"};
let b = { "one":"a"};
console.log(a!=b);//true

example 02

let a = { "one":"a"};
let b = a;
console.log(a!=b);//false

In your case you are comparing Objects then you will have to

  • Convert object into primitive(string) and then compare

or

  • Write a separate deep object comparison function(you could find lot of them with simple google search)

UPDATE - test script for your case

let a = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]};
let b = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]};

console.log(a!=b); //true
console.log(JSON.stringify(b) != JSON.stringify(b)); //false



// Some other approach to make sure you are not fooled by your eye comparison
var crypto = require('crypto');
console.log(crypto.createHash('md5').update(a).digest("hex"));
console.log(crypto.createHash('md5').update(b).digest("hex"));
Sign up to request clarification or add additional context in comments.

2 Comments

I've added more detail to the original question. I tried converting into string, doesn't seem to work :(
thanks I found the solution. Put it in the original question.
0

i hope this will work :)

var a = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}

var b = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}


// check your condition

if( JSON.stringify(a) !=  JSON.stringify(b)){
   console.log("Objects are different")
}else{
   console.log("objects are same")
}

3 Comments

Thanks, but I added: if(JSON.stringify(expectBody) != JSON.stringify(body)) { console.log(" stringify, not equal!"); } And still getting " stringify, not equal!" printed out :(
did you checked the expectBody and body , having any diff or its same in the question
can't check them directly, as they are var, not files. Or did I misunderstand your question?
0

What you can do is.

let obj1 = JSON.stringify({"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]});
let obj2 = JSON.stringify({"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]});

and then compare them, obj1 != obj2

So like this

var body = Buffer.concat(responseBody).toString();
var expectBody = fs.readFileSync(expectfile);
if(JSON.stringify(expectBody) != JSON.stringify(body)) {
  console.log("Response body != expected file");
  console.log("Response body: " + body);
  console.log("Expected body: " + expectBody);
}

Note this will fail if the order of object is not same.

5 Comments

But I am not trying to do ===, I'm doing !=, this is the opposite of ==, isn't it?
sorry, i have updated my answer, the problem with object comparison in object is that it is shallow, so convert them into a string and compare the strings. Is this helpful..?
I added: let obj1 = body; let obj2 = expectBody; if (obj1 != obj2) { console.log("Still not equal!"); } I get "Still not equal" printed :(
convert both of those objects into string using JSON.stringify like in the code I shared above, and then perform the comparison.
The values are inside a file I receive, I can't hard-code them.
0

I met the same issue with 2 strings that are the same letter by letter after rendered to screen, but they are slightly different byte by byte.

By creating hash they are really different, so in test case I have to implement a customized stringEquals instead of using assert.equal.

// Buffer.from(stringA).toString('hex')

3c703e57656972642d6f3a203c7370616e20636c6173733d226b61746578223e3c7370616e20636c6173733d226b617465782d6d6174686d6c223e3c6d61746820786d6c6e733d22687474703a2f2f7777772e77332e6f72672f313939382f4d6174682f4d6174684d4c223e3c73656d616e746963733e3c6d726f773e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2274727565223e3c6d726f773e3c6d6f2066656e63653d2274727565223e283c2f6d6f3e3c6d7461626c6520726f7773706163696e673d22302e31363030656d2220636f6c756d6e616c69676e3d2263656e7465722063656e7465722220636f6c756d6e73706163696e673d2231656d223e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d6e3e313c2f6d6e3e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c2f6d7461626c653e3c6d6f2066656e63653d2274727565223e293c2f6d6f3e3c2f6d726f773e3c2f6d7374796c653e3c2f6d726f773e3c616e6e6f746174696f6e20656e636f64696e673d226170706c69636174696f6e2f782d746578223e5c646973706c61797374796c657b5c626567696e7b706d61747269787d205c242026616d703b20315c5c5c24205c656e647b706d61747269787d7d3c2f616e6e6f746174696f6e3e3c2f73656d616e746963733e3c2f6d6174683e3c2f7370616e3e3c7370616e20636c6173733d226b617465782d68746d6c2220617269612d68696464656e3d2274727565223e3c7370616e20636c6173733d2262617365223e3c7370616e20636c6173733d22737472757422207374796c653d226865696768743a322e3430303033656d3b766572746963616c2d616c69676e3a2d302e3935303033656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d696e6e6572223e3c7370616e20636c6173733d226d6f70656e2064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e283c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d7461626c65223e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d7420766c6973742d7432223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e207374796c653d22746f703a2d322e34303939393939393939393939393937656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d73223ee2808b3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a302e39353030303030303030303030303034656d3b223e3c7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d74223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e313c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d636c6f73652064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e293c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e2e3c2f703e0a

// Buffer.from(stringB).toString('hex')

3c703e57656972642d6f3a203c7370616e20636c6173733d226b61746578223e3c7370616e20636c6173733d226b617465782d6d6174686d6c223e3c6d61746820786d6c6e733d22687474703a2f2f7777772e77332e6f72672f313939382f4d6174682f4d6174684d4c223e3c73656d616e746963733e3c6d726f773e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2274727565223e3c6d726f773e3c6d6f2066656e63653d2274727565223e283c2f6d6f3e3c6d7461626c6520726f7773706163696e673d22302e31363030656d2220636f6c756d6e616c69676e3d2263656e7465722063656e7465722220636f6c756d6e73706163696e673d2231656d223e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d6e3e313c2f6d6e3e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c2f6d7461626c653e3c6d6f2066656e63653d2274727565223e293c2f6d6f3e3c2f6d726f773e3c2f6d7374796c653e3c2f6d726f773e3c616e6e6f746174696f6e20656e636f64696e673d226170706c69636174696f6e2f782d746578223e5c646973706c61797374796c657b5c626567696e7b706d61747269787d205c242026616d703b20315c5c5c24205c656e647b706d61747269787d7d3c2f616e6e6f746174696f6e3e3c2f73656d616e746963733e3c2f6d6174683e3c2f7370616e3e3c7370616e20636c6173733d226b617465782d68746d6c2220617269612d68696464656e3d2274727565223e3c7370616e20636c6173733d2262617365223e3c7370616e20636c6173733d22737472757422207374796c653d226865696768743a322e3430303033656d3b766572746963616c2d616c69676e3a2d302e3935303033656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d696e6e6572223e3c7370616e20636c6173733d226d6f70656e2064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e283c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d7461626c65223e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d7420766c6973742d7432223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e207374796c653d22746f703a2d322e34303939393939393939393939393937656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d73223e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a302e39353030303030303030303030303034656d3b223e3c7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d74223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e313c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d636c6f73652064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e293c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e2e3c2f703e0a

Comments

0

Simplified answer based on title:

Two strings are not equal in node js, but look equal to me

I had the following node code:

console.log(`"${v1}===${v2}"`, v1===v2)

Which printed:

"ID===ID" false

Strangely, when I was copying the above equality on devtools it was printing true.

Solution:

Used the following code to check the actual differrences between the two strings:

console.log(`"${v1}(${Array.from(v1).map(c => c.charCodeAt(0))})===${v2}(${Array.from(v2).map(c => c.charCodeAt(0))})"`, v1===v2);

I got:

"ID(73,68)===ID(65279,73,68)" false

In my case, the problem was that the a csv parser that I was using, added the bom character as part of the first csv column title string (in the specific case the ID).

1 Comment

And for the record to remove the bom character when reading a file in node: fs.readFileSync(file, 'utf-8').replace(/^\uFEFF/, '');
-1

This is what worked for me in the end:

var filecheck = require('./file_checks.js');
var expectjson = JSON.parse(expectBody);
var receivedjson = JSON.parse(body);
if(filecheck.jsonCompareSync(expectjson, receivedjson)) {
    // not equal
}

Thanks everyone for helping!

2 Comments

hope to see the content of your file_checks.js.specially the function jsonCompareSync
@Nuwan oh sorry didn't realise this wasn't a standard library. Had another look, it basically checks all values one by one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.