babel-plugin-testable

1.0.0 • Public • Published

babel-plugin-testable

Babel plugin that exports private members for testing purposes

npm downloads total npm version npm license

Installation

npm install babel-plugin-testable --save-dev

or

yarn add babel-plugin-testable --dev

Enable Plugin

Add babel-plugin-testable to your .babelrc

{
  "plugins": ["testable"]
}

or

{
  "env": {
    "test": {
      "plugins": [ "testable" ]
    }
  }
}

Usage

In your source file, annotate the private members that should be exposed for testing with an // @testable comment. Example:

// @testable
const MY_PRIVATE_CONSTANT = 'Some constant';

// @testable
let someTestableField = true;

// @testable - This is a testable class with additional comments
class TestableClass {
  // Class code
}

Then in your test file, you can import them like any other exported declaration.

import { publicFunction, MY_PRIVATE_CONSTANT } from '../';

it('Test MY_PRIVATE_CONSTANT', () => {
  expect(MY_PRIVATE_CONSTANT).toBe('Some constant');
});

Options

The plugin provides the following options to tweak the behavior of the plugin during code generation.

Option Values Default Description
testComment String testable The comment name to key off for exporting testable code
testCommentRegex Regular Expression ^\s*@##comment##\b The comment regular expression to search for testable code. ##comment## is the placeholder used for the value of testComment.

Options Example

A .babelrc configuration example which looks for __TestExport__ anywhere in a comment.

{
  "plugins": [
    [ 
      "testable", 
      {
        "testComment": "TestExport",
        "testCommentRegex": ".*__##comment##__.*"
      }
    ]
  ]
}

Example Output Code

// @testable
export const MY_PRIVATE_CONSTANT = 'Some constant';

// @testable
export let someTestableField = true;

// @testable - This is a testable class with additional comments
export class TestableClass {
  // Class code
}

Package Sidebar

Install

npm i babel-plugin-testable

Weekly Downloads

13

Version

1.0.0

License

MIT

Unpacked Size

154 kB

Total Files

39

Last publish

Collaborators

  • jetclosing