babel-plugin-mocktail

0.1.16 • Public • Published

babel-plugin-mocktail

NPM Version Coverage Status Bithound Status Build Status Downloads Dependency Status License

Mocktail.mock() all your exports

Usage (Mocha)

Setup

npm install --save-dev babel-cli babel-plugin-mocktail mocha assert

Create example source for testing

File: src/example.js

const Example = "unmocked"
export default Example

Create ENV.TESTING trigger

File: test/setup.js

import { env, ENV } from "mocktail"
env(ENV.TESTING)

Create mock file

File: test/example.mock.js

import { inject } from "mocktail"
inject("Example", "mocked")

Create test (unmocked)

File: test/unmocked.test.js

import assert from "assert"
import Example from "../src/example"
 
describe("Unmocked Example", () => {
  it("should be unmocked", () => assert(Example === "unmocked"))
})

Create unit test (mocked)

File: test/mocked.test.js

import "./setup"
import "./example.mock"
import assert from "assert"
import Example from "../src/example"
 
describe("Mocked Example", () => {
  it("should be mocked", () => assert(Example === "mocked"))
})

Run the tests

babel-node --plugins mocktail $(which _mocha) test/unmocked.test.js
babel-node --plugins mocktail $(which _mocha) test/mocked.test.js

Run the tests INCORRECTLY

babel-node --plugins mocktail $(which _mocha) test/*.test.js

Notes

  • Example Repository
  • Avoid using the plugin outside the test context
  • Run mocked and unmocked tests in separate runs
  • Name your default exports to reduce DI collision chance
  • env(ENV.TESTING) should be in separate file (setup)
  • setup should be imported before any other import
  • mocks should be in separate mock files
  • mock files should be imported before tested sources imports

How it works

The plugin walks the AST and looks for export declarations.
When it finds the declaraions, they are replaced with mock(declaration, name).
The name is either the name of the declaration or camel-cased filename.

Installation

npm install --save-dev babel-plugin-mocktail

Documentation (ESDOC)

https://nhz-io.github.io/babel-plugin-mocktail

LICENSE

MIT

Package Sidebar

Install

npm i babel-plugin-mocktail

Weekly Downloads

2

Version

0.1.16

License

MIT

Last publish

Collaborators

  • ishi.ruy