Skip to content
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

Pass data to MSW handlers, like the create fake data functions. #1114

Closed
Tracked by #1115
stijnvanhulle opened this issue Aug 7, 2024 Discussed in #1062 — with Huly for GitHub · 3 comments
Closed
Tracked by #1115

Pass data to MSW handlers, like the create fake data functions. #1114

stijnvanhulle opened this issue Aug 7, 2024 Discussed in #1062 — with Huly for GitHub · 3 comments
Assignees
Milestone

Comments

Copy link
Collaborator

stijnvanhulle commented Aug 7, 2024

Discussed in #1062

Originally posted by Samuel-Morgan-Tyghe July 1, 2024
So theres alot of uses for passing in hardcoded values to our fake data, but to utilize this we then have to pass it into a handler.

why not pass data directly into the handler into the handler?

import { http } from "msw";
import { createGetProductSummaryMutationResponse } from "../../mocks/retail/createGetProductSummary";


export const getProductSummaryHandler = (extendedData) => http.post("*/v2/products/summary", function handler() {
    return new Response(JSON.stringify(createGetProductSummaryMutationResponse(extendedData)), {
        headers: {
            "Content-Type": "application/json",
        },
    });
});
export function createGetProductSummaryMutationResponse(extendedData): NonNullable<GetProductSummaryMutationResponse> {
    faker.seed([100]);
    return createProductSummary(extendedData);
}
@stijnvanhulle stijnvanhulle added the v3 Kubb v3 label Aug 11, 2024
@stijnvanhulle stijnvanhulle added this to the 3.0.0 milestone Aug 27, 2024
@stijnvanhulle stijnvanhulle mentioned this issue Sep 25, 2024
36 tasks
@stijnvanhulle stijnvanhulle self-assigned this Sep 25, 2024
@cthompson-avb
Copy link

I would also like to see plugin-faker decoupled from plugin-msw.

While faker is useful, I generally prefer to have my msw handlers return known, static, data rather than randomly generated values.

@stijnvanhulle
Copy link
Collaborator Author

@cthompson-avb And how would you provide your static data? The only thing I can think of is something like this:
This means the data needs to be provided in every handler. As in, defining the data will be outside of Kubb.

export const addPetHandler = (data: Partial<Pet>)=>  http.post('*/pet', function handler(info) {
  return new Response(JSON.stringify(data), {
    headers: {
      'Content-Type': 'application/json',
    },
  })
})

@stijnvanhulle
Copy link
Collaborator Author

@cthompson-avb v3 of Kubb now has a possibility to use msw without the faker plugin: https://v3.kubb.dev/plugins/plugin-msw/#parser. Set parser to 'data' and faker will not be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants