Socket
Book a DemoInstallSign in
Socket

privatestate

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

privatestate

Expose your private state in your tests.

1.1.0
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
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

test

FAQs

Package last updated on 06 Nov 2015

Did you know?

Socket

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.