Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

supertest-fetch

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

supertest-fetch

Supertest with WHATWG fetch like interface.

  • 1.4.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.8K
decreased by-63.27%
Maintainers
1
Weekly downloads
 
Created
Source

supertest-fetch

NPM version Build Status Coverage Status semantic-release

A typescript friendly alternative to Supertest, backed by node-fetch

What is it?

This is a library heavily influenced by Visionmedia's excellent supertest library. The advantages of this library are:

  • Uses node-fetch to give you a WHATWG Fetch-like interface.
  • 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

.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();

Keywords

FAQs

Package last updated on 24 Aug 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc