jest-mock-server
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

jest-mock-server

Test your http clients with jest:

  • use Koa API to define http handlers
  • use Jest API to define asserts

Usage example

import { MockServer } from 'jest-mock-server';
import fetch from 'node-fetch';

describe('Testing node-fetch HTTP client', () => {
  const server = new MockServer();

  beforeAll(() => server.start());
  afterAll(() => server.stop());
  beforeEach(() => server.reset());

  it('Receives a status over the network', async () => {
    const route = server
      .get('/')
      // Look ma, plain Jest API!
      .mockImplementationOnce((ctx) => {
        // ...and plain Koa API
        ctx.status = 200;
      })
      .mockImplementationOnce((ctx) => {
        ctx.status = 201;
      });

    // Since we did not passed any port into server constructor, server was started at random free port
    const url = server.getURL();

    const res1 = await fetch(url);
    expect(res1.status).toBe(200);

    const res2 = await fetch(url);
    expect(res2.status).toBe(201);

    const res3 = await fetch(url);
    expect(res3.status).toBe(404);

    expect(route).toHaveBeenCalledTimes(3); // Yep, jest API again
  });
});

Readme

Keywords

none

Package Sidebar

Install

npm i jest-mock-server

Weekly Downloads

7,933

Version

0.1.0

License

MIT

Unpacked Size

9.43 kB

Total Files

6

Last publish

Collaborators

  • daniel.hreben