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

Overview

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.

You might also like...

A Powerful and Elegant "alert" library for JavaScript that replaces that boring alert style of Javascript.

A Powerful and Elegant

A Powerful , Elegant and fully customizable "alert" library using JavaScript that replaces that boring style of alert. Installation Place the below sc

Aug 10, 2021

A browser/Chrome extension that replaces Leftist #trigger words with "bad thing" and "current bad thing". Inspired by Tim Pool.

BadThings browser/Chrome extension replaces Leftist #trigger words with 'bad things' and 'current bad thing'. Quickstart This project uses React 18 an

Sep 5, 2022

Replaces native cursor with custom animated cursor.

Animated Cursor A pure JS library to replace native cursor with a custom animated cursor. Check out the Demo Contents ๐Ÿ“Œ Features ๐ŸŽฏ Quickstart ๐Ÿงฌ Opt

Jul 18, 2022

Replaces the default Strapi WYSIWYG editor with a customized build of CKEditor 5 html editor packed with useful plugins.

Replaces the default Strapi WYSIWYG editor with a customized build of CKEditor 5 html editor packed with useful plugins.

CKEditor 5 for Strapi Replaces the default Strapi WYSIWYG editor with a customized build of CKEditor 5 packed with useful plugins. ๐Ÿ‘‹ Get Started Feat

Jan 2, 2023

Replaces Youtube Chat with Destiny.gg chat.

Replaces Youtube Chat with Destiny.gg chat.

A lightweight extension that replaces the native Youtube Live chat with an embeded destiny.gg chat. Note: This is in no way affiliated with Destiny.gg

Jul 27, 2022

Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator Types generator is a utility tool that will help User to create TS Interfaces from JSON. All you have to do is paste your single objec

Dec 6, 2022

Tiny JavaScript library (1kB) by CurrencyRate.today, providing simple way and advanced number, money and currency formatting and removes all formatting/cruft and returns the raw float value.

Zero dependency tiny JavaScript library (1kB bytes) by CurrencyRate.today, providing simple way and advanced number, money and currency formatting and removes all formatting/cruft and returns the raw float value.

Nov 8, 2022

I forgot about el.outerHTML so I made this, it takes a DOM element and returns its html as string

I forgot about el.outerHTML so I made this, it takes a DOM element and returns its html as string

htmlToString Convert html/DOM element to string Works with rendered and virtual DOM Installation npm install htmltostring Or using CDN script src="ht

Jul 22, 2022

A simple website that returns a random advice.

A simple website that returns a random advice.

Advice Preview ๐Ÿš€ Technologies This project was developed with the following technologies: HTML CSS JavaScript API's Advice Slip JSON API ๐Ÿ’ป Project A

Sep 2, 2022
Comments
  • Simple save/restore pattern

    Simple save/restore pattern

    For simple use by people unfamiliar with descriptors, but wanting to safely put back the old state after overwriting a property, a simple save and restore might be useful. Two cases where I have wanted to do this are setting an environment variable for a unit test, and hacking process.argv to run a unit test.

    I suspect this is equivalent to is a delete in the first operation to clear the way for whatever I do, and perhaps doesn't make sense as a "mock" pattern. Could be wishful thinking that this might be a possible helper routine. ๐Ÿ˜„

    Edit: a better description of the usage pattern might be "stash" and "restore".

    e.g.

    var restoreArgv = mockProperty(process, 'argv');
    process.argv = ['someApp', 'someFile', 'arg1'];
    // run test
    restoreArgv();
    
    question 
    opened by shadowspawn 1
  • Dependency Dashboard

    Dependency Dashboard

    This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

    Pending Approval

    These branches will be created by Renovate only once you click their checkbox below.

    • [ ] [Deps] Update Update eslint to v8.31.0
    • [ ] [Deps] Update Update nyc to v15
    • [ ] ๐Ÿ” Create all pending approval PRs at once ๐Ÿ”

    Detected dependencies

    github-actions
    .github/workflows/node-aught.yml
    .github/workflows/node-pretest.yml
    .github/workflows/node-tens.yml
    .github/workflows/rebase.yml
    • actions/checkout v3
    .github/workflows/require-allow-edits.yml
    npm
    package.json
    • functions-have-names ^1.2.3
    • has ^1.0.3
    • has-property-descriptors ^1.0.0
    • isarray ^2.0.5
    • @ljharb/eslint-config ^21.0.0
    • aud ^2.0.0
    • auto-changelog ^2.4.0
    • es-value-fixtures ^1.4.2
    • eslint =8.8.0
    • evalmd ^0.0.19
    • for-each ^0.3.3
    • in-publish ^2.0.1
    • npmignore ^0.3.0
    • nyc ^10.3.2
    • object-inspect ^1.12.2
    • safe-publish-latest ^2.0.0
    • tape ^5.6.1

    • [ ] Check this box to trigger a request for Renovate to run again on this repository
    opened by renovate[bot] 0
