Node.js Legacy Mock Modules (vojtajina/node-mocks)

0.0.15 · abandoned · verified Wed Apr 22

The `mocks` package (npm: `mocks`), version 0.0.15, is an abandoned Node.js library initially created to provide basic mock implementations for the built-in `fs` (file system) and `http` modules for unit testing. Last published in August 2013, this package targets extremely old Node.js versions (specifically `>= 0.6.5`), which corresponds to Node.js releases from 2011-2012. The API provided is a very limited subset of the actual Node.js `fs` and `http` functionalities, having been developed primarily to support the testing needs of `Testacular` (now known as Karma). Given its lack of updates for over a decade, it does not support modern JavaScript features like ESM, contemporary Node.js APIs, or TypeScript type definitions. Developers should consider modern, actively maintained alternatives such as `mock-fs` for file system mocking, `node-mocks-http` or `nock` for HTTP requests, and general-purpose testing frameworks with robust mocking capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `mocks` package to set up a mock file system (using `_set` and `readFileSync`) and create mock HTTP request/response objects for basic unit testing scenarios in a legacy Node.js environment.

const mocks = require('mocks');
const fs = mocks.fs; // Get the mocked fs module
const http = mocks.http; // Get the mocked http module

// Configure the mock file system with files and directories
fs._set('/app/data/config.json', JSON.stringify({ version: '1.0.0', enabled: true }));
fs._set('/app/logs', {}); // Mock an empty directory
fs._set('/app/src/main.js', 'console.log("Hello from mocked file!");');

console.log('--- Mocking File System ---');
// Simulate reading a file synchronously
try {
  const configContent = fs.readFileSync('/app/data/config.json', 'utf8');
  console.log('Read config.json:', configContent);
  const config = JSON.parse(configContent);
  console.log('Parsed config version:', config.version);
} catch (e) {
  console.error('Error reading config.json:', e.message);
}

// Simulate reading a non-existent file
try {
  fs.readFileSync('/app/data/nonexistent.txt');
} catch (e) {
  console.log('Expected error for non-existent file:', e.message);
}

console.log('\n--- Mocking HTTP Request/Response ---');
// Mock an HTTP server request and response for testing a handler
const mockRequest = new http.ServerRequest({
  url: '/api/items/123',
  method: 'GET',
  headers: { 'Accept': 'application/json', 'User-Agent': 'MockTest' }
});

const mockResponse = new http.ServerResponse();
mockResponse.statusCode = 200;
mockResponse.setHeader('Content-Type', 'application/json');
mockResponse.end(JSON.stringify({ id: '123', name: 'Mock Item' }));

console.log('Mock HTTP Request URL:', mockRequest.url);
console.log('Mock HTTP Request Method:', mockRequest.method);
console.log('Mock HTTP Response Status Code:', mockResponse.statusCode);
console.log('Mock HTTP Response Headers:', mockResponse.getHeaders());
console.log('Mock HTTP Response Body:', mockResponse._getData().toString()); // _getData() retrieves the buffered response

view raw JSON →