diff --git a/README.md b/README.md index 37c4973..95c11d8 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ npm install mm --save-dev ```ts import fs from 'node:fs'; -import mm from 'mm'; +import { mm } from 'mm'; mm(fs, 'readFileSync', function(filename) { return filename + ' content'; @@ -44,7 +44,7 @@ console.log(fs.readFileSync('《九评 Java》')); If mocked property is a function, it will be spied, every time it called, mm will modify `.called`, `.calledArguments` and `.lastCalledArguments`. For example: ```ts -import mm from 'mm'; +import { mm } from 'mm'; const target = { async add(a, b) { @@ -68,7 +68,7 @@ assert.deepEqual(target.add.lastCalledArguments, [ 2, 2 ]); If you only need spy and don't need mock, you can use `mm.spy` method directly: ```ts -import mm from 'mm'; +import { mm } from 'mm'; const target = { async add(a, b) { @@ -96,7 +96,7 @@ assert.deepEqual(target.add.lastCalledArguments, [ 2, 2 ]); ```ts import fs from 'node:fs'; -import mm from 'mm'; +import { mm } from 'mm'; mm.error(fs, 'readFile', 'mock fs.readFile return error'); @@ -120,7 +120,7 @@ Just like `mm.error()`, but only mock error once. ```ts import fs from 'node:fs'; -import mm from 'mm'; +import { mm } from 'mm'; mm.errorOnce(fs, 'readFile', 'mock fs.readFile return error'); @@ -207,7 +207,7 @@ urllib.request = function (...args, callback) { ### .syncError(module, propertyName, errerMessage, errorProperties) ```js -var mm = require('mm'); +var { mm } = require('mm'); var fs = require('fs'); mm.syncError(fs, 'readFileSync', 'mock fs.readFile return error', {code: 'ENOENT'}); @@ -256,7 +256,7 @@ mm.restore(); ### .http.request(mockUrl, mockResData, mockResHeaders) and .https.request(mockUrl, mockResData, mockResHeaders) ```js -var mm = require('mm'); +var { mm } = require('mm'); var http = require('http'); var mockURL = '/foo'; @@ -297,7 +297,7 @@ https.get({ ### .http.requestError(mockUrl, reqError, resError) and .https.requestError(mockUrl, reqError, resError) ```js -var mm = require('mm'); +var { mm } = require('mm'); var http = require('http'); var mockURL = '/foo'; diff --git a/package.json b/package.json index 2ed22ab..119fd83 100644 --- a/package.json +++ b/package.json @@ -25,13 +25,13 @@ }, "devDependencies": { "@arethetypeswrong/cli": "^0.17.1", + "@eggjs/bin": "7", "@eggjs/tsconfig": "1", "@types/mocha": "10", "@types/node": "22", - "egg-bin": "6", "eslint": "8", "eslint-config-egg": "14", - "pedding": "^1.1.0", + "pedding": "^2.0.1", "should": "^13.2.3", "tshy": "3", "tshy-after": "1", diff --git a/src/index.ts b/src/index.ts index dac9103..05bf61f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -595,55 +595,70 @@ const mockHttps = { requestError: mockHttpsRequestError, }; -// import { mm, restore } from 'mm'; -export { +const _mock = Object.assign(mock, { isMocked, mock, - mock as mm, - mockDatas as datas, + mm: mock, + datas: mockDatas, mockDatas, - mockData as data, + data: mockData, mockData, dataWithAsyncDispose, - mockEmpty as empty, + empty: mockEmpty, mockEmpty, - mockError as error, + error: mockError, mockError, spy, errorOnce, syncError, syncEmpty, syncData, - mockHttp as http, - mockHttps as https, + http: mockHttp, + https: mockHttps, spawn, restore, classMethod, -}; +}); + +// import mm from 'mm'; +const proxyMock = new Proxy(_mock, { + apply(target, _, args) { + return target(args[0], args[1], args[2]); + }, + get(_target, property, receiver) { + // import mm from 'mm'; + // mm.isMocked(foo, 'bar') + return Reflect.get(_target, property, receiver); + }, +}) as unknown as ((target: any, property: PropertyKey, value?: any) => void) & typeof _mock; // import mm from 'mm'; -export default Object.assign(mock, { +// mm.restore(); +export default proxyMock; + +// import { mm, restore } from 'mm'; +export { isMocked, mock, - mm: mock, - datas: mockDatas, + _mock as mm, + mockDatas as datas, mockDatas, - data: mockData, + mockData as data, mockData, dataWithAsyncDispose, - empty: mockEmpty, + mockEmpty as empty, mockEmpty, - error: mockError, + mockError as error, mockError, spy, errorOnce, syncError, syncEmpty, syncData, - http: mockHttp, - https: mockHttps, + mockHttp as http, + mockHttps as https, spawn, restore, classMethod, -}); +}; diff --git a/test/async-await.test.ts b/test/async-await.test.ts index ce691fd..efe8aa3 100644 --- a/test/async-await.test.ts +++ b/test/async-await.test.ts @@ -1,5 +1,5 @@ import { strict as assert } from 'node:assert'; -import { mm, restore, mockDatas } from '../src/index.js'; +import { mm } from '../src/index.js'; describe('test/async-await.test.ts', () => { const foo = { @@ -11,7 +11,7 @@ describe('test/async-await.test.ts', () => { }, }; - afterEach(restore); + afterEach(mm.restore); describe('mm()', () => { it('should mock async function', async () => { @@ -22,7 +22,7 @@ describe('test/async-await.test.ts', () => { datas = await foo.request(); assert.equal(datas, 'no'); - restore(); + mm.restore(); datas = await foo.request(); assert(datas, 'yes'); }); @@ -70,11 +70,11 @@ describe('test/async-await.test.ts', () => { describe('datas(), data()', () => { it('should mock async function', async () => { let datas; - mockDatas(foo, 'request', 'no'); + mm.mockDatas(foo, 'request', 'no'); datas = await foo.request(); assert.equal(datas, 'no'); - restore(); + mm.restore(); datas = await foo.request(); assert.equal(datas, 'yes'); }); diff --git a/test/mm.test.ts b/test/mm.test.ts index 5d8a135..5ac1f3d 100644 --- a/test/mm.test.ts +++ b/test/mm.test.ts @@ -10,7 +10,7 @@ import child_process from 'node:child_process'; import { randomUUID } from 'node:crypto'; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore -import pedding from 'pedding'; +import { pedding } from 'pedding'; import mm from '../src/index.js'; import { foo } from './foo.js';