Mock Service Worker Library Enables Resilient REST and GraphQL API Testing –

Uncover what’s next for software engineering at QCon Plus (November 1-12) Register
Facilitating the spread of knowledge and innovation in professional software development

The Excel team announced LAMBDA, a new feature that lets users define and name formula functions. LAMBDA functions admit parameters, can call other LAMBDA functions and recursively call themselves. With LAMBDA, the Excel formula language is Turing-complete: user-defined functions can thus compute anything without resorting to imperative languages (e.g., VBA, JavaScript).
In this episode, Thomas Betts speaks with Tammy Bryant Butow, principal SRE at Gremlin about training new site reliability engineers. The discussion covers a formal SRE Apprenticeship program Tammy led at DropBox, and gets into ideas about the best way to teach people new technical skills.
In this second edition of the Modern Data Engineering eMag, we’ll explore the ways in which data engineering has changed in the last few years. Data engineering has now become key to the success of products and companies. And new requirements breed new solutions.  
Sprint 0 can be a great mechanism in Agile transformations to reset existing teams which are not delivering value, exhibiting a lack of accountability, or struggling with direct collaboration with customers. This article shares the experiences from doing a Sprint 0 with an existing team which was struggling to deliver, helping them to align to a new product vision and become a stronger team.
The panelists discuss monitoring and observability methods that DevOps and SRE teams can employ to balance change and uncertainty without the need to constantly reconfigure monitoring systems.
Learn how to apply containerized applications to improve application speed, reliability and deployment. Virtual Event on September 21th, 9AM EDT / 3PM CEST
Learn how to apply Microservices and DevSecOps to improve application security & deployment speed. Virtual Event on Oct 19th, 9AM EDT/ 3PM CEST
Turn advice from 64+ world-class professionals into immediate action items. Attend online on Nov 1-12.
InfoQ Homepage News Mock Service Worker Library Enables Resilient REST and GraphQL API Testing
This item in japanese
Nov 11, 2020 2 min read
Bruno Couriol
The Mock Service Worker (MSW) API mocking library allows developers to test web applications without using an actual back-end, setting up a mocking server, or stubbing native http/https/fetch implementations. MSW mocks both Rest and GraphQL APIs. MSW received the JavaScript Open Source Awards this year.
An increasingly popular approach to testing UI components consists of rendering and exercising the component under test as much as possible in the same conditions and in the same way as a real user would. The rationale is to avoid testing implementation details linked to the component’s internal API. The mentioned approach seeks to decrease the likelihood of having to change tests, not in response to a change in specifications, but in response to a change in implementation of the component.
However, as user actions are exercised on an actual rendered component, they may trigger real effects (e.g., remote data fetching, API calls) that need to be mocked or stubbed.
While a mocking server is an actual server, the mocking server may differ from the production server in ways that affect the confidence derived from the testing (e.g., different architecture, or technologies). A mocked server may also require to modify the component code to point requests to the mocked server endpoint.
The native stubbing approach avoids modifying the component code for testing purposes. However, having tests passing against an altered version of a native fetch browser method does not completely guarantee that the same tests will pass against the unaltered version of fetch.
MSW allows developers to mock Rest and GraphQL APIs without having to modify the component code or stubbing native http/https/fetch implementations.
MSW documentation explained how MSW works in the browser:
This library registers a Service Worker that listens to the application’s outgoing requests via the fetch event, directs those requests to the client-side library, and sends a mocked response, if any, to the worker to respond with.

As MSW intercepts requests at the network level instead of the application level, developers can use the fetching library of their choice (e.g., native fetch, axios, react-query). For the same reason, developers should still implement end-to-end tests to exercise the actual network layer.
Mocked responses can be observed in the browser dev tool. The following code example:
and a GET request will result in a mocked response visible in the browser’s Network tab:
Chrome DevTools Network screenshot with the request mocked
Other open-source libraries provide functionalities similar to MSW. Nock, an HTTP server mocking library, only supports Node.js. Nock, however, provides also an assertion API. JSON Server creates an actual HTTP server from a JSON file. JSON Server works in Node and the browser. None of the previous two libraries support mocking GraphQL APIs.
MSW received the JavaScript Open Source Awards this year in the Most Exciting Use of Technology category. MSW is open-source software distributed under the MIT license.

Protect Identities. Secure Digital Services Enable highly scalable and secure user access to web and mobile applications. Start free trial.
A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.
You need to Register an InfoQ account or or login to post comments. But there’s so much more behind being registered.
Get the most out of the InfoQ experience.
Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

A round-up of last week’s content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.
Focus on the topics that matter in software development right now.
Deep-dive with 64+ world-class software leaders. Discover how they are applying emerging trends. Learn their use cases and best practices.
Stay ahead of the adoption curve and shape your roadmap with QCon Plus online software development conference. and all content copyright © 2006-2021 C4Media Inc. hosted at Contegix, the best ISP we’ve ever worked with.
Privacy Notice, Terms And Conditions, Cookie Policy
Is your profile up-to-date? Please take a moment to review and update.
Note: If updating/changing your email, a validation request will be sent