{"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.","language":"javascript","status":"abandoned","last_verified":"Wed Apr 22","install":{"commands":["npm install mocks"],"cli":null},"imports":["const mocks = require('mocks');","const fs = require('mocks').fs;","const http = require('mocks').http;"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}