Socket
Socket
Sign inDemoInstall

privatestate

Package Overview
Dependencies
38
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    privatestate

Expose your private state in your tests.


Version published
Maintainers
1
Install size
6.11 MB
Created

Readme

Source

privatestate

Build Status devDependency Status

This module enables files written with the CommonJS module syntax to expose private functions for testing.

getting started

In order to use this module, you must have rewire enabled for all required modules. If you are using browserify, you should enable the browserify transform rewireify. If you are using node, you should use rewire-global.

example

When writing a module, you may have private helper functions:

function helper() {
  return 'helper';
}

var Utils = {
  run: function() {
    return helper();
  }
};

module.exports = Utils;

When we write our tests, we can't access helper when we require our file. privatestate lets us access it.

Expose your function like this:

var privateState = require('privatestate');

function helper() {
  return 'helper';
}

var Utils = {
  run: function() {
    return helper();
  }
};

privateState.exposeForTesting(Utils, ['helper']);

module.exports = Utils;

In our test, we can now access our function:

var utils = require('./utils');
var privateState = require('privatestate');
var sinon = require('sinon');

describe('Utils', function() {
  it('should give us a function', function() {
    var helper = privateState.getForTesting(utils, 'helper');
    assert.isFunction(helper);
  });

  it('should allow us to modify our function and restore', function() {
    privateState.setFunctionForTesting(utils, 'helper', sinon.stub().returns('in test'));

    assert.equal('in test', utils.run());

    privateState.restore(utils);
  });
});

Keywords

FAQs

Last updated on 06 Nov 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc