jest-get-mocks
Recursively find mocks in your source code for your projects Jest config. Returns a flattened object of absolute (global
mocks) and relative path key
:value
(<module_name>
:<mock_function_path>
pairs by searching a directory for mock
functions. For global mocks, found folders will be recursively searched and treated as scoped modules. For relative
mocks, found folders will be recursively searched for __mocks__
folders.
Note: For relative mocks, excludes
<rootDir>/src/__mocks__
, but includes<rootDir>/src/ ** /__mocks__/ *
.
Install
Using NPM
$ npm install --save-dev jest-get-mocks
Using Yarn
$ yarn add --dev jest-get-mocks
Usage
Import the package then apply the results to your moduleNameMapper
field in your jest
configuration.
const jestGetMocks = ;const path = ; const mocks = ; moduleexports = moduleNameMapper: ...mocks ;
If you run the example in the ./demo
folder your moduleNameMapper
will look like the following:
moduleexports = moduleNameMapper: fs: '<rootDir>/src/__mocks__/fs' uuid: '<rootDir>/src/__mocks__/uuid' '\\/now$': '<rootDir>/src/utils/__mocks__/now' ;
Contributing
Run, yarn lint && yarn test