{"id":14569,"library":"fake","title":"Node.js Fake Test Helper","description":"This package, `fake` (version 0.2.2), is an early JavaScript utility designed for creating test doubles, specifically 'faking' dependencies to isolate units under test. Released for very old Node.js environments (requiring Node.js >=0.4.0), it predates most modern testing frameworks and mocking libraries. The project appears to be abandoned, with its last release dating back to a period when Node.js was in its infancy. It provides basic mechanisms for stubbing or mocking parts of an application's dependencies, allowing developers to focus on the logic of a single component without external side effects. Given its age and lack of updates, it is not suitable for contemporary JavaScript development and lacks features prevalent in current mocking solutions like Jest or Sinon.js. Its release cadence is effectively nonexistent due to its abandonment.","status":"abandoned","version":"0.2.2","language":"javascript","source_language":"en","source_url":"git://github.com/felixge/node-fake","tags":["javascript"],"install":[{"cmd":"npm install fake","lang":"bash","label":"npm"},{"cmd":"yarn add fake","lang":"bash","label":"yarn"},{"cmd":"pnpm add fake","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The 'fake' package exports its 'Fake' constructor function directly via CommonJS `module.exports`. ES Module (ESM) `import` syntax is not supported and will result in runtime errors.","wrong":"import Fake from 'fake';\nimport { Fake } from 'fake';","symbol":"Fake","correct":"const Fake = require('fake');"},{"note":"After instantiating 'Fake', call the `run()` method on the instance to replace the target object's method with your faked implementation.","symbol":"Fake.prototype.run()","correct":"const Fake = require('fake');\nconst target = { method: () => 'original' };\nconst f = new Fake(target, 'method', () => 'faked');\nf.run(); // Applies the fake method to target.method\n// ... test execution ..."},{"note":"Crucially, call `restore()` on the `Fake` instance after your test or block to revert the faked method, preventing test pollution and ensuring a clean state for subsequent operations.","symbol":"Fake.prototype.restore()","correct":"const Fake = require('fake');\nconst target = { method: () => 'original' };\nconst f = new Fake(target, 'method', () => 'faked');\nf.run();\n// ... test execution ...\nf.restore(); // Reverts target.method to its original state"}],"quickstart":{"code":"const Fake = require('fake');\n\nconst myService = {\n  getData: () => 'Real data from DB',\n  process: (input) => `Processing: ${input}`\n};\n\nconsole.log('--- Before Faking ---');\nconsole.log('getData:', myService.getData());\nconsole.log('process:', myService.process('initial input'));\n\n// 1. Create a fake for 'getData'\nconst fakeGetData = new Fake(myService, 'getData', () => 'Mocked data from fakeGetData');\n\n// 2. Apply the fake\nfakeGetData.run();\n\nconsole.log('\\n--- During Faking (Test Context) ---');\nconsole.log('getData:', myService.getData()); // Expected: 'Mocked data from fakeGetData'\nconsole.log('process:', myService.process('input during fake')); // Still original\n\n// 3. Create another fake for 'process'\nconst fakeProcess = new Fake(myService, 'process', (input) => `Faked process for: ${input.toUpperCase()}`);\nfakeProcess.run();\n\nconsole.log('\\n--- With Multiple Fakes ---');\nconsole.log('getData:', myService.getData()); // Still 'Mocked data from fakeGetData'\nconsole.log('process:', myService.process('another test input')); // Expected: 'Faked process for: ANOTHER TEST INPUT'\n\n// 4. Restore all fakes after the test\nfakeGetData.restore();\nfakeProcess.restore();\n\nconsole.log('\\n--- After Restoring ---');\nconsole.log('getData:', myService.getData()); // Expected: 'Real data from DB'\nconsole.log('process:', myService.process('final input')); // Expected: 'Processing: final input'\n","lang":"javascript","description":"This quickstart demonstrates the core lifecycle of faking a method: creating a Fake instance, applying the fake with `run()`, and cleaning up by restoring the original method with `restore()`, crucial for isolated unit testing in older Node.js environments."},"warnings":[{"fix":"Migrate your testing setup to a modern, actively maintained testing framework or mocking library. If you must use this package, proceed with extreme caution and be aware of potential compatibility and security vulnerabilities.","message":"The 'fake' package (v0.2.2) is considered abandoned. It has not been updated since early Node.js versions (requiring Node.js >=0.4.0) and lacks ongoing maintenance, security patches, and modern features. For new projects, use actively maintained mocking libraries like Jest (for its built-in mocks), Sinon.js, or Testdouble.js.","severity":"deprecated","affected_versions":">=0.2.2"},{"fix":"Ensure your project uses CommonJS (`require`) for importing this package. If your project is ESM-first, you might need a CommonJS wrapper or, preferably, a different mocking library.","message":"This package is strictly CommonJS and does not support ES Modules (ESM) syntax ('import'). Attempting to use 'import' will result in a 'SyntaxError: Cannot use import statement outside a module' or 'TypeError: require is not a function'.","severity":"breaking","affected_versions":">=0.2.2"},{"fix":"It is strongly advised against using this package in modern Node.js environments. If indispensable, thoroughly test its compatibility with your specific Node.js version, recognizing that maintaining it will be a significant challenge.","message":"The package's declared engine `node: >=0.4.0` signifies it was built for very old Node.js runtimes. Running it on modern Node.js versions (e.g., Node.js 14+) might lead to unexpected behavior, deprecation warnings, or crashes due to significant API changes or removal of global functions.","severity":"breaking","affected_versions":">=0.2.2"},{"fix":"Always ensure `fakeInstance.restore()` is called in a `finally` block or your test runner's `afterEach` hook to guarantee proper cleanup and isolation between tests.","message":"The 'Fake' constructor directly modifies the target object's method in place. Failing to call `fakeInstance.restore()` after a test can lead to global state pollution, affecting subsequent tests, other modules, or application behavior outside the intended test scope. This is a common source of flaky and hard-to-debug tests.","severity":"gotcha","affected_versions":">=0.2.2"},{"fix":"For complex mocking scenarios requiring introspection, conditional faking, or detailed assertions, consider migrating to a feature-rich library like Sinon.js or leveraging the robust built-in mocking capabilities of Jest.","message":"This library offers only basic method replacement. It lacks advanced mocking features such as spies (to record calls), argument capturing, defining different return values based on arguments, or asserting call counts, which are standard in modern mocking frameworks.","severity":"gotcha","affected_versions":">=0.2.2"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Replace `import` statements with CommonJS `require()`: `const Fake = require('fake');`. Ensure your file is treated as CommonJS.","cause":"Attempting to use ES Module 'import' syntax (e.g., `import Fake from 'fake';`) with this CommonJS-only package.","error":"SyntaxError: Cannot use import statement outside a module"},{"fix":"The 'fake' package exports a constructor function. You must use `new Fake(...)` to create an instance: `const Fake = require('fake'); const myFake = new Fake(target, 'method', newMethod);`","cause":"Calling `require('fake')` directly as a function, or attempting to use the `Fake` export without the `new` keyword to instantiate it.","error":"TypeError: Fake is not a constructor"},{"fix":"Install the package using npm: `npm install fake` or `npm install --save-dev fake`.","cause":"The 'fake' package is not installed or not resolvable in the current Node.js environment or project.","error":"Error: Cannot find module 'fake'"},{"fix":"There is no direct fix for the 'fake' package itself. This warning is a symptom of severe compatibility issues. The only robust solution is to migrate away from this abandoned library to a modern alternative.","cause":"This and similar warnings (e.g., related to deprecated APIs or internal module loader behavior) can occur when running a very old, unmaintained package like 'fake' on modern Node.js runtimes.","error":"(node:XXXX) Warning: Accessing non-existent property 'foo' of module exports inside of a require call (or similar Node.js runtime warnings)"}],"ecosystem":"npm"}