1. rollup-plugin-stub
Stub exported bindings in a module.
rollup-plugin-stub
Package: rollup-plugin-stub
Created by: eventualbuddha
Last modified: Sun, 26 Jun 2022 14:01:57 GMT
Version: 1.2.0
License: MIT
Downloads: 1,424
Repository: https://github.com/eventualbuddha/rollup-plugin-stub

Install

npm install rollup-plugin-stub
yarn add rollup-plugin-stub

rollup-plugin-stub

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

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code