Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@cnpmjs/muk-prop

node-modules3.9kMIT1.1.1TypeScript support: included

Mock object methods and properties.

test, mock, stub

readme

@cnpmjs/muk-prop

NPM version Node.js CI codecov npm download Node.js Version

muk

Usage

Object method mocking.

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

muk(fs, 'readFile', (path, callback) => {
  process.nextTick(callback.bind(null, null, 'file contents here'));
});

Object props mocking with setter/getter.

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

const obj = { _a: 1 };
muk(obj, 'a', {
  set: (val) => obj._a = val * 2,
  get: (val) => obj._a,
});

obj.a = 2;
console.log(obj.a); // 4

Check if member has been mocked.

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

isMocked(fs, 'readFile'); // true

Restore all mocked methods/props after tests.

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

fs.readFile(file, (err, data) => {
  // will actually read from `file`
});

Install

npm install @cnpmjs/muk-prop

Tests

Tests are written with mocha

npm test

Contributors

Contributors

Made with contributors-img.

changelog

Changelog

1.1.1 (2024-12-22)

Bug Fixes

  • make sure mocks as global variable for multiple versions of muk-prop (#4) (62e120b)

1.1.0 (2024-12-21)

Features

  • mock property type support symbol and number (#3) (79bf8af)

1.0.0 (2024-12-20)

⚠ BREAKING CHANGES

  • drop Node.js < 18.19.0 support

part of https://github.com/eggjs/egg/issues/3644

https://github.com/eggjs/egg/issues/5257

Features

  • add isMocked method to check if the member of the object is mocked (986493c)
  • reset package (6779edb)
  • support cjs and esm both by tshy (#2) (49d3982)
  • support mock prop with getter setter (#4) (0ba2ae2)

Bug Fixes

0.5.1 / 2016-06-12

  • fix: should check process.env
  • fix: can't redefine process.env when node<4

0.5.0 / 2016-06-11

  • feat: add isMocked method to check if the member of the object is mocked

0.4.0 / 2015-09-17

  • Support mocking accessor descriptor