Mocktail.mock() all your exports
npm install --save-dev babel-cli babel-plugin-mocktail mocha assert
File: src/example.js
const Example = "unmocked"
export default Example
File: test/setup.js
import { env, ENV } from "mocktail"
env(ENV.TESTING)
File: test/example.mock.js
import { inject } from "mocktail"
inject("Example", "mocked")
File: test/unmocked.test.js
import assert from "assert"
import Example from "../src/example"
describe("Unmocked Example", () => {
it("should be unmocked", () => assert(Example === "unmocked"))
})
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"))
})
babel-node --plugins mocktail $(which _mocha) test/unmocked.test.js
babel-node --plugins mocktail $(which _mocha) test/mocked.test.js
babel-node --plugins mocktail $(which _mocha) test/*.test.js
- 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
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
.
npm install --save-dev babel-plugin-mocktail