babel-plugin-transform-module-exports-reflection

0.1.2 • Public • Published

Babel Plugin for ES6 Module Export Reflection

This plugin injects an ES6 named export called 'export' into all ES6 file file that it processes. This export can be used to introspect and mutate the exported values of the module itself for test mocking purposes.

The primary usecase for this module is for tests, where it can be used to mock out parts of your application.

Examples

Mocha

import foo, {__export__} from './utilities/foo';
import bar from './utilities/bar';

describe('bar with stubbed foo', function(){
    beforeEach(function(){
        __export__('default').set(sinon.stub().returns(42));
    });

    afterEach(function(){
        __export__('default').restore();
    });

    it('should test a thing', function(){
        var result = bar();

        expect(result).to.eql(42);
        expect(foo).to.be.called;
    });
});

Direct API Usage

// index.js
import {foo, __export__} from './test';

console.log(foo); // 'bar'

__export__('foo').set('new value');

console.log(foo); // 'new value'

__export__('foo').restore();

console.log(foo); // 'bar'

// test.js
export var foo = 'bar';

Methods

The following three operations are available:

.set(value)

Override the existing exported value with some new arbitrary value.

Note: If a module reassigns its own exports internally, that will also cause the set value to be lost, just as if the .restore() function had been called.

.original()

Access the current non-overridden value of the module export.

.restore()

Revert the overridden export back to the non-overridden value.

Notes

This plugin only allows for inspecting and mutating local bindings exported from the module. Bindings re-exported from submodules will not be visible.

Readme

Keywords

none

Package Sidebar

Install

npm i babel-plugin-transform-module-exports-reflection

Weekly Downloads

1

Version

0.1.2

License

MIT

Last publish

Collaborators

  • loganfsmyth