
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
api-mock-simplifier
Advanced tools
API Mocking and Testing Simplifier is a lightweight and flexible tool for mocking HTTP requests and testing APIs in Node.js projects. This package helps developers create API mocks for testing without hitting real endpoints, enabling faster and more isola
API Mocking and Testing Simplifier is a lightweight and flexible tool for mocking HTTP requests and testing APIs in Node.js projects. This package helps developers create API mocks for testing without hitting real endpoints, enabling faster and more isolated unit and integration tests.
GET, POST, PUT, DELETE, etc.To install the package, use npm:
npm install api-mock-simplifier
Additionally, you need axios and jest for HTTP handling and testing:
npm install axios jest axios-mock-adapter
Here’s how to use the API Mocking and Testing Simplifier in your tests:
The package simplifies API mocking by intercepting HTTP requests using axios and axios-mock-adapter.
const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
// Create a new mock instance
const mock = new MockAdapter(axios);
// Mocking a GET request to /api/users
mock.onGet('/api/users').reply(200, {
users: [{ id: 1, name: 'Alice' }],
});
// Making a GET request using axios
axios.get('/api/users').then(response => {
console.log(response.data.users); // [{ id: 1, name: 'Alice' }]
});
You can use this package with Jest to mock APIs and perform tests.
const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
describe('API Mocking Tests', () => {
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.reset();
});
test('should mock GET request to /api/users', async () => {
mock.onGet('/api/users').reply(200, { users: [{ id: 1, name: 'Alice' }] });
const response = await axios.get('/api/users');
expect(response.data.users).toEqual([{ id: 1, name: 'Alice' }]);
});
test('should mock POST request to /api/users', async () => {
const newUser = { id: 2, name: 'Bob' };
mock.onPost('/api/users').reply(201, newUser);
const response = await axios.post('/api/users', newUser);
expect(response.status).toBe(201);
expect(response.data).toEqual(newUser);
});
});
You can mock various HTTP methods like POST, PUT, and DELETE.
// Mock POST request
mock.onPost('/api/users').reply(201, { id: 2, name: 'Bob' });
// Mock PUT request
mock.onPut('/api/users/2').reply(200, { id: 2, name: 'Bob Updated' });
// Mock DELETE request
mock.onDelete('/api/users/2').reply(204);
You can easily mock error responses, such as 404 or 500 status codes.
// Mock 404 error for GET request
mock.onGet('/api/users/999').reply(404);
// Mock 500 error for POST request
mock.onPost('/api/users').reply(500);
If you want to test using real APIs and switch back to mock APIs when necessary, you can reset or disable the mocks:
// Disable all mocks
mock.restore();
Here’s the recommended file structure for organizing your project:
api-mock-simplifier/
│
├── src/
│ ├── interceptors/
│ │ ├── axiosInterceptor.js # Axios interceptor for API requests
│ │ └── fetchInterceptor.js # Fetch interceptor for API requests
│ ├── mocking/
│ │ ├── schemaMocker.js # Schema-based mocking for API responses
│ │ └── mockEngine.js # Core engine for mocking
│ └── index.js # Main entry point for setting up mocking
│
├── tests/
│ └── mockAPITest.spec.js # Jest test cases for mocking APIs
│
├── package.json # NPM configuration file
└── README.md # Documentation
Contributions are welcome! Feel free to open issues or submit pull requests if you have suggestions, improvements, or bug fixes.
git checkout -b feature/my-feature).git commit -am 'Add some feature').git push origin feature/my-feature).This project is licensed under the MIT License. See the LICENSE file for details.
axios and axios-mock-adapter.Feel free to adapt the content as per your project-specific needs!
FAQs
API Mocking and Testing Simplifier is a lightweight and flexible tool for mocking HTTP requests and testing APIs in Node.js projects. This package helps developers create API mocks for testing without hitting real endpoints, enabling faster and more isola
We found that api-mock-simplifier demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.