got-test

1.0.2 • Public • Published

got-test

Build Status Codecov NPM module

This is a wrapper around Got to enable easy testing for Node application servers, similar to what supertest does.

Features:

  • Works with built-in Server object (including Koa and Express applications)
  • Supports default options with per-request overrides
  • Works with your choice of test framework and assertion library
  • Can work in browser too
  • Comprehensive test coverage

Installation

npm install got-test

Or if using Yarn:

yarn add got-test

Basic usage

The examples below assume Jest as the testing framework with Babel transpilation enabled.

Koa v2

import Koa from 'koa'
import KoaRouter from 'koa-router'
import { gotServer } from 'got-test'
 
describe('app', () => {
  let server
 
  beforeEach(done => {
    const app = new Koa()
    const router = new KoaRouter()
 
    router.get('/blog', async ctx => ctx.body = 'hello world!')
 
    app.use(router.routes())
 
    server = app.listen(3000, done)
  })
 
  afterEach(done => {
    server.close(done)
  })
 
  it('returns blog posts', async () => {
    const request = gotServer(server)
 
    const ret = await request.get('/blog')
 
    expect(ret.body).toEqual('hello world!')
  })
})

Express v4

import express from 'express'
import { gotServer } from 'got-test'
 
describe('app', () => {
  let server
 
  beforeEach(done => {
    const app = express()
 
    app.get('/', (req, res) => res.send('hello world!'))
 
    server = app.listen(3000, done)
  })
 
  afterEach(done => {
    server.close(done)
  })
 
  it('returns blog posts', async () => {
    const request = gotServer(server)
 
    const ret = await request.get('/blog')
 
    expect(ret.body).toEqual('hello world!')
  })
})

Vanilla Node.js

import http from 'http'
import { gotServer } from 'got-test'
 
describe('app', () => {
  let server
 
  beforeEach(done => {
    server = http.createServer((req, res) => {
      res.statusCode = 200
      res.setHeader('Content-Type', 'text/plain')
      res.end('hell world!')
    })
 
    server.listen(3000, done)
  })
 
  afterEach(done => {
    server.close(done)
  })
 
  it('returns blog posts', async () => {
    const request = gotServer(server)
 
    const ret = await request.get('/blog')
 
    expect(ret.body).toEqual('hello world!')
  })
})

The server parameter passed to gotServer() only needs to have an address() method - see API docs below...

API

gotServer (httpServer, defaultOptions = {})

Param: httpServer

This must be a net.Server instance or an object which provides an address() method which does the same thing as net.Server.address() (this is how you can use this lib in a browser)

Param: defaultOptions

This gets passed to got as its options parameter (see docs) for any subsequent calls. All options can be overridden in individual requests (see below).

Returns

It return an object with the following methods available:

  • get(url, options = {}) - calls got.get(url, { ...defaultOptions, ...options })
  • post(url, options = {}) - calls got.post(url, { ...defaultOptions, ...options })
  • put(url, options = {}) - calls got.put(url, { ...defaultOptions, ...options })
  • del(url, options = {}) - calls got.delete(url, { ...defaultOptions, ...options })

As you can see each call passes the got call result right back to you, so it's as if you're using got directly.

License

MIT - see LICENSE.md

Readme

Keywords

none

Package Sidebar

Install

npm i got-test

Weekly Downloads

1

Version

1.0.2

License

MIT

Last publish

Collaborators

  • hiddentao