got-test
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
Express v4
Vanilla Node.js
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 = {})
- callsgot.get(url, { ...defaultOptions, ...options })
post(url, options = {})
- callsgot.post(url, { ...defaultOptions, ...options })
put(url, options = {})
- callsgot.put(url, { ...defaultOptions, ...options })
del(url, options = {})
- callsgot.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