Mock Service Worker
mswjs.io
Mock Service Worker - API mocking library for browser and ...
I found MSW and was thrilled that not only could I still see the mocked responses in my DevTools, but that the mocks didn't have to be written in a Service Worker and could instead live alongside the rest of my app. This made it silly easy to adopt. The fact that I can use it for testing as well makes MSW a huge productivity booster. Kent C. Dodds ... A single source of truth for your network across the entire stack. ... Create, change, and debug fast by augmenting existing APIs—both third-party and local—or designing them as you go.
Docs
This leads to a more resilient setup and, paired with other library features, creates a truly seamless API mocking experience. MSW is designed to be fully environment-, framework- and tool-agnostic. You can use it in any browser or Node.js process without additional configurations, adapters, ...
Quick start
MSW applies on the environment level (Node.js or browser). Integrating it with different tools will come down to finding the appropriate environment entrypoint provided to you by that tool and following the same setup as you did above. For reference, we’ve prepared a curated collection of usage examples: ... Examples of Mock ...
Best practices
You are viewing the docs for MSW 2.0. To access the 1.x docs click here · Our mission is to help you describe the network you want. With that in mind, we have prepared a set of best practices you can employ to improve your network descriptions
Getting started
MSW applies on the environment level (Node.js or browser). Integrating it with different tools will come down to finding the appropriate environment entrypoint provided to you by that tool and following the same setup as you did above. For reference, we’ve prepared a curated collection of usage examples: ... Examples of Mock ...
Mocks-server
mocks-server.org
Mocks Server
Node.js mock server running live, interactive mocks in place of real APIs. Makes able to define different responses for the same route. The user can choose which response has to be used by each route on each particular moment.
mocking - node.js: Mock http request and response - Stack Overflow
Is there convenient way to mock the HTTP Request and Response objects for unit testing middlewares? More on stackoverflow.com
Please recommend a good API Mocking tool
jest + msw + faker More on reddit.com
I built a lightweight NodeJS mocking server
What's the advantage of this over just mocking the request in a regular test? More on reddit.com
Mock unit test an API that uses postgres or integration test API with a "test" database?
For smaller tests or small projects, I mock the DB handler (repository or whatever). For later stuff, I tend to go all the way, have a separate database that gets seeded, all tests are performed against it, then it gets shut down. Testcontainers work well with this. More on reddit.com
Videos
03:53
Generate Node.js Mock Servers with Ease: ChatGPT's Complete Guide.
Tired Of Hard Coding Data? Build a Mock Data API with Node.js
13:29
Mocking a Database in Node with Jest - YouTube
25:58
Free Mock Data NodeJS REST API for Angular v17 | Fullstack ...
12:35
Mocking APIs The Right Way - No More Postman - YouTube
11:42
Build a Mock REST API in Seconds with JSON Server - YouTube
GitHub
github.com › nock › nock
GitHub - nock/nock: HTTP server mocking and expectations library for Node.js · GitHub
const nock = require('nock') const scope = nock('https://api.github.com') .get('/repos/atom/atom/license') .reply(200, { license: { key: 'mit', name: 'MIT License', spdx_id: 'MIT', url: 'https://api.github.com/licenses/mit', node_id: 'MDc6TGljZW5zZTEz', }, }) This setup says that we will intercept every HTTP call to https://api.github.com. It will intercept an HTTPS GET request to /repos/atom/atom/license, reply with a status 200, and the body will contain a (partial) response in JSON.
Starred by 13.1K users
Forked by 754 users
Languages JavaScript 96.1% | TypeScript 3.9%
Kettanaito
kettanaito.com › blog › mocking-in-nodejs-has-just-changed-forever
Mocking in Node.js Has Just Changed Forever - kettanaito.com
February 13, 2025 - Nock is an API mocking library that has been around for almost 15 years. You’ve definitely heard of it and you’ve likely used it as well. It’s extremely popular to this day because it’s extremely good. To me, Nock is also a very special library. When I set off to bring Mock Service Worker to Node.js ...
GitHub
github.com › nahidf › mock-api
GitHub - nahidf/mock-api: A node.js mock api project
mock-api is a simple node.js project that lets you easily mock up APIs, generate custom data, and preform operations on it using RESTful interface.
Forked by 2 users
Languages JavaScript 100.0% | JavaScript 100.0%
GitHub
github.com › mocks-server › main
GitHub - mocks-server/main: Node.js mock server running live, interactive mocks in place of real APIs · GitHub
Define your mocked API routes in YAML, JSON, JavaScript or TypeScript files. Mocks Server loads them automatically and watches for changes.
Starred by 312 users
Forked by 20 users
Languages JavaScript 94.1% | TypeScript 4.7%
Node.js
nodejs.org › en › learn › test-runner › mocking
Node.js — Mocking in tests
As with all module dependency mocking, this is necessary so that the ago module receives the mock before the ago module is executed (if the mocking does not occur before, it will be too late). This is especially useful when comparing against a static fixture (that is checked into a repository), such as in snapshot testing. PreviousUsing Node.js's test runnerNextCollecting code coverage in Node.js
npm
npmjs.com › package › api-mock
api-mock - npm
API-Mock is a node.js npm module that generates a mock server (express) from your API specification.
» npm install api-mock
Published Feb 18, 2016
Version 0.3.2
Author Evan Cordell
Repository https://github.com/localmed/api-mock
npm
npmjs.com › package › node-mock-server
node-mock-server - npm
May 9, 2020 - File based Node REST API mock server. Latest version: 0.25.4, last published: 6 years ago. Start using node-mock-server in your project by running `npm i node-mock-server`. There are 1 other projects in the npm registry using node-mock-server.
» npm install node-mock-server
Published May 09, 2020
Version 0.25.4
Author Simon Mollweide
Better Programming
betterprogramming.pub › build-a-nodejs-mock-server-api-with-random-data-86303db9156a
5 Steps to Build a Node.js Mock Server and API With Random Data
October 20, 2020 - It’s a good idea to start as soon as possible in talking with an external API where your data comes from. In this tutorial, I want to go through a minimal setup for mocking your data. After this, you should be able to extend it with your own data as needed. In order to work with Node, you need to have it installed on your machine. For Mac users, I highly recommend installing it with nvm because it’ll make it easier to update Node.js in the future.
DEV Community
dev.to › grawl › how-to-mock-an-api-with-random-data-from-nodejs-1j91
How to Mock an API with random data from NodeJS - DEV Community
April 4, 2022 - It's a good idea to start as soon as possible to talk with an external API where your data comes from. In this tutorial, I want to go through a minimal setup for mocking your data. After this, you should be able to extend it with your own data as needed. In order to work with Node you need to have it installed on your machine.
npm
npmjs.com › package › mockserver
mockserver - npm
August 25, 2019 - To do this, you need to use #import './code.js'; in first line of your mock file: #import './code.js'; Content-Type: application/json; charset=utf-8 Access-Control-Allow-Origin: * { "Random": "Content" } You import javascript modules to create dynamic code responses: ... module.exports = request.body.indexOf('foo') !== -1 ? 'HTTP/1.1 200 OK' : 'HTTP/1.1 400 Bad request' Tests run on travis, but if you wanna run them locally you simply have to run mocha or its verbose cousin ./node_modules/mocha/bin/mocha (if you don't have mocha installed globally).
» npm install mockserver
Published Aug 25, 2019
Version 3.1.1
Author Mohammad Hallal
Repository https://github.com/namshi/mockserver
Homepage https://github.com/namshi/mockserver
npm
npmjs.com › package › mock-api-server
mock-api-server - npm
March 26, 2017 - Mock out your API server for testing.. Latest version: 0.4.6, last published: 9 years ago. Start using mock-api-server in your project by running `npm i mock-api-server`. There are no other projects in the npm registry using mock-api-server.
» npm install mock-api-server
Top answer 1 of 9
28
It looks like both https://github.com/howardabrams/node-mocks-http and https://github.com/vojtajina/node-mocks can be used to create mock http.ServerRequest and http.ServerResponse objects.
2 of 9
11
From the tag, it looks like this question is about Express. In that case, supertest is very good:
var request = require('supertest')
, express = require('express');
var app = express();
app.get('/user', function(req, res){
res.send(201, { name: 'tobi' });
});
request(app)
.get('/user')
.expect('Content-Type', /json/)
.expect('Content-Length', '20')
.expect(201)
.end(function(err, res){
if (err) throw err;
});
For general Node use, Flatiron Nock looks like a good option:
var nock = require('nock');
var example = nock('http://example.com')
.get('/foo')
.reply(200, { foo: 'bar' });
var http = require('http');
var options = {
host: 'example.com',
port: 80,
path: '/foo',
method: 'GET'
}
var req = http.request(options, function(res) {
res.on('data', function(chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('error: ' + e);
});
req.end();
Output:
BODY: {"foo":"bar"}
Reddit
reddit.com › r/node › please recommend a good api mocking tool
r/node on Reddit: Please recommend a good API Mocking tool
May 16, 2023 -
Hi, I am looking fro a good API mocking tool. Can you please share your recommendation?
Top answer 1 of 5
13
jest + msw + faker
2 of 5
3
I'm the author of Wirepig , which I wrote this year after being disappointed with existing alternatives. A few things that I believe make it stand out: It doesn't require any monkey-patching, dependency injection, or other augmentation of your application code. The code you test is the same code you use in production. It supports HTTP and TCP dependencies – not only can you mock some backend API, but you can mock a redis cache or database (though, the latter might be a bit overkill for wirepig). It offers a very robust API right from within your node process; no need to fiddle around with other processes. Happy to answer any questions.