-
Notifications
You must be signed in to change notification settings - Fork 355
docs: Added example with TypeScript, Jest and Fetch #440
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
docs: Added example with TypeScript, Jest and Fetch #440
Conversation
a881e6f to
62cb76c
Compare
examples/typescript-jest-node-fetch/src/utils/auto-setup-polly.ts
Outdated
Show resolved
Hide resolved
offirgolan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR @justsml! Left a few minor comments but other than that lgtm.
…tsml/pollyjs into DL/add-example-typescript-jest-fetch
|
Hi @offirgolan , hope you're doing well! Please let me know if you are waiting on me for anything; I believe I've included all your suggestions. 🤘 Thanks in advance for your time! |
| import { getUser } from './github-api'; | ||
|
|
||
| describe('github-api client', () => { | ||
| let pollyContext = autoSetupPolly(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm testing this library in our test suite. My feeling is that Polly should be as automatic as possible and devs shouldn't have to think about it (unless they want to mock a request). I found that I could set up Polly without having to add anything manually to tests using this:
// tests/setupTestsAfterEnv.ts
import path from 'path';
import { setupPolly } from 'setup-polly-jest';
import NodeHttpAdapter from '@pollyjs/adapter-node-http';
import FSPersister from '@pollyjs/persister-fs';
global.pollyContext = setupPolly({
adapters: [NodeHttpAdapter],
persister: FSPersister,
persisterOptions: {
fs: {
recordingsDir: path.resolve(__dirname, '__recordings__'),
},
},
// logLevel: 'info',
recordIfMissing: false,
recordFailedRequests: true,
expiresIn: '30d',
});
// tests/global.d.ts
// note that you will have to include this in your tsconfig.json somehow (automatically or manually)
import { setupPolly } from 'setup-polly-jest';
declare global {
/* eslint-disable-next-line @typescript-eslint/no-namespace */
namespace NodeJS {
interface Global {
pollyContext: ReturnType<typeof setupPolly>;
}
}
}
// jest.config.js
module.exports = {
// ...
setupFilesAfterEnv: ['./tests/setupTestsAfterEnv.ts'],
// ...
}This way pollyContext is magically available in any test.
Obviously this is a little bit specific, but what do you think about this approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I love where you're going @mcmire - I think Polly is 🔥 when it's transparent, so your code doesn't change depending on if you want to mock or not.
I was going to include something similar to this pattern! The reason I didn't is because I think an opt-in design can meet more use cases.
As it still allows you to wire it up in setupFilesAfterEnv scripts:
// `./tests/setupFilesAfterEnv.ts`
import { autoSetupPolly } from './src/utils/auto-setup-polly';
declare global {
namespace NodeJS {
interface Global {
pollyContext: ReturnType<typeof autoSetupPolly>;
}
}
}
global.pollyContext = autoSetupPolly();
export default global.pollyContext;Maybe another example or some comments in the README is warranted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah that's a fair point. This technique is also useful if you're slowly introducing Polly into a test suite, I'd imagine. In that case, an extra example would be nice, but not necessary.
|
Hi @justsml, this looks good to me! Let me know if this is ready to be merged |
|
Hi @offirgolan |
The PR adds an example using TypeScript, Jest, and Fetch.
It features a handy utility method (
autoSetupPolly()) I've refined across my last 2 integrations of this tool:To use in test files
Add this one-liner near the top of your test script:
For more complex use cases you can store a reference to the returned context:
Key files
examples/typescript-jest-node-fetch/src/utils/auto-setup-polly.tsgithub-api.tsgithub-api.test.tsI have added tests to cover my changes.
My change requires a change to the documentation.
I have updated the documentation accordingly.
My code follows the code style of this project.
My commits and the title of this PR follow the Conventional Commits Specification.
I have read the contributing guidelines.