Replies: 4 comments 1 reply
-
|
How it could look like in node environment, inspired by https://github.com/apollographql/graphql-testing-library import { createMswHandler } from "counterpart";
import counterpartConfig from "../counterpartConfig.json";
const { handler, replaceHandlers } = createMswHandler(counterpartConfig, {
// msw config
delay: number | "infinite" | "real",
});
// setup
import { setupServer } from 'msw/node';
import { handler } from './counterpartHandler';
export const server = setupServer(handler);
// usage
describe('MyComponent', () => {
it('should make API request', () => {
using _disposed = replaceHandlers({
'user/{userId}': ($) => $.response[200].random(),
});
render(<MyComponent />);
});
}); |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the suggestion! I think what you're suggesting is very similar to #730 I think it's a low hanging fruit. I've just been waiting for someone to come along and say "I would use this". :) |
Beta Was this translation helpful? Give feedback.
-
|
@SPAHI4 I'm curious about your use case. Are you using MSW in the browser or server mode? I'm starting to pull together the code and documentation for using Counterfact with MSW in browser mode and not really working on how it will work in Node (Node should be simpler). I don't want to leave the Node world out but this first pass basically satisfies the Vitest Browser + MSW combination. |
Beta Was this translation helpful? Give feedback.
-
|
Version 1.4.0 supports MSW, but I didn't really write up how it works at present. Here's a quick rundown: There are two new functions exported by Counterfact:
Here's a sample code site for Vitest Browser mode: Now I can use these to create my handlers for MSW: For now that Hope that helps us get everyone going while we keep improving the DX. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think it's a good idea to use counterfact in integration tests.
So, it could integrate into the MSW server and provide the ability to override a request for a specific test case with type safety.
Beta Was this translation helpful? Give feedback.
All reactions