Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix flaky tests #1523

merged 1 commit into from Jul 12, 2022


Copy link

@KhafraDev KhafraDev commented Mar 16, 2022


By relying upon a 100ms delay before aborting, it is possible for the request to succeed before being canceled.

Since these 2 tests merely ensures that cancellation via AbortController works, adding a delay isn't needed regardless.


Removes delay between request and canceling.

Additional information

Undici runs node-fetch's test suite (more or less) and this test is flaky ( for example).

Example code with the issue fixed:

import fetch from 'node-fetch';
import { createServer } from 'http';
import { once } from 'events';

const server = createServer((req, res) => res.end());
server.listen(3000, () => console.log('done'));
await once(server, 'listening');

let i = 1;
while (i++) {
	try {
		const controller = new AbortController();

		const promise = fetch(`http://localhost:3000/`, {
			method: 'POST',
			signal: controller.signal,
			headers: {
				'Content-Type': 'application/json',
				body: '{"hello": "world"}'


		const test = await promise;
		console.log('did not fail', test);
	} catch {
		if (i % 1e3 === 0) {
			console.log('did not fail after ' + i + ' tries');

@KhafraDev KhafraDev changed the title tests: fix flaky tests test: fix flaky tests Mar 16, 2022
Copy link

@LinusU LinusU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat 👍

@jimmywarting jimmywarting merged commit 11b7033 into node-fetch:main Jul 12, 2022
4 of 9 checks passed
Copy link

🎉 This PR is included in version 3.2.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

@KhafraDev KhafraDev deleted the fix-possibly-flaky-test branch July 12, 2022 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

Successfully merging this pull request may close these issues.

None yet

4 participants