jest: Understandable error messages when using spread operator with expect.any(Object)

🚀 Feature Proposal

Understandable error messages when using object destructuring with expect.any(Object).

Motivation

Let’s consider following functions

const createEvent = (event, payload) => ({
  eventType: event,
  ...payload,
})

const getEvents = () => ([
	createEvent('evt1', { some: 'payload' }),
	createEvent('evt2', { some: 'payload' }),
])

Let’s say you want to test that getEvents gives you appropriate actions and you already tested createEvent so you use it in your next test suites and write something like this

test('this test fails :(', () => {
	const expectedEvents = [
		createEvent('evt1', expect.any(Object)),
		createEvent('evt2', expect.any(Object)),
	]

	expect(getEvents()).toEqual(expectedEvents);
});

You will get a close to cryptic error TypeError: val.toAsymmetricMatcher is not a function with no clue what you need to fix.

Maybe there’s a way to detect this kind of mistakes and provide a more user-friendly error message?

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Sorry for a newbie question, but I’m running into this exact cryptic error message and can’t quite understand from this thread what the mistake on my end is and how to fix it – could someone clarify?