Skip to content

Instantly share code, notes, and snippets.

@offirgolan
Created January 30, 2019 18:40
Show Gist options
  • Save offirgolan/c0a8226203a186696fb553b6835634a5 to your computer and use it in GitHub Desktop.
Save offirgolan/c0a8226203a186696fb553b6835634a5 to your computer and use it in GitHub Desktop.
/*
Create a new polly instance.
Connect Polly to fetch. By default, it will record any requests that it
hasn't yet seen while replaying ones it has already recorded.
*/
const polly = new Polly('Simple Example', {
adapters: ['fetch'], // Hook into `fetch`
persister: 'local-storage', // Read/write to/from local-storage
});
const { server } = polly;
/* Use the client-side server to intercept all requests to /posts/:id */
server.get('/posts/:id').intercept((req, res) => {
const { id } = req.params;
/* Respond with our own custom json object */
res
.status(200)
.json({ id, title: `Post ${id}`, body: '👋 𝐌edium' });
});
/* Any networks requests here using fetch will be handled by the Polly instance. */
await fetch('/posts/1');
/* Calling `stop` will persist requests as well as disconnect from any connected adapters. */
await polly.stop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment