Node.js Fake Test Helper

0.2.2 · abandoned · verified Sun Apr 19

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

const Fake = require('fake');

const myService = {
  getData: () => 'Real data from DB',
  process: (input) => `Processing: ${input}`
};

console.log('--- Before Faking ---');
console.log('getData:', myService.getData());
console.log('process:', myService.process('initial input'));

// 1. Create a fake for 'getData'
const fakeGetData = new Fake(myService, 'getData', () => 'Mocked data from fakeGetData');

// 2. Apply the fake
fakeGetData.run();

console.log('\n--- During Faking (Test Context) ---');
console.log('getData:', myService.getData()); // Expected: 'Mocked data from fakeGetData'
console.log('process:', myService.process('input during fake')); // Still original

// 3. Create another fake for 'process'
const fakeProcess = new Fake(myService, 'process', (input) => `Faked process for: ${input.toUpperCase()}`);
fakeProcess.run();

console.log('\n--- With Multiple Fakes ---');
console.log('getData:', myService.getData()); // Still 'Mocked data from fakeGetData'
console.log('process:', myService.process('another test input')); // Expected: 'Faked process for: ANOTHER TEST INPUT'

// 4. Restore all fakes after the test
fakeGetData.restore();
fakeProcess.restore();

console.log('\n--- After Restoring ---');
console.log('getData:', myService.getData()); // Expected: 'Real data from DB'
console.log('process:', myService.process('final input')); // Expected: 'Processing: final input'

view raw JSON →