Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
chore(no-mocks-import): improve implementation (#378)
  • Loading branch information
G-Rath authored and SimenB committed Aug 11, 2019
1 parent 2ae5870 commit 2748525
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
19 changes: 9 additions & 10 deletions src/rules/no-mocks-import.ts
@@ -1,26 +1,23 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import { posix } from 'path';
import { createRule, isLiteralNode } from './tsUtils';
import { createRule, getStringValue, isStringNode } from './tsUtils';

const mocksDirName = '__mocks__';

const isMockPath = (path: string) =>
path.split(posix.sep).includes(mocksDirName);

const isMockImportLiteral = (expression?: TSESTree.Expression): boolean =>
expression !== undefined &&
isLiteralNode(expression) &&
typeof expression.value === 'string' &&
isMockPath(expression.value);
const isMockImportLiteral = (expression: TSESTree.Expression): boolean =>
isStringNode(expression) && isMockPath(getStringValue(expression));

export default createRule({
name: __filename,
meta: {
type: 'problem',
docs: {
category: 'Best Practices',
description:
'When using `jest.mock`, your tests (just like the code being tested) should import from `./x`, not `./__mocks__/x`. Not following this rule can lead to confusion, because you will have multiple instances of the mocked module',
category: 'Best Practices',
recommended: 'error',
},
messages: {
Expand All @@ -32,14 +29,16 @@ export default createRule({
create(context) {
return {
ImportDeclaration(node: TSESTree.ImportDeclaration) {
if (isMockImportLiteral(node.source)) {
if (node.source && isMockImportLiteral(node.source)) {
context.report({ node, messageId: 'noManualImport' });
}
},
'CallExpression[callee.name="require"]'(node: TSESTree.CallExpression) {
if (isMockImportLiteral(node.arguments[0])) {
const [arg] = node.arguments;

if (arg && isMockImportLiteral(arg)) {
context.report({
node: node.arguments[0],
node: arg,
messageId: 'noManualImport',
});
}
Expand Down
4 changes: 0 additions & 4 deletions src/rules/tsUtils.ts
Expand Up @@ -306,10 +306,6 @@ export const isDescribe = (
);
};

export const isLiteralNode = (node: {
type: AST_NODE_TYPES;
}): node is TSESTree.Literal => node.type === AST_NODE_TYPES.Literal;

export const hasExpressions = (
node: TSESTree.Node,
): node is TSESTree.Expression =>
Expand Down

0 comments on commit 2748525

Please sign in to comment.