eslint-plugin-jest-dom
Advanced tools
Comparing version 3.6.3 to 3.6.4
@@ -8,3 +8,6 @@ "use strict"; | ||
exports.getAssignmentForIdentifier = getAssignmentForIdentifier; | ||
exports.getQueryNodeFrom = getQueryNodeFrom; | ||
var _queries = require("./queries"); | ||
/** | ||
@@ -19,3 +22,15 @@ * Gets the inner relevant node (CallExpression, Identity, et al.) given a generic expression node | ||
function getInnerNodeFrom(expression) { | ||
return expression.type === "TSAsExpression" ? getInnerNodeFrom(expression.expression) : expression.type === "AwaitExpression" ? getInnerNodeFrom(expression.argument) : expression; | ||
switch (expression.type) { | ||
case "TSAsExpression": | ||
return getInnerNodeFrom(expression.expression); | ||
case "AwaitExpression": | ||
return getInnerNodeFrom(expression.argument); | ||
case "MemberExpression": | ||
return getInnerNodeFrom(expression.object); | ||
default: | ||
return expression; | ||
} | ||
} | ||
@@ -53,2 +68,34 @@ /** | ||
return assignmentNode; | ||
} | ||
/** | ||
* get query node, arg and isDTLQuery flag for a given node. useful for rules that you only | ||
* want to apply to dom elements. | ||
* | ||
* @param {Object} context - Context for a rule | ||
* @param {Object} nodeWithValueProp - AST Node to get the query from | ||
* @returns {Object} - Object with query, queryArg & isDTLQuery | ||
*/ | ||
function getQueryNodeFrom(context, nodeWithValueProp) { | ||
const queryNode = nodeWithValueProp.type === "Identifier" ? getAssignmentForIdentifier(context, nodeWithValueProp.name) : getInnerNodeFrom(nodeWithValueProp); | ||
if (!queryNode || !queryNode.callee) { | ||
return { | ||
isDTLQuery: false, | ||
query: null, | ||
queryArg: null | ||
}; | ||
} | ||
const query = queryNode.callee.name || queryNode.callee.property.name; | ||
const queryArg = queryNode.arguments[0] && queryNode.arguments[0].value; | ||
const isDTLQuery = _queries.queries.includes(query); | ||
return { | ||
queryArg, | ||
query, | ||
isDTLQuery | ||
}; | ||
} |
@@ -8,2 +8,4 @@ "use strict"; | ||
var _assignmentAst = require("./assignment-ast"); | ||
var _default = ({ | ||
@@ -16,4 +18,5 @@ preferred, | ||
const isBannedArg = node => attributes.some(attr => attr === node.arguments[0].value); | ||
const isBannedArg = node => attributes.some(attr => attr === node.arguments[0].value); //expect(el).not.toBeEnabled() => expect(el).toBeDisabled() | ||
return { | ||
@@ -34,2 +37,3 @@ [`CallExpression[callee.property.name=/${preferred}|${negatedPreferred}/][callee.object.property.name='not'][callee.object.object.callee.name='expect']`](node) { | ||
//expect(getByText('foo').<attribute>).toBeTruthy() | ||
"CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']"(node) { | ||
@@ -52,2 +56,6 @@ const { | ||
const { | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]); | ||
if (!isDTLQuery) return; | ||
const isNegated = matcher.endsWith("Falsy") || (matcher === "toBe" || matcher === "toEqual") && matcherArg !== true; | ||
@@ -83,2 +91,6 @@ const correctFunction = getCorrectFunctionFor(node.callee.object, isNegated); | ||
const { | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]); | ||
if (!isDTLQuery) return; | ||
const correctFunction = getCorrectFunctionFor(node); | ||
@@ -85,0 +97,0 @@ const incorrectFunction = node.callee.property.name; |
@@ -8,2 +8,4 @@ "use strict"; | ||
var _assignmentAst = require("../assignment-ast"); | ||
/** | ||
@@ -86,3 +88,7 @@ * @fileoverview prefer toHaveClass over checking element className | ||
const matcher = node.callee.property; | ||
const classNameProp = node.callee.object.arguments[0].object; // don't report here if using `expect.foo()` | ||
const classNameProp = node.callee.object.arguments[0].object; | ||
const { | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp); | ||
if (!isDTLQuery) return; // don't report here if using `expect.foo()` | ||
@@ -109,2 +115,6 @@ if (classValue.type === "CallExpression" && classValue.callee.type === "MemberExpression" && classValue.callee.object.name === "expect") return; | ||
const matcherArg = node.arguments[0].callee.property; | ||
const { | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp); | ||
if (!isDTLQuery) return; | ||
context.report({ | ||
@@ -128,2 +138,6 @@ node: matcher, | ||
const classNameProp = node.callee.object.object.arguments[0].object; | ||
const { | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, classNameProp); | ||
if (!isDTLQuery) return; | ||
context.report({ | ||
@@ -148,2 +162,6 @@ node: matcher, | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return; | ||
const { | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]); | ||
if (!isDTLQuery) return; | ||
context.report({ | ||
@@ -168,2 +186,6 @@ node: matcher, | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return; | ||
const { | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.object.arguments[0]); | ||
if (!isDTLQuery) return; | ||
context.report({ | ||
@@ -188,2 +210,6 @@ node: matcher, | ||
if (matcher.name === "toHaveAttribute" && classNameValue !== "class" || matcher.name === "toHaveProperty" && classNameValue !== "className") return; | ||
const { | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, node.callee.object.arguments[0]); | ||
if (!isDTLQuery) return; | ||
context.report({ | ||
@@ -190,0 +216,0 @@ node: matcher, |
@@ -8,4 +8,2 @@ "use strict"; | ||
var _queries = require("../queries"); | ||
var _assignmentAst = require("../assignment-ast"); | ||
@@ -37,7 +35,11 @@ | ||
function validateQueryNode(nodeWithValueProp) { | ||
const queryNode = nodeWithValueProp.type === "Identifier" ? (0, _assignmentAst.getAssignmentForIdentifier)(context, nodeWithValueProp.name) : (0, _assignmentAst.getInnerNodeFrom)(nodeWithValueProp); | ||
const { | ||
query, | ||
queryArg, | ||
isDTLQuery | ||
} = (0, _assignmentAst.getQueryNodeFrom)(context, nodeWithValueProp); | ||
if (!queryNode || !queryNode.callee) { | ||
if (!query) { | ||
return { | ||
isValidQuery: false, | ||
isDTLQuery: false, | ||
isValidElement: false | ||
@@ -47,10 +49,5 @@ }; | ||
const query = queryNode.callee.name || queryNode.callee.property.name; | ||
const queryArg = queryNode.arguments[0] && queryNode.arguments[0].value; | ||
const isValidQuery = _queries.queries.includes(query); | ||
const isValidElement = query.match(/^(get|find|query)ByRole$/) && ["textbox", "dropdown"].includes(queryArg); | ||
return { | ||
isValidQuery, | ||
isDTLQuery, | ||
isValidElement | ||
@@ -69,3 +66,3 @@ }; | ||
const { | ||
isValidQuery, | ||
isDTLQuery, | ||
isValidElement | ||
@@ -78,3 +75,3 @@ } = validateQueryNode(queryNode); | ||
if (isValidQuery) { | ||
if (isDTLQuery) { | ||
context.report({ | ||
@@ -100,3 +97,3 @@ messageId, | ||
const { | ||
isValidQuery, | ||
isDTLQuery, | ||
isValidElement | ||
@@ -109,3 +106,3 @@ } = validateQueryNode(queryNode); | ||
if (isValidQuery) { | ||
if (isDTLQuery) { | ||
context.report({ | ||
@@ -112,0 +109,0 @@ messageId, |
{ | ||
"name": "eslint-plugin-jest-dom", | ||
"version": "3.6.3", | ||
"version": "3.6.4", | ||
"description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom", | ||
@@ -49,3 +49,3 @@ "main": "dist/index.js", | ||
"@typescript-eslint/parser": "^4.8.2", | ||
"eslint": "7.14", | ||
"eslint": "7.15", | ||
"eslint-remote-tester": "^0.3.3", | ||
@@ -52,0 +52,0 @@ "jest-extended": "^0.11.5", |
@@ -167,2 +167,3 @@ <div align="center"> | ||
<!-- prettier-ignore-end --> | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
@@ -169,0 +170,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
76998
1225
200