Created
January 30, 2019 18:40
-
-
Save offirgolan/c0a8226203a186696fb553b6835634a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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