# mock-property

> Given an object and a property, replaces a property descriptor (or deletes it), and returns a thunk to restore it.

Latest version **1.1.2** (published 2026-06-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install mock-property
pnpm add mock-property
yarn add mock-property
bun add mock-property
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2026-06-20 |
| First published | 2022-04-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 0.4 |
| Dependencies | 8 |
| Unpacked size | 59.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | Jordan Harband |
| Maintainers | ljharb |
| Keywords | testing, mock, object, property, value, descriptor, test, delete, define |

## Links

- npm: https://www.npmjs.com/package/mock-property
- Repository: https://github.com/ljharb/mock-property
- Homepage: https://github.com/ljharb/mock-property#readme
- Issues: https://github.com/ljharb/mock-property/issues
- Funding: https://github.com/sponsors/ljharb
- npm.io page: https://npm.io/package/mock-property

## Dependencies (8)

- [gopd](https://npm.io/package/gopd.md) ^1.2.0
- [hasown](https://npm.io/package/hasown.md) ^2.0.4
- [isarray](https://npm.io/package/isarray.md) ^2.0.5
- [es-errors](https://npm.io/package/es-errors.md) ^1.3.0
- [object-inspect](https://npm.io/package/object-inspect.md) ^1.13.4
- [define-data-property](https://npm.io/package/define-data-property.md) ^1.1.4
- [functions-have-names](https://npm.io/package/functions-have-names.md) ^1.2.3
- [has-property-descriptors](https://npm.io/package/has-property-descriptors.md) ^1.0.2

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2026-06-20
- 1.1.1 — 2026-06-19
- 1.1.0 — 2024-08-06
- 1.0.3 — 2023-10-21
- 1.0.2 — 2023-10-11
- 1.0.1 — 2023-10-11
- 1.0.0 — 2022-04-16

## README

# mock-property <sup>[![Version Badge][npm-version-svg]][package-url]</sup>

[![github actions][actions-image]][actions-url]
[![coverage][codecov-image]][codecov-url]
[![dependency status][deps-svg]][deps-url]
[![dev dependency status][dev-deps-svg]][dev-deps-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

[![npm badge][npm-badge-png]][package-url]

Given an object and a property, replaces a property descriptor (or deletes it), and returns a thunk to restore it.

## Example

```js
var mockProperty = require('mock-property');
var assert = require('assert');

var i = 0;
var object = {
	a: 1,
	get b() {
		i += 1;
		return 'b ' + i;
	}
};

assert.equal(object.a, 1);
assert.equal(object.b, 'b 1');
assert.equal(object.b, 'b 2');

var restoreA = mockProperty(object, 'a', { 'delete': true });
assert.equal('a' in object, false);

var restoreB = mockProperty(object, 'b', { value: 42 });
assert.equal(object.b, 42);

restoreA();
assert.equal('a' in object, true);

restoreB();
assert.equal(object.b, 'b 3');
```

## Tests
Simply clone the repo, `npm install`, and run `npm test`

## Security

Please email [@ljharb](https://github.com/ljharb) or see https://tidelift.com/security if you have a potential security vulnerability to report.

[package-url]: https://npmjs.org/package/mock-property
[npm-version-svg]: https://versionbadg.es/ljharb/mock-property.svg
[deps-svg]: https://david-dm.org/ljharb/mock-property.svg
[deps-url]: https://david-dm.org/ljharb/mock-property
[dev-deps-svg]: https://david-dm.org/ljharb/mock-property/dev-status.svg
[dev-deps-url]: https://david-dm.org/ljharb/mock-property#info=devDependencies
[npm-badge-png]: https://nodei.co/npm/mock-property.png?downloads=true&stars=true
[license-image]: https://img.shields.io/npm/l/mock-property.svg
[license-url]: LICENSE
[downloads-image]: https://img.shields.io/npm/dm/mock-property.svg
[downloads-url]: https://npm-stat.com/charts.html?package=mock-property
[codecov-image]: https://codecov.io/gh/ljharb/mock-property/branch/main/graphs/badge.svg
[codecov-url]: https://app.codecov.io/gh/ljharb/mock-property/
[actions-image]: https://img.shields.io/github/check-runs/ljharb/mock-property/main
[actions-url]: https://github.com/ljharb/mock-property/actions

---
_Source: https://npm.io/package/mock-property · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
