doocs/leetcode · error · Error
Not Equal
Error message
Not Equal
What it means
This error is thrown by a hand-rolled expect(val).toBe(expected) assertion helper for LeetCode problem 2704. It uses strict equality (!==), so toBe rejects any pair that is not strictly equal, including cases where == would pass such as 1 vs '1' or two objects with identical contents. The thrown Error('Not Equal') is the expected output the judge captures via expect(() => ...).toThrow().
Source
Thrown at solution/2700-2799/2704.To Be Or Not To Be/Solution.js:9
/**
* @param {string} val
* @return {Object}
*/
var expect = function (val) {
return {
toBe: function (expected) {
if (val !== expected) {
throw new Error('Not Equal');
}
return true;
},
notToBe: function (expected) {
if (val === expected) {
throw new Error('Equal');
}
return true;
},
};
};
/**
* expect(5).toBe(5); // true
* expect(5).notToBe(5); // throws "Equal"
*/
View on GitHub (pinned to f84f361dc4)
Solutions
- Confirm the mismatch is intended: toBe is strict (===) equality, so '5' vs 5 or two separately-created objects are correctly 'Not Equal'
- If you expected deep equality, compare JSON.stringify(val) === JSON.stringify(expected) or use a deep-equal helper instead
- Use notToBe when you want the assertion to pass on unequal values
- Wrap the call in expect(() => fn()).toThrow('Not Equal') when testing the thrower itself
Example fix
// before
expect('5').toBe(5); // throws 'Not Equal'
// after
expect(Number('5')).toBe(5); // passes
// or assert the throw:
jest.fn(() => expect('5').toBe(5)); // toThrow('Not Equal') Defensive patterns
Strategy: validation
Validate before calling
if (val === expected) { /* toBe will pass */ } else { /* toBe will throw 'Not Equal' */ } Type guard
const isStrictlyEqual = (a: unknown, b: unknown): boolean => a === b;
Try / catch
try { expect(5).toBe(x); } catch (e) { if ((e as Error).message !== 'Not Equal') throw e; /* handle mismatch */ } Prevention
- Normalize types before asserting
- Use deep comparison for objects/arrays
- Remember NaN !== NaN
When it happens
Trigger: Calling expect(5).toBe('5') (type mismatch under ===), expect({}).toBe({}) (different object identities), or any value/expected pair that fails strict equality.
Common situations: Writing or testing the assertion utility itself; confusing === with ==; expecting toBe to behave like deep equality (e.g. comparing objects or arrays); accidentally comparing NaN (NaN !== NaN always throws here).
Related errors
AI-assisted analysis of doocs/leetcode@f84f361dc4 (2026-08-27).
Data as JSON: /api/errors/7b7949e8603bf7b6.
Report an issue: GitHub.