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

Calls to worker.use() remove handlers to same endpoint with different method #2129

Closed
lavoscore opened this issue Apr 10, 2024 · 1 comment · Fixed by #2145
Closed

Calls to worker.use() remove handlers to same endpoint with different method #2129

lavoscore opened this issue Apr 10, 2024 · 1 comment · Fixed by #2145

Comments

@lavoscore
Copy link

I'm trying to write dynamic handler builders, but I'm hitting this issue:

worker.use(
  http.get("/v1/issues", () => HttpResponse.json(/* some json */))
)

// this will make the get handler ignored!
worker.use(
  http.post("/v1/issues", () => HttpResponse.json(/* some json */))
);

I'm aware that repeated calls to worker.use prepend handlers to the existing context, and therefore order matters. But then why does this code work?

// both handlers work
worker.use(
  http.get("/v1/issues", () => HttpResponse.json(/* some json */)),
  http.post("/v1/issues", () => HttpResponse.json(/* some json */))
);

Since both handlers work when added in a single worker.use call, shouldn't this method take into account any methods previously added to the context so as not to remove them and provide the same end result?

@kettanaito
Copy link
Member

Hi, @lavoscore. Thanks for reporting this.

There's no difference between these two snippets:

worker.use(a)
worker.use(b)
worker.use(a, b)

It's prepending values to an array:

msw/src/core/SetupApi.ts

Lines 26 to 28 in 7cf34c1

public prepend(runtimeHandles: Array<RequestHandler>): void {
this.handlers.unshift(...runtimeHandles)
}

I've added regression tests for your use case and they are passing for worker.use() and server.use():

#2145

Could you please take a look at those tests and see if you can spot any difference compared to what you are doing? If the issue still persists for you, please provide a reproduction repository where I can see it. Thanks.

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

Successfully merging a pull request may close this issue.

2 participants