### Install mm package Source: https://github.com/node-modules/mm/blob/master/README.md Installs the 'mm' package as a development dependency using npm. ```bash npm install mm --save-dev ``` -------------------------------- ### Mocking HTTP/HTTPS Requests with mm.http/https.request (JavaScript) Source: https://github.com/node-modules/mm/blob/master/README.md Demonstrates how to mock HTTP and HTTPS GET requests using mm.http.request and mm.https.request. It shows setting up mocks for a specific URL with custom response data and headers, and then verifies the mock by making actual requests. ```javascript var { mm } = require('mm'); var http = require('http'); var mockURL = '/foo'; var mockResData = 'mock data'; var mockResHeaders = { server: 'mock server' }; mm.http.request(mockURL, mockResData, mockResHeaders); mm.https.request(mockURL, mockResData, mockResHeaders); // http http.get({ path: '/foo' }, function (res) { console.log(res.headers); // should be mock headers var body = ''; res.on('data', function (chunk) { body += chunk.toString(); }); res.on('end', function () { console.log(body); // should equal 'mock data' }); }); // https https.get({ path: '/foo' }, function (res) { console.log(res.headers); // should be mock headers var body = ''; res.on('data', function (chunk) { body += chunk.toString(); }); res.on('end', function () { console.log(body); // should equal 'mock data' }); }); ``` -------------------------------- ### Mocking Sync Function Return Value with mm.syncData (JavaScript) Source: https://github.com/node-modules/mm/blob/master/README.md Shows how to use mm.syncData to make a synchronous function return a specific value. It provides an example of mocking fs.readFileSync to return a Buffer containing 'some content', and compares it to manually replacing the function. ```javascript mm.syncData(fs, 'readFileSync', Buffer.from('some content')); // equals fs.readFileSync = function (...args) { return Buffer.from('some content'); }; ``` -------------------------------- ### Using Mocked Async Dispose Resource in JavaScript Source: https://github.com/node-modules/mm/blob/master/README.md Shows how to use the mocked object created by `mm.dataWithAsyncDispose` with the `await using` syntax. ```javascript mm.dataWithAsyncDispose(locker, 'tryLock', { locked: true, }); await using lock = await locker.tryLock('foo-key'); assert.equal(lock.locked, true); ``` -------------------------------- ### Mocking Empty Callback with mm.empty in JavaScript Source: https://github.com/node-modules/mm/blob/master/README.md Illustrates `mm.empty`, which mocks a function to simply call its callback with no arguments, simulating a successful operation that returns no data. ```javascript mm.empty(mysql, 'query'); // equals mysql.query = function (...args, callback) { callback(); }; ``` -------------------------------- ### Mocking Class Methods with mm.classMethod (JavaScript) Source: https://github.com/node-modules/mm/blob/master/README.md Explains how to mock a specific method on a class instance using mm.classMethod. It shows mocking the fetch method of a Foo instance to return a different value and verifies that the mock affects the specified instance as well as other instances of the same class. ```javascript class Foo { async fetch() { return 1; } } const foo = new Foo(); const foo1 = new Foo(); mm.classMethod(foo, 'fetch', async () => { return 3; }); assert(await foo.fetch() === 3); assert(await foo1.fetch() === 3); ``` -------------------------------- ### Mocking Callback Data with mm.data in JavaScript Source: https://github.com/node-modules/mm/blob/master/README.md Shows how to use `mm.data` to mock a function that uses a callback pattern. It intercepts the call and invokes the callback with null error and the specified data. ```javascript mm.data(fs, 'readFile', Buffer.from('some content')); // equals fs.readFile = function (...args, callback) { callback(null, Buffer.from('some content')) }; ``` -------------------------------- ### Restoring Mocked Properties with mm.restore (JavaScript) Source: https://github.com/node-modules/mm/blob/master/README.md Explains how to use mm.restore() to revert all properties that were previously mocked by the mm module back to their original state. This is essential for cleaning up mocks after tests or operations. ```javascript // restore all mock properties mm.restore(); ``` -------------------------------- ### Mocking Async Dispose Resource with mm.dataWithAsyncDispose in JavaScript Source: https://github.com/node-modules/mm/blob/master/README.md Demonstrates using `mm.dataWithAsyncDispose` to mock a function that returns an object intended for use with `await using`, specifically supporting `Symbol.asyncDispose`. ```javascript mm.dataWithAsyncDispose(locker, 'tryLock', { locked: true, }); // equals locker.tryLock = async () => { return { locked: true, [Symbol.asyncDispose](): async () => { // do nothing }, }; } ``` -------------------------------- ### Mocking Sync Function Error with mm.syncError (JavaScript) Source: https://github.com/node-modules/mm/blob/master/README.md Demonstrates how to use mm.syncError to make a synchronous function throw a specific error. It shows mocking fs.readFileSync to throw an error with a custom message and code, and compares it to manually replacing the function. ```javascript var { mm } = require('mm'); var fs = require('fs'); mm.syncError(fs, 'readFileSync', 'mock fs.readFile return error', {code: 'ENOENT'}); // equals fs.readFileSync = function (...args) { var err = new Error('mock fs.readFile return error'); err.code = 'ENOENT'; throw err; }; ``` -------------------------------- ### Mocking Callback with Multiple Arguments with mm.datas in JavaScript Source: https://github.com/node-modules/mm/blob/master/README.md Shows how to use `mm.datas` to mock a function that uses a callback and needs to return multiple arguments (e.g., error, data, response). It calls the callback with the provided array of arguments. ```javascript mm.datas(urllib, 'request', [Buffer.from('data'), {headers: { foo: 'bar' }}]); // equals urllib.request = function (...args, callback) { callback(null, Buffer.from('data'), {headers: { foo: 'bar' }}); }; ``` -------------------------------- ### Basic Function Mocking with mm in TypeScript Source: https://github.com/node-modules/mm/blob/master/README.md Demonstrates how to use the 'mm' function to mock a method on an object (fs.readFileSync) and restore the original behavior. The mock replaces the original function with a custom implementation. ```typescript import fs from 'node:fs'; import { mm } from 'mm'; mm(fs, 'readFileSync', function(filename) { return filename + ' content'; }); console.log(fs.readFileSync('《九评 Java》')); // => 《九评 Java》 content restore(); console.log(fs.readFileSync('《九评 Java》')); // => throw `Error: ENOENT, no such file or directory '《九评 Java》` ``` -------------------------------- ### Mocking Sync Function to Return Undefined with mm.syncEmpty (JavaScript) Source: https://github.com/node-modules/mm/blob/master/README.md Illustrates the use of mm.syncEmpty to make a synchronous function return undefined. It shows mocking fs.readFileSync to return nothing, equivalent to a function that simply returns or has no explicit return value. ```javascript mm.syncEmpty(fs, 'readFileSync'); // equals fs.readFileSync = function (...args) { return; } ``` -------------------------------- ### Mocking Function Return Value and Spying with mm.data in TypeScript Source: https://github.com/node-modules/mm/blob/master/README.md Shows how to use `mm.data` to mock a function's return value directly. It also illustrates how 'mm' automatically adds spying capabilities to mocked functions, allowing inspection of call count and arguments. ```typescript import { mm } from 'mm'; const target = { async add(a, b) { return a + b; }, }; mm.data(target, 'add', 3); assert.equal(await target.add(1, 1), 3); assert.equal(target.add.called, 1); assert.deepEqual(target.add.calledArguments, [[ 1, 1 ]]); assert.deepEqual(target.add.lastCalledArguments, [ 1, 1 ]); assert.equal(await target.add(2, 2), 3); assert.equal(target.add.called, 2); assert.deepEqual(target.add.calledArguments, [[ 1, 1 ], [ 2, 2 ]]); assert.deepEqual(target.add.lastCalledArguments, [ 2, 2 ]); ``` -------------------------------- ### Mocking HTTP/HTTPS Request Errors with mm.http/https.requestError (JavaScript) Source: https://github.com/node-modules/mm/blob/master/README.md Shows how to use mm.http.requestError to simulate errors during HTTP requests. It demonstrates mocking a specific URL to cause a response error, preventing the end event and triggering the request's error event with a mock error object. ```javascript var { mm } = require('mm'); var http = require('http'); var mockURL = '/foo'; var reqError = null; var resError = 'mock res error'; mm.http.requestError(mockURL, reqError, resError); var req = http.get({ path: '/foo' }, function (res) { console.log(res.statusCode, res.headers); // 200 but never emit `end` event res.on('end', fucntion () { console.log('never show this message'); }); }); req.on('error', function (err) { console.log(err); // should return mock err: err.name === 'MockHttpResponseError' }); ``` -------------------------------- ### Mocking Function to Throw Error with mm.error in TypeScript Source: https://github.com/node-modules/mm/blob/master/README.md Shows how to use `mm.error` to force a function (fs.readFile) to throw a specified error when called. Includes restoring the original function behavior. ```typescript import fs from 'node:fs'; import { mm } from 'mm'; mm.error(fs, 'readFile', 'mock fs.readFile return error'); fs.readFile('/etc/hosts', 'utf8', function (err, content) { // err.name === 'MockError' // err.message === 'mock fs.readFile return error' console.log(err); mm.restore(); // remove all mock effects. fs.readFile('/etc/hosts', 'utf8', function (err, content) { console.log(err); // => null console.log(content); // => your hosts }); }); ``` -------------------------------- ### Mocking Function to Throw Error Once with mm.errorOnce in TypeScript Source: https://github.com/node-modules/mm/blob/master/README.md Demonstrates `mm.errorOnce`, which is similar to `mm.error` but only causes the function to throw the specified error on the first call. Subsequent calls execute the original function. ```typescript import fs from 'node:fs'; import { mm } from 'mm'; mm.errorOnce(fs, 'readFile', 'mock fs.readFile return error'); fs.readFile('/etc/hosts', 'utf8', function (err, content) { // err.name === 'MockError' // err.message === 'mock fs.readFile return error' console.log(err); fs.readFile('/etc/hosts', 'utf8', function (err, content) { console.log(err); // => null console.log(content); // => your hosts }); }); ``` -------------------------------- ### Spying on a Function without Mocking with mm.spy in TypeScript Source: https://github.com/node-modules/mm/blob/master/README.md Explains how to use `mm.spy` to add spying capabilities to an existing function without altering its original behavior. This allows tracking call count and arguments for debugging or testing. ```typescript import { mm } from 'mm'; const target = { async add(a, b) { await this.foo(); return a + b; }, async foo() { /* */ }, }; mm.spy(target, 'add'); assert.equal(await target.add(1, 1), 2); assert.equal(target.add.called, 1); assert.deepEqual(target.add.calledArguments, [[ 1, 1 ]]); assert.deepEqual(target.add.lastCalledArguments, [ 1, 1 ]); assert.equal(await target.add(2, 2), 4); assert.equal(target.add.called, 2); assert.deepEqual(target.add.calledArguments, [[ 1, 1 ], [ 2, 2 ]]); assert.deepEqual(target.add.lastCalledArguments, [ 2, 2 ]); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.