question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue while comparing a string value(prepared using concatenated strings) and an another string value(prepared using buffer)

See original GitHub issue

I would like to raise an issue that I am facing since last few months. While working on Node.js I have defined many test cases as below code snippet. While most of them works and getting passed with proper data, it will fail for some of the cases even though there is no change of even single space in two string values to getting compared.

This issue will not come in all the systems. For example in some desktop systems it fails while in deployed servers, this same piece of code and data will never fail. Here there is no change I see even in terms of single character(alphabet, special char, blank space or anything, these is true with many such cases where it fails). That is strange !
See attached screenshot issue while comparing

I tried searching solutions through many sources online but no success .

If you find this as an issue, could you please resolve it ?

Code:

const chai = require('chai');
const expect = chai.expect;
const fs = require('fs');
const jsFile = require('../../../lib/<some-file-path>');

describe('test case', function () {
    it('test scenario', function (done) {
        jsFile.someOperation(someValue, function (htmlText) {
            // htmlText here is string value like say, let htmlText = "<a>" + "Link Label" + "</a>";
            fs.readFile('<File_Path>/resultTestFileName.html', "UTF-8", function (err, buf) {
                let compareToHtml = buf.toString();
                expect(compareToHtml).to.equal(htmlText);
                done();
            });
        })
    });
});

Chai and Mocha version used: “chai”: “^3.5.0”, “chai-http”: “^2.0.1”, “mocha”: “^2.4.5”,

Feel free to ask if you need more details.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
cpmangukiyacommented, Sep 26, 2018

The eol package has worked in a specific way. Sharing the solution here.

One may need to write a case something like this to make it work.

const chai = require('chai');
const expect = chai.expect;
const fs = require('fs');
const eol = require('eol');
const jsFile = require('../../../lib/<some-file-path>');


describe('test case', function () {
    it('test scenario', function (done) {
        jsFile.someOperation(someValue, function (htmlText) {
            // htmlText here is string value like say, let htmlText = "<a>" + "Link Label" + "</a>";
            fs.readFile('<File_Path>/resultTestFileName.html', "UTF-8", function (err, buf) {
                let compareToHtml = buf.toString();
                expect(eol.lf(compareToHtml)).to.equal(eol.lf(htmlText));
                done();
            });
        })
    });
});

Here I have used eol.lf function as below to update the resultTestFileName.html file in a Windows based system .Then this case with pass without any issue in Unix and Windows both.

....
fs.writeFile('<File_Path>/resultTestFileName.html', eol.lf(htmlText), function () { 
....
1reaction
meebercommented, Sep 13, 2018

Kinda depends on your application’s requirements. If your application is meant to be cross-platform, then in general I wouldn’t suggest trying to alter your app’s output to enforce a particular End-Of-Line (EOL) format. Instead, you could just normalize EOLs in the strings before comparing them in your tests. I haven’t used it before, but it looks like this package can help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

String Concatenation in Java
String.format() method allows to concatenate multiple strings using format specifier like %s followed by the string values or objects. StrFormat.java.
Read more >
String vs StringBuilder vs StringBuffer in Java
A string is a sequence of characters. In java, objects of String are immutable which means a constant and cannot be changed once...
Read more >
Efficient string copying and concatenation in C
Martin Sebor looks at C string handling functions used to copy and concatenate strings and examines ways to improve their efficiency.
Read more >
4 ways to concatenate Strings in Java [Example and ...
At last, StringBuffer.toString() calls create a new String object with a copy of the StringBuilder buffer. This means, to concatenate two String, you...
Read more >
Java String Concatenation: Which Way Is Best? - Code Red
Unfortunately, Java doesn't offer this method of string concatenation. Instead we are left with the following ways of combining the strings: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found