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
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.
@cpmangukiya Just a quick thought: Is there any chance this has to do with the difference between Windows and Unix line endings?
@meeber , Not sure but yeah it could be. Is there anyway to check and debug if this is the case ?
Update on the case,
Used,
var utils = require('mocha/lib/utils');
var stringify = utils.stringify;
expect(stringify(compareToHtml)).to.equal(stringify(htmlText)); // Updated
I tried this case using stringify function and then I am seeing the difference. As @meeber said something with Windows and Unix line ending. Although this does not happen with many other lines. Only few among many will fail.
Here it fails for highlighted text string in below snapshots,
Expected result is as shown in this HTML file content,
Any thoughts on how can this be resolved ?
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.
Thank you @meeber . I will check out this solution.
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 () {
....
Most helpful comment
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.
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.