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

@forivall/fetch-test-server

v1.4.1

Published

Test node.js HTTP servers using the fetch API

Downloads

7

Readme

Node.js Fetch Test Server

Build Status Version License

This package allows you to easily run your Node.js server for integration testing, and interact with it using the Fetch API. It is similar to SuperTest, but using the Fetch API means that you can take advantage of promises, and newer ES2017 features like async/await.

Installation

npm install --save-dev fetch-test-server

Usage

Create a new instance of TestServer, passing in your HTTP server. You can then call fetch() to make requests against it. This example uses Mocha (which natively supports promises), but you can use any test framework you like.

import { assert } from 'chai';
import app from './myapp';

const server = new TestServer(app);

describe('API Integration Test', () => {
  it('responds to /user', () => {
    return server.fetch('/user').then((res) => {
      assert.strictEqual(res.status, 200);
      
      return res.json();
    }).then((body) => {
      assert.strictEqual(body.name, 'Adrian');
    });
  });
});

Using async/await (currently requires Babel or another transpiler):

import { assert } from 'chai';
import app from './myapp';

const server = new TestServer(app);

describe('API Integration Test', () => {
  it('responds to /user', async () => {
    const res = await server.fetch('/user');
    const body = await res.json();
    
    assert.strictEqual(res.status, 200);
    assert.strictEqual(body.name, 'Adrian');
  });
});

Behind the scenes, it uses node-fetch to implement the Fetch API. The server listens on a random port, and does not start listening until you first call fetch(). Your requests will be automatically held until the server is available.

You can also use helper methods to call common HTTP verbs:

server.head('/path');
server.get('/path');
server.post('/path');
server.put('/path');
server.patch('/path');
server.delete('/path');
server.options('/path');

Per the Fetch API, you can customize the request with an optional second parameter:

server.post('/users', {
  headers: { authorization: 'supersecret' },
  body: 'name=adrian',
});

Finally, if you pass an object as the body parameter, it will automatically be encoded as JSON and sent with a Content-Type: application/json header:

server.post('/users', {
  headers: { authorization: 'supersecret' },
  body: { name: 'Adrian' },
});

This is equivalent to body: JSON.stringify({ name: 'adrian' })

If you need the URL of your test server, use server.address:

server.listen().then(() => {
  // server is listening
  console.log(server.address);
});

If you want to stop the HTTP server, simply call server.close():

server.listen().then(() => {
  // server is listening
  return server.close();
}).then(() => {
  // server is now stopped
});

HTTP Framework Support

Fetch Test Server works with any Node.js HTTP framework.

Express

import app from './expressapp';

const server = new TestServer(app);

Koa

import app from './koaapp';

const server = new TestServer(app.callback());

License

MIT