Skip to content

Latest commit

 

History

History
70 lines (52 loc) · 3.16 KB

File metadata and controls

70 lines (52 loc) · 3.16 KB

testing-library/no-wait-for-multiple-assertions

📝 Disallow the use of multiple expect calls inside waitFor.

💼 This rule is enabled in the following configs: badge-angular angular, badge-dom dom, badge-marko marko, badge-react react, badge-svelte svelte, badge-vue vue.

🔧 This rule is automatically fixable by the --fix CLI option.

Rule Details

This rule aims to ensure the correct usage of expect inside waitFor, in the way that they're intended to be used.

If you use multiple assertions against the same asynchronous target inside waitFor, you may have to wait for a timeout before seeing a test failure, which is inefficient. Therefore, you should avoid using multiple assertions on the same async target inside a single waitFor callback.

However, multiple assertions against different async targets (for example, independent state updates or different function calls) are allowed. This avoids unnecessary verbosity and maintains readability, without increasing the risk of missing failures.

Example of incorrect code for this rule:

const foo = async () => {
	await waitFor(() => {
		expect(window.fetch).toHaveBeenCalledTimes(1);
		expect(window.fetch).toHaveBeenCalledWith('/foo');
	});

	// or
	await waitFor(function () {
		expect(window.fetch).toHaveBeenCalledTimes(1);
		expect(window.fetch).toHaveBeenCalledWith('/foo');
	});
};

Examples of correct code for this rule:

const foo = async () => {
	await waitFor(() => expect(window.fetch).toHaveBeenCalledTimes(1);
	expect(window.fetch).toHaveBeenCalledWith('/foo');

	// or
	await waitFor(function () {
		expect(window.fetch).toHaveBeenCalledTimes(1);
	});
	expect(window.fetch).toHaveBeenCalledWith('/foo');

	// it only detects expect
	// so this case doesn't generate warnings
	await waitFor(() => {
		fireEvent.keyDown(input, { key: 'ArrowDown' });
		expect(window.fetch).toHaveBeenCalledTimes(1);
	});

	// different async targets so the rule does not report it
	await waitFor(() => {
		expect(window.fetch).toHaveBeenCalledWith('/foo');
		expect(localStorage.setItem).toHaveBeenCalledWith('bar', 'baz');
	});
};

Further Reading