An opinionated integration between Jest and Polly.js.
yarn add --dev jest @jomaxx/jest-polly
# or with NPM
npm install --save-dev jest @jomaxx/jest-polly
In your package.json
{
"jest": {
"setupFilesAfterEnv": ["@jomaxx/jest-polly"]
}
}
Or in jest.config.js
module.exports = {
setupFilesAfterEnv: ['@jomaxx/jest-polly'],
};
In my.test.js
import '@jomaxx/jest-polly';
import fetch from 'node-fetch';
test('is ok', async () => {
const response = await fetch('https://www.google.com/', { method: 'HEAD' });
expect(response.ok).toBe(true);
});
Use the polly
instance to change default behavior. Read docs.
import { polly } from '@jomaxx/jest-polly';
import fetch from 'node-fetch';
polly.server
.any('https://www.google.com/')
.intercept((req, res) => res.sendStatus(500));
test('is not ok', async () => {
const response = await fetch('https://www.google.com/', { method: 'HEAD' });
expect(response.ok).not.toBe(true);
});