{"id":17293,"library":"mocks","title":"Node.js Legacy Mock Modules (vojtajina/node-mocks)","description":"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.","status":"abandoned","version":"0.0.15","language":"javascript","source_language":"en","source_url":"git://github.com/vojtajina/node-mocks","tags":["javascript","mock","stub","dummy","test double","fake","nodejs","js","testing"],"install":[{"cmd":"npm install mocks","lang":"bash","label":"npm"},{"cmd":"yarn add mocks","lang":"bash","label":"yarn"},{"cmd":"pnpm add mocks","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"This package is CommonJS-only and does not support ES Modules. Using `import` will result in a runtime error.","wrong":"import mocks from 'mocks';","symbol":"mocks","correct":"const mocks = require('mocks');"},{"note":"Access the mocked `fs` module directly from the `mocks` object. The package does not automatically monkey-patch the global `fs` module.","wrong":"const fs = require('fs'); // This loads the real fs module unless patched differently","symbol":"mocks.fs","correct":"const fs = require('mocks').fs;"},{"note":"Access the mocked `http` module directly from the `mocks` object. It provides `ServerRequest` and `ServerResponse` constructors.","wrong":"const http = require('http'); // This loads the real http module","symbol":"mocks.http","correct":"const http = require('mocks').http;"}],"quickstart":{"code":"const mocks = require('mocks');\nconst fs = mocks.fs; // Get the mocked fs module\nconst http = mocks.http; // Get the mocked http module\n\n// Configure the mock file system with files and directories\nfs._set('/app/data/config.json', JSON.stringify({ version: '1.0.0', enabled: true }));\nfs._set('/app/logs', {}); // Mock an empty directory\nfs._set('/app/src/main.js', 'console.log(\"Hello from mocked file!\");');\n\nconsole.log('--- Mocking File System ---');\n// Simulate reading a file synchronously\ntry {\n  const configContent = fs.readFileSync('/app/data/config.json', 'utf8');\n  console.log('Read config.json:', configContent);\n  const config = JSON.parse(configContent);\n  console.log('Parsed config version:', config.version);\n} catch (e) {\n  console.error('Error reading config.json:', e.message);\n}\n\n// Simulate reading a non-existent file\ntry {\n  fs.readFileSync('/app/data/nonexistent.txt');\n} catch (e) {\n  console.log('Expected error for non-existent file:', e.message);\n}\n\nconsole.log('\\n--- Mocking HTTP Request/Response ---');\n// Mock an HTTP server request and response for testing a handler\nconst mockRequest = new http.ServerRequest({\n  url: '/api/items/123',\n  method: 'GET',\n  headers: { 'Accept': 'application/json', 'User-Agent': 'MockTest' }\n});\n\nconst mockResponse = new http.ServerResponse();\nmockResponse.statusCode = 200;\nmockResponse.setHeader('Content-Type', 'application/json');\nmockResponse.end(JSON.stringify({ id: '123', name: 'Mock Item' }));\n\nconsole.log('Mock HTTP Request URL:', mockRequest.url);\nconsole.log('Mock HTTP Request Method:', mockRequest.method);\nconsole.log('Mock HTTP Response Status Code:', mockResponse.statusCode);\nconsole.log('Mock HTTP Response Headers:', mockResponse.getHeaders());\nconsole.log('Mock HTTP Response Body:', mockResponse._getData().toString()); // _getData() retrieves the buffered response\n","lang":"javascript","description":"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."},"warnings":[{"fix":"Migrate to actively maintained mocking libraries like `mock-fs` (for file system), `nock` or `node-mocks-http` (for HTTP), or general testing frameworks (e.g., Jest, Sinon.js) for robust and current testing solutions.","message":"The `mocks` package is officially abandoned and has not been updated since August 2013. It is not compatible with modern Node.js versions or ES Modules, and its APIs are severely outdated and incomplete compared to current Node.js built-ins.","severity":"breaking","affected_versions":">=0.0.15"},{"fix":"Review the source code or limited documentation on the GitHub page to understand exactly which methods are supported. For broader API coverage, consider alternative libraries.","message":"The mock `fs` and `http` modules provided by this package implement only a very small subset of the actual Node.js APIs. Many common `fs` or `http` methods will be missing or have incomplete implementations, leading to unexpected runtime errors during testing.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Use CommonJS `require()` syntax (`const mocks = require('mocks');`) to import the package. If your project is ESM-only, you will need to use a different mocking library.","message":"This package is written exclusively in CommonJS and does not provide ES Module (ESM) entry points. Attempting to `import` it will result in a `SyntaxError` in ESM contexts.","severity":"gotcha","affected_versions":">=0.0.1"},{"fix":"Either create custom declaration files (`.d.ts`) or, preferably, migrate to a modern mocking library that offers built-in TypeScript support.","message":"There are no TypeScript type definitions available for the `mocks` package. Using it in a TypeScript project will result in type errors and lack of autocomplete, requiring manual type declarations.","severity":"gotcha","affected_versions":">=0.0.1"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Change `import mocks from 'mocks';` to `const mocks = require('mocks');` and ensure your environment supports CommonJS.","cause":"Attempting to use ES Module `import` syntax with the CommonJS-only `mocks` package.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"Consult the `mocks` package's GitHub repository for the limited list of supported methods. For broader API support, consider modern alternatives like `mock-fs` or `nock`.","cause":"Attempting to call an `fs` or `http` method that is not implemented by the `mocks` package.","error":"TypeError: fs.someUnsupportedMethod is not a function"},{"fix":"Ensure the package is installed via `npm install mocks` or `yarn add mocks` and that your Node.js resolution paths are correctly configured. Verify that `node_modules` is accessible.","cause":"The package is not installed or the Node.js runtime cannot resolve its path.","error":"Error: Cannot find module 'mocks'"}],"ecosystem":"npm","meta_description":null}