### Applying a Reusable Mock to Another Dependency in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md Similar to the previous snippet, this example from `test/b_spec.js` further illustrates the reusability of the `./spy` mock. It applies the same mock to a different dependency (`../some/other/dependency`), highlighting how a single mock definition can be used across multiple test files or scenarios. ```JavaScript var mock = require('mock-require'); mock('../some/other/dependency', './spy'); ... ``` -------------------------------- ### Replacing a Node.js Module with Another Module in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md This example illustrates `mock-require`'s capability to replace one module's export with another existing module's export. Here, the 'fs' module is mocked to return the 'path' module's exports, demonstrating how `require('fs')` will subsequently yield the same object as `require('path')`. ```JavaScript mock('fs', 'path'); require('fs') === require('path'); // true ``` -------------------------------- ### Defining a Reusable Mock Module in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md This snippet defines a simple JavaScript module (`test/spy.js`) that exports a function. This module is intended to be used as a reusable mock for other dependencies, returning a specific string when called. It serves as an example of creating a dedicated mock file. ```JavaScript module.exports = function() { return 'this was mocked'; }; ``` -------------------------------- ### Stopping All Active Module Mocks in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md This example illustrates the `mock.stopAll()` function, which clears all currently registered mocks. After mocking 'fs' and 'path', `fs1` and `path1` receive the mocked versions. Calling `mock.stopAll()` then ensures that `fs2` and `path2` retrieve the original, unmocked modules, demonstrating a complete reset of the mocking state. ```JavaScript mock('fs', {}); mock('path', {}); var fs1 = require('fs'); var path1 = require('path'); mock.stopAll(); var fs2 = require('fs'); var path2 = require('path'); fs1 === fs2; // false path1 === path2; // false ``` -------------------------------- ### Stopping a Specific Module Mock in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md This snippet demonstrates the `mock.stop(path)` API, showing how to cease mocking a specific module. After `mock('fs', ...)` is called, `fs1` gets the mocked version. Calling `mock.stop('fs')` restores the original 'fs' module for subsequent `require` calls, making `fs2` different from `fs1`. ```JavaScript var mock = require('mock-require'); mock('fs', { mockedFS: true }); var fs1 = require('fs'); mock.stop('fs'); var fs2 = require('fs'); fs1 === fs2; // false ``` -------------------------------- ### Ensuring Consistent Mocking Across Nested Dependencies in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md This advanced `reRequire` example shows how to ensure all levels of a dependency tree use a mock. When 'fs' is mocked, `fileToTest` and `otherDep` (which also depends on 'fs') initially retain the unmocked version due to caching. By explicitly calling `reRequire` on both `otherDep` and `fileToTest`, all modules are reloaded, ensuring the 'fs' mock is consistently applied throughout the dependency chain. ```JavaScript var fs = require('fs'); var otherDep = require('./otherDep') // requires fs as a dependency var fileToTest = require('./fileToTest'); // requires fs and otherDep as a dependency mock('fs', {}); // fileToTest and otherDep are still using the unmocked fs module otherDep = mock.reRequire('./otherDep'); // do this to make sure fs is being mocked consistently fileToTest = mock.reRequire('./fileToTest'); ``` -------------------------------- ### Mocking a Node.js Module with Custom Export in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md This snippet demonstrates how to use `mock-require` to replace a built-in Node.js module like 'http' with a custom object. It shows how to define a mock export with a specific function (`request` in this case) and then verify that subsequent `require('http')` calls return the mocked object, executing the custom function. ```JavaScript var mock = require('mock-require'); mock('http', { request: function() { console.log('http.request called'); }}); var http = require('http'); http.request(); // 'http.request called' ``` -------------------------------- ### Applying a Reusable Mock to a Dependency in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md This snippet shows how `mock-require` is used within a test file (`test/a_spec.js`) to apply a previously defined reusable mock (`./spy`) to a specific dependency (`../some/dependency`). It demonstrates the pattern of using a dedicated mock file to replace a module. ```JavaScript var mock = require('mock-require'); mock('../some/dependency', './spy'); ... ``` -------------------------------- ### Refreshing Module Cache for Mocked Dependencies in JavaScript Source: https://github.com/boblauer/mock-require/blob/master/README.md This snippet demonstrates `mock.reRequire(path)`, used to clear Node.js's cache for a specific file, ensuring it reloads with any newly applied mocks. Initially, `fileToTest` uses the unmocked 'fs'. After mocking 'fs' and calling `reRequire('./fileToTest')`, `fileToTest` is reloaded and now uses the mocked 'fs' module. ```JavaScript var fs = require('fs'); var fileToTest = require('./fileToTest'); mock('fs', {}); // fileToTest is still using the unmocked fs module fileToTest = mock.reRequire('./fileToTest'); // fileToTest is now using your mock ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.