Skip to content

Commit

Permalink
feat: support mock.dataWithAsyncDispose()
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 9, 2023
1 parent 2a2dfb4 commit 5043902
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 104 deletions.
74 changes: 0 additions & 74 deletions .github/workflows/codeql.yml

This file was deleted.

4 changes: 1 addition & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ on:
pull_request:
branches: [ master ]

workflow_dispatch: {}

jobs:
Job:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-test.yml@v1
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '14, 16, 18, 20'
6 changes: 1 addition & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@ on:
push:
branches: [ master ]

workflow_dispatch: {}

jobs:
release:
name: Node.js
uses: artusjs/github-actions/.github/workflows/node-release.yml@v1
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
with:
checkTest: false
10 changes: 0 additions & 10 deletions AUTHORS

This file was deleted.

50 changes: 47 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mm
=======
# mm, mock mate

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/node-modules/mm/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/mm/actions/workflows/nodejs.yml)
Expand All @@ -18,7 +17,7 @@ An simple but flexible **mock(or say stub)** package, mock mate.
## Install

```bash
$ npm install mm --save-dev
npm install mm --save-dev
```

## Usage
Expand Down Expand Up @@ -145,6 +144,38 @@ fs.readFile = function (...args, callback) {
};
```

### .dataWithAsyncDispose(module, propertyName, promiseResolveArg)

Support [Symbol.asyncDispose](https://www.totaltypescript.com/typescript-5-2-new-keyword-using)

```js
mm.dataWithAsyncDispose(locker, 'tryLock', {
locked: true,
});

// equals

locker.tryLock = async () => {
return {
locked: true,
[Symbol.asyncDispose](): async () => {
// do nothing
},
};
}
```

Run test with `await using` should work:

```js
mm.dataWithAsyncDispose(locker, 'tryLock', {
locked: true,
});

await using lock = await locker.tryLock('foo-key');
assert.equal(lock.locked, true);
```

### .empty(module, propertyName)

```js
Expand Down Expand Up @@ -305,3 +336,16 @@ assert(await foo1.fetch() === 3);
## License

[MIT](LICENSE)

<!-- GITCONTRIBUTOR_START -->

## Contributors

