-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] - Vitest support #499
Comments
polly + native fetch in node 20.11 with vitest is working for me
|
This is really great, thanks @ekrata-main! I took this a step further and tried to add sensible defaults where possible. /**
* Sets up Polly for recording and replaying HTTP interactions in tests.
*
* @param {Object} [options={}] - Configuration options for the recording.
* @param {string} [options.recordingName] - The name of the recording. If not provided, the suite name will be used.
* @param {string} [options.recordingPath] - The path to save the recordings. If not provided, the recordings will be saved in a "__recordings__" directory next to the test file.
*/
export function useRecording(
options: { recordingName?: string; recordingPath?: string } = {},
) {
beforeAll((suite) => {
polly = new Polly(options.recordingName ?? suite.name, {
adapters: ['fetch'],
mode,
recordIfMissing,
recordFailedRequests: true,
persister: 'fs',
persisterOptions: {
fs: {
recordingsDir:
options.recordingPath ??
`${suite.file.filepath.substring(0, suite.file.filepath.lastIndexOf('/'))}/__recordings__`,
},
},
});
});
beforeEach((context) => {
// Overwrite recording name on a per-test basis
polly.recordingName = options.recordingName ?? context.task.name;
});
afterAll(async () => {
await polly.stop();
});
return;
} Now, in a test, I can call import { describe, expect, test } from 'vitest';
import { useRecording } from '../../test-utils.js';
describe('integration', function () {
useRecording();
test('returns correct `posts` data', async function () {
const res = await fetch('https://jsonplaceholder.typicode.com/posts/2');
const post = await res.json();
expect(res.status).to.equal(200);
expect(post.id).to.equal(2);
});
}); The results:
|
Excellent. Thanks. Got it working. I like your organisation of recordings more than the default with jest. |
Vitest is getting increasingly popular.
We were using jest, but have moved to vitest because it was much simpler/easier with Typescript & ESM modules (just works - whereas it we had lots of config trial and error trying to get jest to work, before we gave up)
We still have our polly tests stuck on jest however, as we are using setup-polly-test ....
Any plans or work in progress to support vitest ?
The text was updated successfully, but these errors were encountered: