babel-plugin-mocktail
v0.1.16
Published
Mocktail.mock() all your exports
Downloads
7
Maintainers
Readme
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
andunmocked
tests in separate runs - Name your
default
export
s to reduce DI collision chance env(ENV.TESTING)
should be in separate file (setup
)setup
should be imported before any otherimport
- mocks should be in separate
mock file
s mock file
s should be imported before testedsource
simport
s
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