|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/985607?v=4" width="100px;"/><br/><sub><b>dead-horse</b></sub>](https://github.com/dead-horse)<br/>|[<img src="https://avatars.githubusercontent.com/u/1147375?v=4" width="100px;"/><br/><sub><b>alsotang</b></sub>](https://github.com/alsotang)<br/>|[<img src="https://avatars.githubusercontent.com/u/360661?v=4" width="100px;"/><br/><sub><b>popomore</b></sub>](https://github.com/popomore)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/4635838?v=4" width="100px;"/><br/><sub><b>gemwuu</b></sub>](https://github.com/gemwuu)<br/>|
| :---: | :---: | :---: | :---: | :---: | :---: |
|[<img src="https://avatars.githubusercontent.com/u/7971415?v=4" width="100px;"/><br/><sub><b>paranoidjk</b></sub>](https://github.com/paranoidjk)<br/>|[<img src="https://avatars.githubusercontent.com/u/2972143?v=4" width="100px;"/><br/><sub><b>nightink</b></sub>](https://github.com/nightink)<br/>|[<img src="https://avatars.githubusercontent.com/u/6897780?v=4" width="100px;"/><br/><sub><b>killagu</b></sub>](https://github.com/killagu)<br/>|[<img src="https://avatars.githubusercontent.com/u/9213756?v=4" width="100px;"/><br/><sub><b>gxkl</b></sub>](https://github.com/gxkl)<br/>|[<img src="https://avatars.githubusercontent.com/u/2170848?v=4" width="100px;"/><br/><sub><b>iyuq</b></sub>](https://github.com/iyuq)<br/>|[<img src="https://avatars.githubusercontent.com/u/227713?v=4" width="100px;"/><br/><sub><b>atian25</b></sub>](https://github.com/atian25)<br/>|
[<img src="https://avatars.githubusercontent.com/u/2748884?v=4" width="100px;"/><br/><sub><b>xavierchow</b></sub>](https://github.com/xavierchow)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>

This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Sat Dec 09 2023 11:34:46 GMT+0800`.

<!-- GITCONTRIBUTOR_END -->
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ declare namespace mm {
*/
function data(mod: any, method: string, data: any, timeout?: number): MockMate;

/**
* mock return data with Symbol.asyncDispose method
*/
function dataWithAsyncDispose(mod: any, method: string, data: object, timeout?: number): MockMate;

/**
* mock return callback(null, null).
*/
Expand Down
10 changes: 10 additions & 0 deletions lib/es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ mm.data = function(mod, method, data, timeout) {
return mm.datas(mod, method, data, timeout);
};

mm.dataWithAsyncDispose = function(mod, method, data, timeout) {
data = {
...data,
async [Symbol.asyncDispose]() {
// do nothing
},
};
return mm.data(mod, method, data, timeout);

Check warning on line 53 in lib/es6.js

View check run for this annotation

Codecov / codecov/patch

lib/es6.js#L47-L53

Added lines #L47 - L53 were not covered by tests
};

const mockError = mm.error;
mm.error = function(mod, method, error, props, timeout) {
if (!is.generatorFunction(mod[method])) {
Expand Down
18 changes: 13 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,32 @@
"lib"
],
"scripts": {
"test": "npm run lint && egg-bin test",
"ci": "npm run lint && egg-bin cov",
"lint": "eslint --fix lib test index.js"
"test": "npm run lint && egg-bin test --ts false",
"test-ts": "egg-bin test --ts true",
"ci": "npm run lint && egg-bin cov --ts false && npm run test-ts",
"lint": "eslint --fix lib test index.js",
"contributor": "git-contributor"
},
"dependencies": {
"is-type-of": "^1.2.1",
"muk-prop": "^1.2.1",
"thenify": "^3.3.0"
},
"devDependencies": {
"@eggjs/tsconfig": "^1.3.3",
"@hazae41/symbol-dispose-polyfill": "^1.0.2",
"@types/mocha": "^10.0.6",
"@types/node": "^20.10.4",
"chunkstream": "^0.0.1",
"co": "^4.6.0",
"egg-bin": "^6.4.0",
"eslint": "^8.28.0",
"eslint-config-egg": "^12.1.0",
"eslint": "^8.55.0",
"eslint-config-egg": "^12.0.0",
"git-contributor": "^2.1.5",
"pedding": "^1.1.0",
"should": "^13.2.3",
"thunkify-wrap": "^1.0.4",
"typescript": "^5.3.3",
"urllib": "^3.5.1"
},
"homepage": "http://github.com/node-modules/mm",
Expand Down
2 changes: 0 additions & 2 deletions test/async-await.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const mm = require('..');

describe('test/async-await.test.js', () => {
Expand Down
38 changes: 38 additions & 0 deletions test/asyncDispose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { strict as assert } from 'node:assert';
require('@hazae41/symbol-dispose-polyfill');
import mm from '../index';

describe('test/asyncDispose.test.ts', () => {
const foo = {
async request() {
return 'yes';
},
async echo() {
return {
hi: 'yes',
async [Symbol.asyncDispose]() {
console.log('asyncDispose run');
},
};
},
* generatorRequest() {
return 'yes';
},
};

afterEach(mm.restore);

describe('dataWithAsyncDispose()', () => {
it('should mock async function with asyncDispose', async () => {
mm.dataWithAsyncDispose(foo, 'echo', {
hi: 'no',
});
await using data1 = await foo.echo();
assert.equal(data1.hi, 'no');

mm.restore();
await using data2 = await foo.echo();
assert.equal(data2.hi, 'yes');
});
});
});
2 changes: 0 additions & 2 deletions test/mm.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

require('should');
const os = require('os');
const path = require('path');
Expand Down
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "@eggjs/tsconfig",
"compileOnSave": true,
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
}
}

0 comments on commit 5043902

Please sign in to comment.