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 75ba8a4 commit ace4a3c
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 112 deletions.
6 changes: 6 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
16 changes: 16 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
Job:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test.yml@master
with:
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
13 changes: 13 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Release

on:
push:
branches: [ master ]

jobs:
release:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-release.yml@master
secrets:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
GIT_TOKEN: ${{ secrets.GIT_TOKEN }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules
.tshy*
.eslintcache
dist
coverage
package-lock.json
13 changes: 0 additions & 13 deletions .travis.yml

This file was deleted.

File renamed without changes.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (C) 2012 by fent
Copyright (C) 2024 - present node-modules

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
38 changes: 27 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
# muk-prop.js
# @cnpmjs/muk-prop

[![Build Status](https://secure.travis-ci.org/fent/muk-prop.js.svg)](http://travis-ci.org/fent/muk-prop.js)
[![Dependency Status](https://david-dm.org/fent/muk-prop.js.svg)](https://david-dm.org/fent/muk-prop.js)
[![codecov](https://codecov.io/gh/fent/muk-prop.js/branch/master/graph/badge.svg)](https://codecov.io/gh/fent/muk-prop.js)
[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/node-modules/muk-prop.js/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/muk-prop.js/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/@cnpmjs/muk-prop.svg?style=flat)](https://nodejs.org/en/download/)

[npm-image]: https://img.shields.io/npm/v/@cnpmjs/muk-prop.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@cnpmjs/muk-prop
[codecov-image]: https://codecov.io/github/node-modules/muk-prop.js/coverage.svg?branch=master
[codecov-url]: https://codecov.io/github/node-modules/muk-prop.js?branch=master
[download-image]: https://img.shields.io/npm/dm/@cnpmjs/muk-prop.svg?style=flat-square
[download-url]: https://npmjs.org/package/@cnpmjs/muk-prop

![muk](muk.gif)

# Usage
## Usage

Object method mocking.

```js
const fs = require('fs');
const muk = require('muk-prop');
const { muk } = require('@cnpmjs/muk-prop');

muk(fs, 'readFile', (path, callback) => {
process.nextTick(callback.bind(null, null, 'file contents here'));
Expand All @@ -22,7 +31,7 @@ muk(fs, 'readFile', (path, callback) => {
Object props mocking with setter/getter.

```js
const muk = require('muk-prop');
const { muk } = require('@cnpmjs/muk-prop');

const obj = { _a: 1 };
muk(obj, 'a', {
Expand Down Expand Up @@ -50,15 +59,22 @@ fs.readFile(file, (err, data) => {
});
```

## Install

# Install

npm install muk-prop
```bash
npm install @cnpmjs/muk-prop
```

## Tests

# Tests
Tests are written with [mocha](https://mochajs.org)

```bash
npm test
```

## Contributors

[![Contributors](https://contrib.rocks/image?repo=node-modules/muk-prop.js)](https://github.com/node-modules/muk-prop.js/graphs/contributors)

Made with [contributors-img](https://contrib.rocks).
65 changes: 50 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,68 @@
{
"name": "@cnpmjs/muk-prop",
"version": "1.0.0",
"publishConfig": {
"access": "public"
},
"description": "Mock object methods and properties.",
"keywords": [
"test",
"mock",
"stub"
],
"version": "1.0.0",
"repository": {
"type": "git",
"url": "git://github.com/node-modules/muk-prop.js.git"
},
"author": "fent (https://github.com/fent)",
"main": "./lib/index.js",
"scripts": {
"test": "istanbul cover node_modules/.bin/_mocha -- -R spec test/*-test.js"
},
"directories": {
"lib": "./lib"
"license": "MIT",
"engines": {
"node": ">= 18.19.0"
},
"devDependencies": {
"istanbul": "^0.4.5",
"mocha": "^6.0.0"
"@arethetypeswrong/cli": "^0.17.1",
"@eggjs/tsconfig": "1",
"@types/node": "22",
"@types/mocha": "10",
"egg-bin": "6",
"eslint": "8",
"eslint-config-egg": "14",
"tshy": "3",
"tshy-after": "1",
"typescript": "5"
},
"engines": {
"node": ">=6"
"scripts": {
"lint": "eslint --cache src test --ext .ts",
"pretest": "npm run lint -- --fix && npm run prepublishOnly",
"test": "egg-bin test",
"preci": "npm run lint && npm run prepublishOnly && attw --pack",
"ci": "egg-bin cov",
"prepublishOnly": "tshy && tshy-after"
},
"type": "module",
"tshy": {
"exports": {
".": "./src/index.ts",
"./package.json": "./package.json"
}
},
"exports": {
".": {
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
},
"./package.json": "./package.json"
},
"license": "MIT",
"files": [
"lib"
]
"dist",
"src"
],
"types": "./dist/commonjs/index.d.ts",
"main": "./dist/commonjs/index.js",
"module": "./dist/esm/index.js"
}
42 changes: 23 additions & 19 deletions lib/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
'use strict';

// Keep track of mocks
let mocks = [];
const cache = new Map();
export interface MockItem {
obj: any;
key: string;
descriptor: PropertyDescriptor;
hasOwnProperty: boolean;
}

let mocks: MockItem[] = [];

const cache = new Map<any, Set<any>>();

/**
* Mocks a value of an object.
*
* @param {Object} obj
* @param {string} key
* @param {!Function|Object} value
*/
const method = module.exports = (obj, key, value) => {
const hasOwnProperty = Object.prototype.hasOwnProperty.call(obj, key);
export function mock(obj: any, key: string, value?: any) {
const hasOwnProperty = Object.hasOwn(obj, key);
mocks.push({
obj,
key,
descriptor: Object.getOwnPropertyDescriptor(obj, key),
descriptor: Object.getOwnPropertyDescriptor(obj, key)!,
// Make sure the key exists on object not the prototype
hasOwnProperty,
});
Expand All @@ -35,7 +36,7 @@ const method = module.exports = (obj, key, value) => {
}
flag.add(key);

const descriptor = {
const descriptor: PropertyDescriptor = {
configurable: true,
enumerable: true,
};
Expand All @@ -51,14 +52,17 @@ const method = module.exports = (obj, key, value) => {
}

Object.defineProperty(obj, key, descriptor);
};
}

// alias to mock
export const muk = mock;

/**
* Restore all mocks
*/
method.restore = () => {
export function restore() {
for (let i = mocks.length - 1; i >= 0; i--) {
let m = mocks[i];
const m = mocks[i];
if (!m.hasOwnProperty) {
// Delete the mock key, use key on the prototype
delete m.obj[m.key];
Expand All @@ -69,9 +73,9 @@ method.restore = () => {
}
mocks = [];
cache.clear();
};
}

method.isMocked = (obj, key) => {
let flag = cache.get(obj);
export function isMocked(obj: any, key: string) {
const flag = cache.get(obj);
return flag ? flag.has(key) : false;
};
}
Loading

0 comments on commit ace4a3c

Please sign in to comment.