Subham Raj
Subham Raj

Reputation: 113

toBe() failing in string comparision

I was working in a test and I have and expect block like expect(getHighChartDataLabel(container).textContent.trim()).toBe('AFN 4.97M') and the test is failing, even I'm getting the same value.

Can anyone help me where am I going wrong?

enter image description here

Upvotes: 1

Views: 99

Answers (1)

dave
dave

Reputation: 64657

It's impossible to say based off a screenshot, but as a means of figuring out how they differ - I would guess one contains a nonprintable character, or a character which looks identical but is in fact a different - e.g.

console.log('A' === 'Α') // 'A' === "\u0391"

let str = 'a\x08bc';
console.log(str);
console.log('abc');
console.log(str === 'abc');

I would try comparing their lengths, and then compare character by character:

const text = getHighchartDataLabel(container).textContent.trim();
const expected = 'AFN 4.97M';
expect(text.length).toBe(expected.length);
for (let i = 0; i < text.length; i++) {
    expect(text[i]).toBe(expected[i]);
}

Upvotes: 2

Related Questions