How to make jest not distinguish between negative zero and positive zero?

A solution for lists and nested objects would be to convert them to JSON and back.

const negativeObject = { list: [-0, -0] };
const positiveObject = { list: [0, 0] };

expect(JSON.parse(JSON.stringify(negativeObject)))
  .toEqual(positiveObject);

This works because JSON.stringify() converts -0 to 0.


You can use the .toBeCloseTo(number, numDigits) matcher:

expect(-0).toBeCloseTo(0, 10);

You can slightly sidestep the issue by doing your logic inside of the expect function:

expect(0).toEqual(-0); // fails
expect(0 === -0).toEqual(true); // ugly, but it works!