Giter VIP home page Giter VIP logo

rollup-plugin-stub's Introduction

rollup-plugin-stub Build Status npm

Stub module exports at runtime when bundling with Rollup.

Install

$ npm install --save-dev rollup-plugin-stub

Usage

This plugin is intended to be used only when building for tests where stubs are required. Consider using dependency injection instead, which is a natural fit both for production code and for tests.

import { rollup } from 'rollup';
import stub from 'rollup-plugin-stub';

rollup({
  entry: 'test/main_test.js',
  plugins: [
    stub()
  ]
}).then(...)

Once the stub plugin is set up, you will have some extra exports in all the modules built by rollup. For example, given this module:

// path.js
export function join(...parts) {
  return parts.join(SEP);
}

export const SEP = '/';

You'll be able to stub any named exports from that module, like so:

// main.js
import { join, stub_SEP } from './path';

console.log(join('a', 'b', 'c')); // 'a/b/c'
stub_SEP('!');
console.log(join('a', 'b', 'c')); // 'a!b!c'

Note that this plugin takes advantage of the fact that ES modules use bindings for named exports. Since the default export is not bound, this plugin does not work with default exports.

In tests you might want to stub a value for one test and reset it afterward. You can do that too:

// test.js
import { join, SEP, stub_SEP, reset_SEP } from './path';
import { strictEqual as eq } from 'assert';

describe('join', () => {
  afterEach(reset_SEP);
  
  it('joins using SEP', () => {
    stub_SEP('&');
    eq(join('a', 'b'), 'a&b');
  });
});

License

MIT

rollup-plugin-stub's People

Contributors

dirv avatar eventualbuddha avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.