question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular MSW (Jest)

See original GitHub issue

First of all, thank you for writing this library. It’s been a joy to use it on a demo project.

Environment

Name Version
msw ^0.19.5
node v12.18.0
OS Windows 10

Request handlers

// handlers
import { rest } from 'msw';

export const handlers = [
	rest.get('https://rickandmortyapi.com/api/character', (req, res, ctx) => {
		console.log('MSW Handler');
		return res(
			ctx.status(202, 'Mocked status'),
			ctx.json({
				message: 'Mocked response JSON body',
			}),
		);
	}),
];

// server
import { setupServer } from "msw/node";
import { handlers } from "./handlers";

export const server = setupServer(...handlers);

Actual request

export class RickAndMortyService {
	constructor(private http: HttpClient) {}

	getCharacters() {
		return this.http.get('https://rickandmortyapi.com/api/character');
	}
}

Current behavior

The requests reaches the MSW handler, but Angular doesn’t receive the response.

Expected behavior

I would expect that the Angular HTTP client handles the request/response correctly. This only happens with Angular + Jest, with Jasmine/Karma Angular does receive the response.

Observations

What I see is that Angular uses addEventListener to receive XHR responses. When I rewrite this line, by just using the onload function, the response gets handled correctly. I have no idea why, and I’m not sure where to look. I was wondering if you have seen this behavior before with other libraries.

const onLoad = () => {/* Angular's implementation */}

// onLoad is never invoked
// xhr.addEventListener('load', onLoad);     

// this works
xhr.onload  = onLoad;

The Angular source code can be found here

An older reproduction of this can be found here

To reproduce:

  • clone
  • run yarn
  • run yarn test:jest to run the jest test
  • run yarn test to run the jasmine test

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
chrisguttandincommented, Jul 11, 2020

Tim already wrote a blog post on the topic: https://timdeschryver.dev/blog/using-msw-in-an-angular-project

2reactions
marcosvega91commented, Jul 11, 2020

Okok I see, but maybe we could add angular project example here in this way with new updates we are pretty sure that they will work on angular and react. On the CI there is a step that run examples with new version of MSW

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using MSW (Mock Service Worker) in an Angular project
Using MSW to mock your server in an Angular project. ... Don't worry, the mocks are reused but because Jest runs in a...
Read more >
Examples – MSW - Mock Service Worker
Learn how to mock a RESTful API in an Angular application. Libraries & Frameworks. Examples below are focused on the integration of Mock...
Read more >
Mocking with MSW and Nx - Angular Tips
Introducing MSW. MSW as they say, is the API mocking of the next generation. Right, but what does that mean? It works by...
Read more >
How to fix Jest and Mock Service Worker integration errors
In a recent project, my team decided to use Mock Service Worker (MSW) in our unit tests. It was meant to be a...
Read more >
msw - Mock Service Worker - npm
Start using msw in your project by running `npm i msw`. ... Take a look at the example of an integration test in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found