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

Package detail

mock-property

ljharb881.6kMIT1.1.0TypeScript support: included

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

testing, mock, object, property, value, descriptor, test, delete, define

readme

mock-property Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

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

Example

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 or see https://tidelift.com/security if you have a potential security vulnerability to report.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

v1.1.0 - 2024-08-06

Commits

  • [Tests] refactor tests for easier testing of multiple properties 605c32e
  • [New] add types 848be58
  • [Fix] properly stringify Symbol properties 369d824
  • [Fix] avoid a crash when mocking a nonexistent data property as non-enumerable sans a value 9994626
  • [Dev Deps] update @ljharb/eslint-config, aud,npmignore, object-inspect, tape ebfaf9a
  • [Dev Deps] ensure tape’s mock-property is ourself, for testing 2deaf72
  • [Deps] update define-data-property, has-property-descriptors, hasown e9f503d
  • [Tests] switch from aud to npm audit f5e6ce2
  • [meta] add sideEffects flag 2089940

v1.0.3 - 2023-10-20

Commits

  • [Refactor] use hasown instead of has da0d497
  • [Dev Deps] update object-inspect, tape 0b14b22
  • [Deps] update define-data-property 538289b

v1.0.2 - 2023-10-11

v1.0.1 - 2023-10-11

Commits

  • [meta] use npmignore to autogenerate an npmignore file 46993e4
  • [Refactor] use define-data-property for data properties 04e7859
  • [patch] replace spaces with tabs be261ab
  • [meta] fix auto-changelog config 39eca6c
  • [Fix] on restore, properly delete previously nonexistent properties in ES3 engines 0378863
  • [Fix] use gopd to handle IE < 9 f4a7461
  • [actions] update rebase action fea7b9c
  • [Dev Deps] update @ljharb/eslint-config, aud, object-inspect, tape 3a5c6f5
  • [Fix] throw validation errors before SyntaxErrors 34c9995
  • [Dev Deps] update @ljharb/eslint-config, aud, tape cb46e3d
  • [Dev Deps] update es-value-fixtures, object-inspect, tape bc66f0d
  • [Fix] in ES3 engines, do not attempt to change enumerability of array lengths cea6cd6
  • [Dev Deps] update tape e44b744
  • [Tests] skip tests that require ES5 in ES3 baa63d8
  • [Deps] update functions-have-names dd791a7
  • [meta] correct FUNDING.yml 5e2ca17

v1.0.0 - 2022-04-16

Commits