Skip to content

Commit

Permalink
fix: allow call mm.restore on normal export
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 2, 2025
1 parent 93c6647 commit f557999
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 35 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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');

Expand All @@ -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');

Expand Down Expand Up @@ -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'});
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
53 changes: 34 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

10 changes: 5 additions & 5 deletions test/async-await.test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -11,7 +11,7 @@ describe('test/async-await.test.ts', () => {
},
};

afterEach(restore);
afterEach(mm.restore);

describe('mm()', () => {
it('should mock async function', async () => {
Expand All @@ -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');
});
Expand Down Expand Up @@ -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');
});
Expand Down
2 changes: 1 addition & 1 deletion test/mm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit f557999

Please sign in to comment.