![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
stub-service
Advanced tools
A simple and flexible npm package for creating stub servers to mock HTTP endpoints. This package allows you to define custom routes, methods, headers, and responses for testing your applications.
To start the stub server, create an instance of StubService and call the start method with the desired port number. Call StubService.[HTTP Method]
to mock requests.
//import package
import { StubService } from "stub-service";
// create server instance
const stubService = new StubService();
// start server
await stubService.start(3000);
// configure
stubService.get("/api/users/1").reply(200, { id: 1, name: "John" });
stubService.get("/api/users/2").reply(200, { id: 2, name: "Jane" });
// sample tests
const { data } = await axios.get("http://localhost:3000/api/users/1");
expect(data).toEqual({ id: 1, name: "John" });
Host is localhost. The stub service will run on port provided to the .start()
method.
stubService
.get("/api/users/1", { headers: { authorization: `Bearer abc` } })
.reply(200, { id: 1, name: "John" });
stubService
.get("/api/users/1", { headers: { authorization: `Bearer xyz` } })
.reply(401, { error: "Access denied" });
stubService
.post("/api/users", { name: "John" })
.reply(200, { id: 1, name: "John" });
To stop the stub server, call the stop method.
stubService.stop();
To reset the stub server between tests, use the reset method. This clears all the defined routes and responses.
stubService.reset();
This project is licensed under the MIT License.
FAQs
Simple Stub service
The npm package stub-service receives a total of 3 weekly downloads. As such, stub-service popularity was classified as not popular.
We found that stub-service demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.