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

rehire

v1.1.1

Published

Easy dependency injection for unit tests (based on rewire)

Downloads

8

Readme

rehire

Easy monkey-patching for nodejs unit tests (based on rewire)

Install

npm i rehire

Use cases

How can I test internal function in module?

util.js:

const sum = (...args) => {
    let s = 0;
    for (const i of args) {
        s += i;
    }
    return s;
};

module.exports.printSum = (...args) => {
    console.log(sum(...args));
}

test.js:

const expect = require('chai').expect;
const rehire = require('rehire');
const util = rehire('./util');

describe('sum()', () => {
    it('should calculate sum of numbers', () => {
        const sum = util.__get__('sum');
        expect(sum(1, 2, 3)).to.be.equal(6);
    });
});

Can I call exported functions from imported module with rehire?

Yes, you can call it, like you do via require: util.printSum().

How can I mock console, process & other global objects?

test.js:

const chai = require('chai');
chai.use(require("sinon-chai"));
const expect = chai.expect;
const rehire = require('rehire');
const sinon = require('sinon');
const util = rehire('./util'); // util.js see above

describe('printSum()', () => {
    let console_;

    beforeEach(() => {
        console_ = { log: sinon.stub() };
        util.__set__({ console: console_, sum: () => 1 });
    });

    afterEach(() => {
        util.__reset__();
    });

    it('should print sum of numbers', () => {
        util.printSum();
        expect(console_.log).to.be.calledOnce;
        expect(console_.log.args[0][0]).to.be.equal(1);
    });
});

How can I mock third-party imported modules?

util.js:

const fs = require('fs');

module.exports.resetDir = path => {
    if (fs.exists(path)) {
        fs.rmdirSync(path);
    }
    fs.mkdirSync(path);
};

test.js:

const chai = require('chai');
chai.use(require("sinon-chai"));
const expect = chai.expect;
const rehire = require('rehire');
const sinon = require('sinon');
const util = rehire('./util');

describe('resetDir()', () => {
    let fs;

    beforeEach(() => {
        fs = {
            exists: sinon.stub(),
            rmdirSync: sinon.stub(),
            mkdirSync: sinon.stub(),
        };
        util.__set__({ fs });
    });

    afterEach(() => {
        util.__reset__();
    });

    it('should create dir if it is absent', () => {
        util.resetDir('/path/to/dir');
        expect(fs.exists).to.be.calledOnce;
        expect(fs.rmdirSync).to.not.be.called;
        expect(fs.mkdirSync).to.be.calledOnce;
        expect(fs.mkdirSync.args[0][0]).to.be.equal('/path/to/dir');
    });

    it('should remove existing dir before create', () => {
        fs.exists.returns(true);
        util.resetDir('/path/to/dir');
        expect(fs.exists).to.be.calledOnce;
        expect(fs.rmdirSync).to.be.calledOnce;
        expect(fs.rmdirSync.args[0][0]).to.be.equal('/path/to/dir');
        expect(fs.mkdirSync).to.be.calledOnce;
    });
});

How can I avoid original third-party modules import?

test.js:

const chai = require('chai');
chai.use(require("sinon-chai"));
const expect = chai.expect;
const rehire = require('rehire');
const sinon = require('sinon');
const fs = {};
const util = rehire('./util', { fs }); // util.js see above

describe('resetDir()', () => {

    beforeEach(() => {
        fs.exists = sinon.stub();
        fs.rmdirSync = sinon.stub();
        fs.mkdirSync = sinon.stub();
    });

    it('should create dir if it is absent', () => {
        util.resetDir('/path/to/dir');
        expect(fs.exists).to.be.calledOnce;
        expect(fs.rmdirSync).to.not.be.called;
        expect(fs.mkdirSync).to.be.calledOnce;
        expect(fs.mkdirSync.args[0][0]).to.be.equal('/path/to/dir');
    });

    it('should remove existing dir before create', () => {
        fs.exists.returns(true);
        util.resetDir('/path/to/dir');
        expect(fs.exists).to.be.calledOnce;
        expect(fs.rmdirSync).to.be.calledOnce;
        expect(fs.rmdirSync.args[0][0]).to.be.equal('/path/to/dir');
        expect(fs.mkdirSync).to.be.calledOnce;
    });
});

Should I import rehire in each tests module?

Not necessary, you can make it as global object: require('rehire').global();.

API

rehire(filepath: String): rehiredModule

Imports module.

args:

  • filepath - Path to module (relative or absolute).

return:

  • Rehired module captured by filepath. Use rehire() like require().

rehire(filepath: String, fakeImports: Object): rehiredModule

Imports module.

args:

  • filepath - Path to module (relative or absolute).
  • fakeImports - Object with fake modules which will be loaded in rehired module instead of original. Takes keys of object as fake module names and sets its values as fake modules respectively.

return:

  • Rehired module captured by filepath. Use rehire() like require().

rehire.global()

Makes rehire as available globally function.

rehiredModule.__set__(name: String, value: Any): Function

Sets mock for module's internal entity (variable/module/function).

args:

  • name - Name of entity to mock.
  • value - Mocked value of entity.

return:

  • Function to reset mock.

rehiredModule.__set__(obj: Object): Function

Sets mocks for module's internal entities (variables/modules/functions).

args:

  • obj - Object with mocked entities. Takes keys of object as entity names and sets the values respectively.

return:

  • Function to reset mocks.

rehiredModule.__reset__()

Resets all mocks which had set.

rehiredModule.__get__(name: String): Any

Retrieves internal entity from module.

args:

  • name - Name of internal entity.

return:

  • Entity value.

rehiredModule.__with__(obj: Object): Function<callback: Function>

Returns a function which - when being called - sets obj, executes the given callback and reverts obj. If callback returns a promise, obj is only reverted after the promise has been resolved or rejected. For your convenience the returned function passes the received promise through.

Overview

rehire is based on rewire and extends it with some useful features:

  • method global in case if you want to use it in global context and not import in each tests file. I found issue, that if to make vanilla rewire as global then it detects relative paths wrongly. It's fixed with rehire:

    const rehire = require("rehire");
    // or
    require("rehire").global();
    
    rehire("../my-mod");
  • method __reset__ to reset all mocked objects. You don't need to restore each mock after each test, with rehire it can be done with one function call:

    const my_mod = rehire("../../my-mod");
    
    describe("my scope", () => {
        afterEach(() => {
            my_mod.__reset__();
        });
    
        it("test1", () => {
            my_mod.__set__("func", () => {});
            do_test();
        });
    
        it("test2", () => {
            my_mod.__set__("MY_CONST", 42);
            do_test();
        });
    });
  • second argument to pass patched third-party modules in order to load them instead of original ones in tested module and provide full isolation for unit testing. I like this feature in proxyquire:

    // in tested module
    const fs = require("fs");
    const some_module = require("../third-party-module");
    
    // in tests
    const my_mod = rehire("../../my-mod", {
        "fs": { rmdir: () => {} },
        "../third-party-module": {},
    });