Why
Do you like to write paths in jest.mock()
calls? I don't. If you don't like too, then you're in the right place. Probably.
The following input:
; ; ; // this is just placeholder for transformation, will be removed from output jest; jest;
will be transformed to:
jest; jest; jest; ; ; ;
You can say you have to write paths in import
calls anyway. But you're wrong if you use IDE/editor with autoimporting feature - just start to write symbol name and you'll get the imported path for free.
API
jest.mockObj()
jest.mockObj(...args: any[]);
Creates string mock with default implementation to return identifier name. Very handy to mock react components for ReactTestRenderer
:
// comp1.jsimport InnerComp from "./inner";const Comp = <div><InnerComp /></div>; // comp1.spec.jsimport InnerComp from "../inner";import Comp from "../comp1"; // Easy including with IDE autoimportingjest; const testRenderer = TestRenderer;; // <div><InnerComp /></div>
You can pass implementation for module export too:
; jest; jest;
This will be transformed to:
jest;
jest.mockFn()
Note Requires jest 22.0+
jest;
Creates jest.fn()
mock calls for specified symbols
; jest;
This will be transformed to:
jest;
Partial mocking (experimental)
Given some file:
{ return "func1"; } { return "func2"; }const a = "a";
Want to mock only func1
but leave original behavior for func2/a
? It's possible, put into your babel configuration:
plugins:
And following test file:
;jest; ; // test; // func2console; // a
will be transformed to:
jest;
You need either spread transform enabled in the babel conf, or node 8.4+
Nesting is being supported for one level deep from module export:
; ; jest; jest; // declarations from the same module will be merged into one module mock
will result to:
jest; jest; ; ;
For explicit module mocks with jest.mock
, jest.doMock
, jest.unmock
and jest.dontMock
the transformation will be ignored:
; jest; // the order doesn't matter jest;
will be transformed to:
; jest;
Installation
npm install babel-plugin-jest-easy-mock --save-dev
Add to your .babelrc
or in .babelrc.js
:
plugins: ["jest-easy-mock"]
Configuration
Plugin exposes few configuration options:
jestIdentifier
- Jest identifier used to create actual mocks
requireActual
- Do require.requireActual
for partial module mocks
ignoreIdentifiers
- List of call expression identifiers to ignore the mocking
identfiers
- List of call identifiers configuration to do the mocking
Default config:
jestIdentifier: "jest" mockIdentifiers: "jest.mock" "jest.doMock" "jest.unmock" "jest.dontMock" identifiers: name: "jest.mockObj" remove: true type: "name" name: "jest.mockFn" remove: true type: "mock" requireActual: false
To pass custom conviguration add to your babelrc/baberc.js:
"plugins":
Usage with Typescript
Unfortunately TS when compiling to commonjs modules, produces very hard to statically understand structure, so use "module": "es2015"
for output and let babel to transform the output with babel-plugin-transform-es2015-modules-commonjs
. Altenatively, you can just use babel for all TS transpilation with preset babel-preset-typescript
and use TS only for editor/IDE purposes.