mockyeah

A powerful service mocking, recording, and playback utility.

Notice: These docs are for mockyeah 1.x & above. For legacy mockyeah 0.x, please see https://mockyeah.github.io/mockyeah/.

Getting Started with Server

mockyeah can spin up a real server that can mock or proxy requests. It can work in your local or a higher environment. Let's see how to set it up locally for a project:

  1. Create an example project and initialized with npm:

    $ mkdir example-app && cd example-app
    $ npm init --yes # defaults will be fine
  2. Install @mockyeah/server

    $ npm add --save-dev @mockyeah/server

    or

    $ yarn add --dev @mockyeah/server
  3. Create script file and add the source below:

    $ touch index.js
    const mockyeah = require('@mockyeah/server');
    
    mockyeah.get('/hello-world', { text: 'Hello World' });
  4. Run the script file with Node

    $ node index.js
  5. Open http://localhost:4001/hello-world

  6. Profit. You should see "Hello World" returned from your mock server.

Withe relative URLs like /hello-world above, if no mock matches, then mockyeah will response with a 404.

But mockyeah can accept absolute URLs like this:

  • https://localhost:4001/https://api.example.com?foo=bar
  • https://localhost:4001/https://service.example.com/foo/bar

This makes it easy to simply prepend the mockyeah server URL to your existing API URLs (see Connect Your App below).

With the proxy option, mockyeah will also transparently forward all non-matching requests onto their original URL, and send the response back to you. This enables working with real APIs by default, but partially mocking responses for some requests.

So if we added this mock:

mockyeah.get('https://service.example.com/foo/bar', {
  json: {
    hello: 'there'
  }
});

Then a request to https://localhost:4001/https://service.example.com/foo/bar would result in a response of { "hello": "there" }.

But any other request like https://localhost:4001/https://api.example.com?foo=bar would not hit any mocks so it would proxy to the actual https://api.example.com?foo=bar endpoint and respond normally (though perhaps with some additional headers from mockyeah).

const mockyeah = require('@mockyeah/server');

mockyeah.get('https://example./hello-world', { text: 'Hello World' });

fetch('https://example.local?ok=yes').then(async res => {
  const data = await res.json();
  console.log('data is now { fake: "response" }', data);
});

Connect Your App

You can use @mockyeah/fetch to automatically rewrite all fetch requests from your client-side or server-side app or unit tests to hit your new mockyeah server. See the proxy option.

Or you can just manually configure your app's service URLs to point to your mockyeah server.

CLI

You can use our command-line interface to start a mockyeah server. See more at @mockyeah/cli. You can also use it to record, play, or play all suites with your @mockyeah/server.


© 2020 mockyeah, Built with Gatsby