Home Reference Source Test Repository

babel-plugin-mocktail

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

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