babel-plugin-mocktail
Mocktail.mock() all your exports
Usage (Mocha)
Setup
npm install --save-dev babel-cli babel-plugin-mocktail mocha assert
source
for testing
Create example File: src/example.js
const Example = "unmocked"
ENV.TESTING
trigger
Create File: test/setup.js
mock file
Create File: test/example.mock.js
test
(unmocked)
Create File: test/unmocked.test.js
unit test
(mocked)
Create File: test/mocked.test.js
Run the tests
babel-node --plugins mocktail $(which _mocha) test/unmocked.test.jsbabel-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