AngelaE / ts-mountebank

Mountebank client for TS / node
15 stars 5 forks source link

ts-mountebank

A simple node package that lets you manage your Mountebank test server. Based on https://www.npmjs.com/package/@toincrease/node-mountebank, but

Usage

Pre-Requisite

Install Mountebank locally or run in docker. The easiest local install is as follows, otherwise check out options in the integration-tests.

npm install -g mountebank --production

Start Mountebank:

mb 

I recommend reading the Mountebank documentation for a deeper understanding of their API.

For more samples on how to use this package check this blog: https://angela-evans.com/easy-api-tests-with-mountebank/ or the integration tests.

Create Imposter

const mb = new Mountebank();
let imposter = new Imposter().withPort(port).withStub(
    new DefaultStub(testPath, HttpMethod.GET, 'testbody', 222));
await mb.createImposter(imposter);

Check for QueryString

Add a query to the stub. For usages check the tests in ./integration-tests/predicate.flexi-predicate.query.mb-tests.ts

new Stub()
    .withPredicate(
    new FlexiPredicate()
        .withOperator(Operator.equals)
        .withPath('/testpath')
        .withQuery({name: 'x', max: 5})
    )
    .withResponse(new DefaultResponse('found', 222))

Create Debug Proxy

A simple proxy which always proxies the request and records the responses. This is useful to

// forward all requestsfrom port 5000 => 5001 and generate response stubs
let proxyImposter = new Imposter()
    .withPort(5000)
    .withStub(new Stub()
        .withProxy(new DebugProxy(`http://localhost:5001}`))); 

await mb.createImposter(proxyImposter);

Contributing

Optional Pre-Requisite Devbox.sh

You can use devbox.sh from jetpack.io to setup the local dev environment tooling.

  1. Motivation
  2. Install devbox.sh
  3. Start devbox.sh using devbox shell

Check the integration-tests for notes on how to run and test the package locally.