/
no-mocks-import.js
41 lines (37 loc) · 1.01 KB
/
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
39
40
41
'use strict';
const { posix } = require('path');
const { getDocsUrl } = require('./util');
const mocksDirName = '__mocks__';
const isMockPath = path => path.split(posix.sep).includes(mocksDirName);
module.exports = {
meta: {
docs: {
url: getDocsUrl(__filename),
},
messages: {
noManualImport: `Mocks should not be manually imported from a ${mocksDirName} directory. Instead use jest.mock and import from the original module path.`,
},
schema: [],
},
create(context) {
return {
ImportDeclaration(node) {
if (isMockPath(node.source.value)) {
context.report({ node, messageId: 'noManualImport' });
}
},
'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,
messageId: 'noManualImport',
});
}
},
};
},
};