Miscellaneous
Wirepig helps you test software that relies on something over a network. If your code talks to any of the following, then wirepig can help:
Wirepig spins up an HTTP or TCP server that can programmatically behave like your software's actual dependencies. Simply tell wirepig what kinds of requests to emulate and point your application at it. No monkey-patching the runtime, no external processes, no dependency injection, just honest-to-goodness sockets.
Requires NodeJS >=14
.
Install with your favorite package manager:
npm install --save-dev wirepig
yarn add --dev wirepig
Pull in however you get your modules:
import { http, tcp, helpers, errors } from 'wirepig';
const { http, tcp, helpers, errors } = require('wirepig');
And get to mocking:
const dep = await http();
dep.mock({
req: { method: 'POST', pathname: '/bloop' },
res: { statusCode: 200, body: 'bloop' },
});
const res = await request.post(`http://localhost:${dep.port}/bloop`)
assert.strictEqual(res.statusCode, 200);
assert.strictEqual(res.text, 'bloop');
await dep.teardown();
Here we:
It's highly recommended that you utilize a test runner's lifecycle hooks to properly manage your mocks:
before(async function () {
this.dep = await tcp();
});
afterEach(function () {
this.dep.reset();
});
after(async function () {
await this.dep.teardown();
});
it('sends a GET request to redis', async function() {
this.dep.mock({
req: ['*2', '$3', 'GET', '$8', 'ns:bloop', ''].join('\r\n'),
res: ['$17', 'bloop-the-big-one', ''].join('\r\n'),
});
const client = createClient({ url: `redis://localhost:${this.dep.port}` });
await client.connect();
assert.strictEqual(await client.get('ns:bloop'), 'bloop-the-big-one');
await client.disconnect();
});
Making sense? Head over to the docs for a full accounting of what wirepig can do.
When things get confounding, set the NODE_DEBUG
environment variable to one
of the following:
$NODE_DEBUG | Behavior |
---|---|
wirepig | Logs general diagnostics |
wirepig.match | Logs diagnostics related to request matching |
wirepig* | Logs all diagnostics |
With the expanded output in your console, you ought to be able to figure out what's going on.
Alternatively, a tool like Wireshark can help you monitor all packets flowing between your application and wirepig, and might yield a clue.
Wirepig occupies a space similar to nock except instead of overriding node internals, wirepig operates as an independent server your application sends real requests to. Since you spin up wirepig in the same process as your tests, you still have programmatic control over it.
I'm optimistic this design will offer a few notable upsides:
The wirepig API strives to offer every desirable feature in a simple package, often at the expense of terseness. For example, there's no built-in magic for handling JSON, even though it's an exceptionally common serialization format (there are some handy helpers, though). First and foremost, wirepig wants to be a strong foundation other projects can build on top of (for example, a redis-specific mocking library).
Of course, wirepig isn't the best tool for every job, and even though it can technically mock a postgres database, that might not be the best idea depending on your needs. Other tools I highly recommend:
I'm eager to hear from you! Feature requests, bug reports, general feedback, or just an excuse to chat, drop into issues and let me know what's on your mind. I much prefer it over a pull request out of the blue, if even so that we can get on the same page before you spend any time working up a patch.