Part of HTTP Toolkit: powerful tools for building, testing & debugging HTTP(S)
There's a lot of tools to do this, but typically by stubbing the HTTP functions in your process at the JS level. That ties you to a specific environment, doesn't test the real requests that'd be made, and only works for requests made in the same JS processs. It's inflexible, limiting and inaccurate, and often unreliable & tricky to debug too.
Mockttp is here to make this better.
Mockttp allows you to do accurate true integration testing, writing one set of tests that works out of the box in node or browsers, with support for transparent proxying & HTTPS, strong typing & promises throughout, fast & safe parallel testing, and helpful built-in debuggability support all the way down.
Let's get specific. Mockttp lets you:
- Write easy, fast & reliable node.js & browser HTTP integration tests
- Stub server responses and verify HTTP requests made by your code
- Intercept HTTPS too, with built-in self-signed certificate generation
- Mock requests inside or outside your process/tab, including subprocesses, native code, remote devices, and more
- Test true real-world behaviour, verifying the real requests made, and testing exactly how your whole stack will handle a response in reality
- Stub direct requests, or transparently stub requests elsewhere as an HTTP mocking proxy
- Mock for node & browser tests with the same code (universal/'isomorphic' HTTP mocking)
- Safely mock HTTP in parallel, with autoconfiguration of ports, mock URLs and proxy settings, for super-charged integration testing
- Debug your tests easily, with full explainability of all mock matches & misses, mock autosuggestions, and an extra detailed debug mode
- Write modern test code, with promises all the way down, async/await, and strong typing (with TypeScript) throughout
npm install --save-dev mockttp
To run an HTTP integration test, you need to:
- Start a Mockttp server
- Mock the endpoints you're interested in
- Make some real HTTP requests
- Assert on the results
Here's a simple minimal example of all that using plain promises, Mocha, Chai & Superagent, which works out of the box in Node and modern browsers:
(Want to play with this yourself? Try running a standalone version live on RunKit: https://npm.runkit.com/mockttp)
That is pretty easy, but we can make this simpler & more powerful. Let's take a look at some more fancy features:
These examples uses Mocha, Chai and Superagent, but none of those are required: Mockttp will work with any testing tools that can handle promises (and with minor tweaks, many that can't), and can mock requests from any library, tool or device you might care to use.
- Many thanks to https://github.com/vieiralucas for donating the package name!