Jest test fails?

In fact, I’m surprised that this:

res.status(200).json({

doesn’t throw an error when you run the test.

Oh!!! Wait, I see. It’s the circular reference. Because status (at least in the test) returns res which does have a method json. OK, that is a really bizarre way to mock that. Your mock should match your data to confirm that the code is handling it properly.

I would expect something like this:

const mockStatusReturnObject = { json: jest.fn() }
const res = { status: jest.fn(() => mockStatusReturnObject) }

Then you can confirm that res.status is called and confirm that the object it returns has a method json that got called.

I haven’t written a jest test in 8 months, but that looks about right.