[Feature Request] MSW adapter
Closed this issue · 1 comments
FranciscoKloganB commented
Would be nice to be able to create request interceptors to create mock responses without mocking Fetch API and/or axios libs, for example using MSW and Faker.
Right now we do:
const users = await zodios.getUsers()
What I was think about was something like
// some stupid integration test in React (but not really related with react)
const zodiosMocker = new ZodiosRequestInterceptor(zodiosInstance)
beforeEach(() => {
// clear MSW interceptors - could be done on jest.setup.ts or vitest.setup.ts
zodiosMocker.clearAllInterceptors()
})
test('displays user first name', () => {
zodiosMocker.intercept('getUsers', 200) // generates random data based on schema for ok response
zodiosMocker.intercept('getUsers', 401) // generates random data based on schema for unauthorized response
zodiosMocker.intercept('getUsers', 200, { alice: 'bob' }) // pass a custom object to override some keys of the automatically generated data (not type checked)
zodiosMocker.intercept('getUsers', 200, () => ({ foo: 'bar' })) // pass a custom factory function (does not autogen anything with faker, simply calls the factory function)
render(<Component />)
expect(getByText('alice')).toBeVisible()
})
I don't know if I should say this... I'm not a TypeScript wizard... But I would not mind contributing to this in some way. 😓
I have a very rudimentary implementation of this setup based on vanilla JS Classes (without zodios).
stale commented
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.