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

MSW with Angular Component Testing: Failed to register a Service Worker #26131

Open
inigoiparragirre opened this issue Mar 16, 2023 · 8 comments
Labels
CT Issue related to component testing

Comments

@inigoiparragirre
Copy link

Current behavior

For Angular Component Testing, I want to use MSW (Mock Service Worker), using msw request handlers instead of cy.intercept().
The tested component is not working as expected

Desired behavior

MSW needs to be registered succesfully. But this is what happens:

component-testing-msw-angular

Steps to reproduce:

  • Install msw as dependency.
  • Execute 'npx msw init src/'
  • Add msw setup inside test.

Test code to reproduce

I forked the repository cypress-component-testing-apps in https://github.com/inigoiparragirre/cypress-component-testing-apps with an example for AppComponent using MSW (main branch).

Cypress Version

12.8.1

Node version

18.14.1

Operating System

Linux Ubuntu 22.10

Debug Logs

No response

Other

No response

@lmiller1990
Copy link
Contributor

Hi, perhaps duplicate of this issue in the msw repo: mswjs/msw#744

Looks like someone might have a solution in: #16742

Also related: mswjs/msw#744

@inigoiparragirre
Copy link
Author

Hi @lmiller1990, this issue is specific to Angular Component Testing. React Component Testing with MSW is working perfectly fine in this repository: cypress-component-testing-apps, so I think the issue is in the configuration files for Webpack in an Angular environment. So not sure about this is duplicated. Thank you!

@lmiller1990
Copy link
Contributor

I am not able to get it to work in the other project in the repository - I'm not seeing the same error, but I'm also not getting back the expected response. Which project(s) did you see it working in?

@lmiller1990
Copy link
Contributor

Actually, it's working now in my Vite project - I can use msw to intercept and respond to /blah as shown here:

Image

@lmiller1990
Copy link
Contributor

Seems Angular specific. The other issue has an Angular example that is also not working: mswjs/msw#744 (comment) but I got this to work in a simple project (both webpack and Vite).

@lmiller1990
Copy link
Contributor

I spent a few hours trying to figure this out but I am still unsure. I suppose it's something to do with how Angular routes publicPath requests, but I'm yet to figure out how/what is different compared to other webpack based toolchains.

@lmiller1990 lmiller1990 added the CT Issue related to component testing label Mar 28, 2023
@mct-dev
Copy link

mct-dev commented May 17, 2023

I am having the exact same issue but with NextJs instead of Angular. Would love to hear if anyone's been able to get this working! :)

@lmiller1990 lmiller1990 removed their assignment Jun 28, 2023
@lydemann
Copy link

I tried with the esbuild builder in Angular 16 and it still went looping when I was fetching the MSW service worker.

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

No branches or pull requests

4 participants