1.0.0 • Published 6 years ago

babel-plugin-testable v1.0.0

Weekly downloads
172
License
MIT
Repository
github
Last release
6 years ago

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.

OptionValuesDefaultDescription
testCommentStringtestableThe comment name to key off for exporting testable code
testCommentRegexRegular Expression^\s*@##comment##\bThe 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
}