npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

object-sandbox

v1.0.3

Published

Nodejs object sandboxes

Downloads

14

Readme

object-sandbox

Save and restore definition of Nodejs objects to allow stubbing of properties, methods, getters, and setters during testing.

Usage

Sandbox objects

let obj1 = {
    method() { console.log('original'); }
};
let obj2 = {
    foo: 'bar'
};

const objectSandbox = require('object-sandbox');
let sandbox = objectSandbox.create(obj1, obj2);

Sandbox addtional objects

let obj3 = { another: 'object' };
sandbox.add(obj3);

Change object properties

obj1.method = () => console.log('stubbed');
obj2.foo = 'rab';

obj1.method(); // stubbed
obj2.foo === 'rab' // true

Restore all objects in the sandbox

sandbox.restore(); 

obj1.method(); // original
obj2.foo === 'bar' // true

This can be used to sandbox and restore objects with sinon stubbed getters and setters, as sinon doesn't support restoring getters (2.2.0), as it tries to run the getter while restoring, and sinon.sandbox doesn't support defining getters.

const sinon = require('sinon');
require('chai').should();

const objectSandbox = require('object-sandbox');

class MyClass {
    constructor() {
        this.event = 'no event';
    }
    get prop() {
        this.event = 'original getter run';
        return 'original value';
    }
    set prop(value) {
        this.event = 'original setter run: ' + value;
    }
}

describe('Sandboxed object', () => {
    let sandbox, instance;

    beforeEach( () => {
        sandbox = objectSandbox.create(MyClass.prototype);
        sinon.stub(MyClass.prototype, 'prop').get(function () {
            this.event = 'stubbed getter run';
            return 'test value';
        });
        instance = new MyClass;
    });

    afterEach( () => {
        sandbox.restore();
    });

    it('returns the stubbed getter', () => {
        instance.prop.should.equal('test value');
        instance.event.should.equal('stubbed getter run');
    });

    it('runs the original setter', () => {
        instance.prop = 'new value';
        instance.event.should.equal('original setter run: new value');
    });

    it('runs the original getter when restored', () => {
        sandbox.restore();
        instance.prop.should.equal('original value');
        instance.event.should.equal('original getter run');
    });

    it('does not run any getters or setters when restoring', () => {
        sandbox.restore();
        instance.event.should.equal('no event');
    });
});