@putout/plugin-convert-mock-require-to-mock-import
Version:
πPutout plugin adds ability to convert mockRequire to mockImport
74 lines (52 loc) β’ 1.7 kB
Markdown
# @putout/plugin-convert-mock-require-to-mock-import [![NPM version][NPMIMGURL]][NPMURL]
[NPMIMGURL]: https://img.shields.io/npm/v/@putout/plugin-convert-mock-require-to-mock-import.svg?style=flat&longCache=true
[NPMURL]: https://npmjs.org/package/@putout/plugin-convert-mock-require-to-mock-import "npm"
π[**Putout**](https://github.com/coderaiser/putout) plugin adds ability to convert [mockRequire](https://github.com/boblauer/mock-require) to [mockImport](https://github.com/coderaiser/mock-import). Moved to [`@putout/plugin-tape`](https://www.npmjs.com/package/@putout/plugin-tape).
## Install
```
npm i @putout/plugin-convert-mock-require-to-mock-import -D
```
## Rule
Rule `convert-mock-require-to-mock-import` is enabled by default for `*.mjs`, to disable add to `.putout.json`:
Good complement of [@putout/plugin-tape](https://github.com/coderaiser/putout/tree/master/packages/plugin-tape#putoutplugin-tape-)
```json
{
"rules": {
"convert-mock-require-to-mock-import": "off"
}
}
```
## β Example of incorrect code
```js
const mockRequire = require('mock-require');
const {reRequire, stopAll} = mockRequire;
test('', (t) => {
mockRequire('fs/promises', {
unlink: stub(),
});
const fn = reRequire('..');
fn();
stopAll();
t.end();
});
```
## β
Example of correct code
```js
import {createMockImport} from 'mock-import';
const {
mockImport,
reImport,
stopAll,
} = createMockImport(import.meta.url);
test('', async (t) => {
mockImport('fs/promises', {
unlink: stub(),
});
const fn = await reImport('..');
fn();
stopAll();
t.end();
});
```
## License
MIT