
Security News
Software Engineering Daily Podcast: Feross on AI, Open Source, and Supply Chain Risk
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.
supertest-native-fetch
Advanced tools
This is a fork of supertest-fetch that uses native node Fetch API implementation (support since node v18.0.0) instead of a node-fetch package. It will be droped after / in case of a pull request merging.
import http from 'http';
import { makeRequest } from 'supertest-native-fetch';
const server = http.createServer((req, res) => {
res.setHeader('content-type', 'application/json');
res.end(JSON.stringify({ greeting: 'Hello!' }));
});
// This is a function with an API identical to the WHATWG `fetch()` function,
// except the returned Promise has a bunch of supertest like functions on it.
//
// If the server is not listening, then `request()` will call `listen()` on the
// server before each fetch, and close it after each fetch.
const request = makeRequest(server);
describe('my server tests', function () {
it('should return a response', async function () {
await request('/hello')
.expect(200)
.expect('content-type', 'application/json')
.expect({ greeting: 'Hello!' });
});
it('will work just like fetch if you need to do more advanced things', async function () {
const response = await request('/hello')
.expect(200)
.expect('content-type', 'application/json');
expect(await response.json()).to.eql({ greeting: 'Hello!' });
});
it('should post data', async function () {
await request('/hello', {
method: 'post',
body: '<message>Hello</message>',
headers: { 'content-type': 'application/xml' },
});
});
});
Returns a new request function. This is identical to the WHAT-WG fetch function, except that the returned object has some extra assertions added to it.
If the server passed in is not already listening, each call to request() will call listen() on the server, and close it after each request. This will assign a random free port to the server, so you don't need to worry about listening on a well-known port for your tests to work.
If the server passed in is an instance of tls.Server, then the returned request instance will use HTTPS to connect to the server instead of HTTP. Note that it's up to you to appropriately configure the server, supplying a certificate and key, and if you're using a self-signed certificate you'll need to pass an "agent" to the call to request. See this example for details.
Verify response status code and text.
Verify headerName matches the given value or regex. If value is null,
verifies that the header is not present.
Verify body is the given string, JSON object, or matches the given regular expression.
Supertest friendly alias for .expectStatus(statusCode).
Supertest friendly alias for .expectStatus(statusCode).expectBody(body).
Supertest friendly alias for .expectBody(body).
Supertest friendly alias for .expectHeader(field, value).
Convenience function which returns a Promise which resolves to the JSON content of the response. This:
const result = await request('/hello').expect(200).json();
is equivalent to:
const response = await request('/hello').expect(200);
const result = await response.json();
FAQs
Supertest with node native fetch implementation.
We found that supertest-native-fetch demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket CEO Feross Aboukhadijeh joins Software Engineering Daily to discuss modern software supply chain attacks and rising AI-driven security risks.

Security News
GitHub has revoked npm classic tokens for publishing; maintainers must migrate, but OpenJS warns OIDC trusted publishing still has risky gaps for critical projects.

Security News
Rust’s crates.io team is advancing an RFC to add a Security tab that surfaces RustSec vulnerability and unsoundness advisories directly on crate pages.