'Two empty strings are not equal in Javascript error
Before you dislike, leave a comment telling me why...
One of these strings is imported using fs and the other is created through code. I noticed some weird characters when debugging but i'm not sure how to get this to pass without trimming. I don't want to trim since that will ruin a lot of the other checks, spacing matters.
32m vs 31m is the reason these strings aren't matching.
More Context:
Even More Context:
const originalFile = fs.readFileSync(`./tests/mocks/${filePath}.js`, 'utf8');
const classParser = ClassParser.create();
let parseResult = classParser.parse(originalFile, '');
const compiledFile = fs.readFileSync(`./tests/mocks/${filePath}.d.ts`, 'utf8');
const typeFileAry = parseResult.typeFile.split('\n');
const compileAry = compiledFile.split('\n');
expect(typeFileAry.length).toBe(compileAry.length);
for(let i = 0; i < typeFileAry.length; ++i) {
expect(typeFileAry[i]).toEqual(compileAry[i]);
}
Dug into Jest Source Code and it looks like they do Object.is(a,b) and that is returning false. Object.is('','') typically returns true. I'm not sure where those characters are coming from.
Solution 1:[1]
the "===" operator will return true only if both variables have the same value and type. If the value is the same, maybe there is a type mismatch.
To test it, you can check your variable's type using typeof operator.
Check here: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/typeof
Solution 2:[2]
I hit this today (two seemingly empty strings not being equal). The output from Jest was:
expect(received).toBe(expected) // Object.is equality
Expected: ""
Received: "?"
After converting the string to codepoints (Array.from(string).map((char) => char.charCodeAt(0))
) it seems that a non-breaking space had crept in from one of the elements I was getting the innerText
of.
Worth running something like the above to verify there aren't any "hidden" chars that don't appear in your terminal, but exist in the string.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | danielarend |
Solution 2 | Dom Hastings |