↩ī¸Ž

abuser

🤕 Create modifiers to require cache based on file path (e.g. __filename)

Supported from node 10.12.0

The abuser function accepts routes from which to resolve the modules as arguments

const abuser = require('abuser');
const {clean, override, reset} = abuser(__filename);

override: Override module exports value with any given thing (other than undefined)

const {override} = require('abuser')(__filename);

override('modulename', () => 'return value');

reset: Reset module cache to intended values, recursively

const {reset} = require('abuser')(__filename);

const myThing = reset('.');

clean: Clean module and its dependencies from memory

const {clean} = require('abuser')(__filename);

clean('.');
const {override, clean} = require('abuser')(__filename);

describe('my suite', () => {
    const dependency = stub();
    let mine;
    before(() => {
        override('dependency', dependency);
        mine = require('.');
    });
    afterEach(() => stub.reset());
    after(() => clean('.'));

    it('Should call "dependency"', () => {
        mine();
        assert(dependency.called);
    });
});