Owner
Jordan Harband
software engineer/nerd/teacher/will try anything once; surgeon with git rebase. @tc39, ex @airbnb @twitter @MobBase @tripit. Fav punctuation โธฎ, scent petrichor
Jordan Harband
Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Welcome to ts-object-keys ?? Fix for Object.keys, which normally just returns an array of strings, which is not good when you care about strong typing

Funtal Foundation 1 Jul 4, 2022
Promisified thunk with caching (pronounced "funky")

phunky phunky (promisified thunk, pronounced funky) is yet another thunk-inspired library, to delay a calculation until its result is needed, but also

Matt 2 Oct 14, 2022
BookStore is a website that allows a given user to view a list of books, to add a new book and remove a given book.

Project Name : BookStore CMS BookStore is a website that allows a given user to view a list of books, to add a new book and remove a given book. In or

Chris Siku 10 Aug 22, 2022
๐Ÿค  Functional utilities using object property paths with wildcards and regexps ๐ŸŒต

?? Functional utilities using object property paths with wildcards and regexps. ?? Available functional methods include: ??๏ธ Mapping: map() ?? Merging

ehmicky 29 Dec 15, 2022
๐Ÿค  Object property paths with wildcards and regexps ๐ŸŒต

?? Object property paths with wildcards and regexps. ?? Get/set object properties using: โ›๏ธ Dot-delimited paths: foo.bar.0.baz โญ Wildcards: foo.*, **.

ehmicky 585 Jan 3, 2023
The ManageYourCompany ๐Ÿ“ˆ project is a project that creates, deletes, updates companies, units and assets.

The ManageYourCompany ?? project is a project that creates, deletes, updates companies, units and assets. The rule is that every company has several units and the units have several assets, these assets are machines with several fields: Name, status, person in charge, image, among others... This is a project in order to exercise my Backend skills with NodeJs and front with react.

Bruno da Silva Leite 2 Feb 9, 2022
This is a project that add, stores and deletes current book in the library

Project Name Awesome books: with ES6 Description the project. In this project, I restructured my Awesome books app code. The goal was to make it more

Harrison Njuguna 5 Nov 11, 2022
Simple string diffing. Given two strings, striff will return an object noting which characters were added or removed, and at which indices

Simple string diffing. Given two strings, striff will return an object noting which characters were added or removed, and at which indices

Alex MacArthur 196 Jan 6, 2023
Docker Extension to backup and restore docker volumes.

vackup-docker-extension This repository contains a Docker Extension that backups Docker volumes. This extension is purely a demonstration of how it'd

Felipe Cruz Martinez 34 Dec 28, 2022
A GUI to browse and restore restic backup repositories.

A simple, cross-platform restic backup GUI for browsing and restoring restic repositories. Built with Wails2, based on leaanthony's Restoric PoC. Down

Eduard Mรผller / taktik 91 Dec 29, 2022