forked from jest-community/eslint-plugin-jest
/
no-mocks-import.js
38 lines (34 loc) · 952 Bytes
/
no-mocks-import.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict';
const { posix } = require('path');
const { getDocsUrl } = require('./util');
const mocksDirName = '__mocks__';
const message = `Mocks should not be manually imported from a ${mocksDirName} directory. Instead use jest.mock and import from the original module path.`;
const isMockPath = path => path.split(posix.sep).includes(mocksDirName);
module.exports = {
meta: {
docs: {
url: getDocsUrl(__filename),
},
},
create(context) {
return {
ImportDeclaration(node) {
if (isMockPath(node.source.value)) {
context.report({ node, message });
}
},
'CallExpression[callee.name="require"]'(node) {
if (
node.arguments.length &&
node.arguments[0].value &&
isMockPath(node.arguments[0].value)
) {
context.report({
loc: node.arguments[0].loc,
message,
});
}
},
};
},
};