npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

supertest-fetch

v2.0.0

Published

Supertest with WHATWG fetch like interface.

Downloads

18,962

Readme

supertest-fetch

NPM version Build Status Coverage Status semantic-release

A typescript friendly alternative to Supertest. Backed by native node fetch implementation so it requires node 18+ version.

What is it?

This is a library heavily influenced by Visionmedia's excellent supertest library, but with a WHATWG Fetch-like interface. The advantages of this library are:

  • Uses native node Fetch API implementation (support since node v18.0.0).
  • Should be instantly familiar to anyone who has used supertest.
  • First class support for promises.
  • Supertest has some weird quirks when used with Typescript becuase of @types/superagent.

Example

import http from 'http';
import { makeFetch } from 'supertest-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 `fetch()` will call `listen()` on the
// server before each fetch, and close it after each fetch.
const fetch = makeFetch(server);

describe('my server tests', function () {
    it('should return a response', async function () {
        await fetch('/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 fetch('/hello')
            .expect(200)
            .expect('content-type', 'application/json');

        expect(await response.json()).to.eql({ greeting: 'Hello!' });
    });

    it('should post data', async function () {
        await fetch('/hello', {
            method: 'post',
            body: '<message>Hello</message>',
            headers: { 'content-type': 'application/xml' },
        });
    });
});

API

makeFetch(server)

Returns a new fetch 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 fetch() 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 fetch 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 fetch. See this example for details.

.expectStatus(statusCode[, statusText])

Verify response status code and text.

.expectHeader(headerName, value)

Verify headerName matches the given value or regex. If value is null, verifies that the header is not present.

.expectBody(body)

Verify body is the given string, JSON object, or matches the given regular expression.

.expect(statusCode[, fn])

Supertest friendly alias for .expectStatus(statusCode).

.expect(statusCode, body)

Supertest friendly alias for .expectStatus(statusCode).expectBody(body).

.expect(body)

Supertest friendly alias for .expectBody(body).

.expect(field, value)

Supertest friendly alias for .expectHeader(field, value).

.json()

Convenience function which returns a Promise which resolves to the JSON content of the response. This:

const result = await fetch('/hello').expect(200).json();

is equivalent to:

const response = await fetch('/hello').expect(200);
const result = await response.json();