Skip to content

Commit

Permalink
feat: support cjs and esm both by tshy
Browse files Browse the repository at this point in the history
BREAKING CHANGE: drop Node.js < 18.19.0 support

part of eggjs/egg#3644

eggjs/egg#5257
  • Loading branch information
fengmk2 committed Dec 20, 2024
1 parent 8855186 commit 8facd3d
Show file tree
Hide file tree
Showing 18 changed files with 582 additions and 710 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
6 changes: 3 additions & 3 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name: CI
on:
push:
branches: [ master ]

pull_request:
branches: [ master ]

Expand All @@ -12,5 +11,6 @@ jobs:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
os: 'ubuntu-latest, macos-latest, windows-latest'
version: '14, 16, 18, 20'
version: '18.19.0, 18, 20, 22, 23'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/pkg.pr.new.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish Any Commit
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- run: corepack enable
- uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm install

- name: Build
run: npm run prepublishOnly --if-present

- run: npx pkg-pr-new publish
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ results
node_modules
npm-debug.log
coverage
package-lock.json
.tshy*
.eslintcache
dist
53 changes: 25 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![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)
[![Test coverage][codecov-image]][codecov-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/mm.svg?style=flat)](https://nodejs.org/en/download/)

[npm-image]: https://img.shields.io/npm/v/mm.svg?style=flat-square
[npm-url]: https://npmjs.org/package/mm
Expand All @@ -22,9 +23,9 @@ npm install mm --save-dev

## Usage

```js
var mm = require('mm');
var fs = require('fs');
```ts
import fs from 'node:fs';
import mm from 'mm';

mm(fs, 'readFileSync', function(filename) {
return filename + ' content';
Expand All @@ -33,8 +34,7 @@ mm(fs, 'readFileSync', function(filename) {
console.log(fs.readFileSync('《九评 Java》'));
// => 《九评 Java》 content

mm.restore();

restore();
console.log(fs.readFileSync('《九评 Java》'));
// => throw `Error: ENOENT, no such file or directory '《九评 Java》`
```
Expand All @@ -43,7 +43,9 @@ 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:

```js
```ts
import mm from 'mm';

const target = {
async add(a, b) {
return a + b;
Expand All @@ -65,7 +67,9 @@ assert.deepEqual(target.add.lastCalledArguments, [ 2, 2 ]);

If you only need spy and don't need mock, you can use `mm.spy` method directly:

```js
```ts
import mm from 'mm';

const target = {
async add(a, b) {
await this.foo();
Expand All @@ -90,9 +94,9 @@ assert.deepEqual(target.add.lastCalledArguments, [ 2, 2 ]);

### .error(module, propertyName, errerMessage, errorProperties)

```js
var mm = require('mm');
var fs = require('fs');
```ts
import fs from 'node:fs';
import mm from 'mm';

mm.error(fs, 'readFile', 'mock fs.readFile return error');

Expand All @@ -114,9 +118,9 @@ fs.readFile('/etc/hosts', 'utf8', function (err, content) {

Just like `mm.error()`, but only mock error once.

```js
const mm = require('mm');
const fs = require('fs');
```ts
import fs from 'node:fs';
import mm from 'mm';

mm.errorOnce(fs, 'readFile', 'mock fs.readFile return error');

Expand All @@ -135,12 +139,12 @@ fs.readFile('/etc/hosts', 'utf8', function (err, content) {
### .data(module, propertyName, secondCallbackArg)

```js
mm.data(fs, 'readFile', new Buffer('some content'));
mm.data(fs, 'readFile', Buffer.from('some content'));

// equals

fs.readFile = function (...args, callback) {
callback(null, new Buffer('some content'))
callback(null, Buffer.from('some content'))
};
```

Expand Down Expand Up @@ -191,12 +195,12 @@ mysql.query = function (...args, callback) {
### .datas(module, propertyName, argsArray)

```js
mm.datas(urllib, 'request', [new Buffer('data'), {headers: { foo: 'bar' }}]);
mm.datas(urllib, 'request', [Buffer.from('data'), {headers: { foo: 'bar' }}]);

// equals

urllib.request = function (...args, callback) {
callback(null, new Buffer('data'), {headers: { foo: 'bar' }});
callback(null, Buffer.from('data'), {headers: { foo: 'bar' }});
}
```

Expand All @@ -221,12 +225,12 @@ fs.readFileSync = function (...args) {
### .syncData(module, propertyName, value)

```js
mm.syncData(fs, 'readFileSync', new Buffer('some content'));
mm.syncData(fs, 'readFileSync', Buffer.from('some content'));

// equals

fs.readFileSync = function (...args) {
return new Buffer('some content');
return Buffer.from('some content');
};
```

Expand Down Expand Up @@ -337,15 +341,8 @@ assert(await foo1.fetch() === 3);

[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`.
[![Contributors](https://contrib.rocks/image?repo=node-modules/mm)](https://github.com/node-modules/mm/graphs/contributors)

<!-- GITCONTRIBUTOR_END -->
Made with [contributors-img](https://contrib.rocks).
3 changes: 0 additions & 3 deletions index.js

This file was deleted.

98 changes: 0 additions & 98 deletions lib/es6.js

This file was deleted.

Loading

0 comments on commit 8facd3d

Please sign in to comment.