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

server.listen() called after server.close() does not mock response #2370

Closed
4 tasks done
vishakha94 opened this issue Nov 28, 2024 · 5 comments · Fixed by #2383
Closed
4 tasks done

server.listen() called after server.close() does not mock response #2370

vishakha94 opened this issue Nov 28, 2024 · 5 comments · Fixed by #2383
Assignees
Labels
bug Something isn't working scope:node Related to MSW running in Node

Comments

@vishakha94
Copy link

vishakha94 commented Nov 28, 2024

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 18 or higher

Node.js version

v2.6.6

Reproduction repository

vishakha94@b90344e

Reproduction steps

npm run test:node

Current behavior

We have a NextJS app that implements mocking using feature flag. When we switch the flag off for mocking, we call server.close() and if the feature flag is enabled again, serve.listen() is called.
I observed the following:

  1. the first call to server.listen() mocks API properly
  2. The second call to server.listen() after server.close() does not mock API

Our NextJS app setup is complicated, but I was able to reproduce the issue here: https://github.com/vishakha94/msw/tree/repro-server-close

I ran npm run test:node

Expected behavior

Expect for MSW to resume mocking after server.listen() is called after sever.close().

@vishakha94 vishakha94 added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:node Related to MSW running in Node labels Nov 28, 2024
@vishakha94 vishakha94 changed the title serve.listen() called after serve.close() does not mock response server.listen() called after serve.close() does not mock response Nov 28, 2024
@vishakha94 vishakha94 changed the title server.listen() called after serve.close() does not mock response server.listen() called after server.close() does not mock response Dec 2, 2024
@kettanaito kettanaito changed the title server.listen() called after server.close() does not mock response server.listen() called after server.close() does not mock response Dec 6, 2024
@kettanaito
Copy link
Member

Hi, @vishakha94. Thanks for reporting this.

I was able to reproduce this issue in isolation in #2383. Will take a look at whats's wrong once I have a moment.

@kettanaito kettanaito self-assigned this Dec 6, 2024
@kettanaito
Copy link
Member

Root cause

Calling server.close() triggers this.emitter.removeAllListeners() on the interceptor. Starting the server again doesn't seem to attach those listeners again, causing no request listener to be present to handle the intercepted request.

@kettanaito
Copy link
Member

I've pushed the fix to the PR. Once the CI is green, I will merge it.

@kettanaito kettanaito removed the needs:triage Issues that have not been investigated yet. label Dec 6, 2024
@kettanaito
Copy link
Member

Released: v2.6.8 🎉

This has been released in v2.6.8!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

@vishakha94
Copy link
Author

Thanks @kettanaito!

@github-actions github-actions bot locked and limited conversation to collaborators Dec 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working scope:node Related to MSW running in Node
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants