Socket
Socket
Sign inDemoInstall

eslint-plugin-jest-dom

Package Overview
Dependencies
4
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.6.2 to 3.6.3

dist/assignment-ast.js

40

dist/rules/prefer-in-document.js

@@ -10,2 +10,4 @@ "use strict";

var _assignmentAst = require("../assignment-ast");
/**

@@ -45,3 +47,3 @@ * @fileoverview prefer toBeInTheDocument over checking getAttribute/hasAttribute

if (matcherArguments[0].type === "Identifier") {
const assignment = getAssignmentForIdentifier(matcherArguments[0].name);
const assignment = (0, _assignmentAst.getAssignmentForIdentifier)(context, matcherArguments[0].name);

@@ -117,30 +119,2 @@ if (!assignment) {

function getAssignmentFrom(expression) {
return expression.type === "TSAsExpression" ? getAssignmentFrom(expression.expression) : expression.type === "AwaitExpression" ? getAssignmentFrom(expression.argument) : expression.callee ? expression.callee : expression;
}
function getAssignmentForIdentifier(identifierName) {
const variable = context.getScope().set.get(identifierName);
if (!variable) return;
const init = variable.defs[0].node.init;
let assignmentNode;
if (init) {
// let foo = bar;
assignmentNode = getAssignmentFrom(init);
} else {
// let foo;
// foo = bar;
const assignmentRef = variable.references.reverse().find(ref => !!ref.writeExpr);
if (!assignmentRef) {
return;
}
assignmentNode = getAssignmentFrom(assignmentRef.writeExpr);
}
return assignmentNode;
}
return {

@@ -165,3 +139,3 @@ // expect(<query>).not.<matcher>

[`MemberExpression[object.object.callee.name=expect][object.property.name=not][property.name=${alternativeMatchers}][object.object.arguments.0.type=Identifier]`](node) {
const queryNode = getAssignmentForIdentifier(node.object.object.arguments[0].name);
const queryNode = (0, _assignmentAst.getAssignmentForIdentifier)(context, node.object.object.arguments[0].name);
const matcherNode = node.property;

@@ -172,3 +146,3 @@ const matcherArguments = node.parent.arguments;

negatedMatcher: true,
queryNode,
queryNode: queryNode && queryNode.callee || queryNode,
matcherNode,

@@ -182,3 +156,3 @@ matcherArguments,

[`MemberExpression[object.callee.name=expect][property.name=${alternativeMatchers}][object.arguments.0.type=Identifier]`](node) {
const queryNode = getAssignmentForIdentifier(node.object.arguments[0].name);
const queryNode = (0, _assignmentAst.getAssignmentForIdentifier)(context, node.object.arguments[0].name);
const matcherNode = node.property;

@@ -188,3 +162,3 @@ const matcherArguments = node.parent.arguments;

negatedMatcher: false,
queryNode,
queryNode: queryNode && queryNode.callee || queryNode,
matcherNode,

@@ -191,0 +165,0 @@ matcherArguments

@@ -44,3 +44,5 @@ "use strict";

fix(fixer) {
return [fixer.removeRange([checkedProp.range[1], expectArg.range[1]]), fixer.replaceText(matcher, `${isTruthy ? "" : "not."}toHaveClass`), matcherArg ? fixer.replaceText(matcherArg, context.getSourceCode().getText(classValue)) : fixer.insertTextBeforeRange([node.range[1] - 1, node.range[1] - 1], context.getSourceCode().getText(classValue))];
return [fixer.removeRange([checkedProp.range[1], expectArg.range[1]]), fixer.replaceText(matcher, `${isTruthy ? "" : "not."}toHaveClass`), matcherArg ? fixer.replaceText(matcherArg, context.getSourceCode().getText(classValue)) : fixer.insertTextBefore(context.getSourceCode().getTokenAfter(matcher, {
skip: 1
}), context.getSourceCode().getText(classValue))];
}

@@ -47,0 +49,0 @@

@@ -8,2 +8,6 @@ "use strict";

var _queries = require("../queries");
var _assignmentAst = require("../assignment-ast");
/**

@@ -31,44 +35,102 @@ * @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute

const create = context => ({
// expect(element.value).toBe('foo') / toEqual / toStrictEqual
[`CallExpression[callee.property.name=/to(Be|(Strict)?Equal)$/][callee.object.arguments.0.property.name=value][callee.object.callee.name=expect]`](node) {
context.report({
messageId,
node,
const create = context => {
function validateQueryNode(nodeWithValueProp) {
const queryNode = nodeWithValueProp.type === "Identifier" ? (0, _assignmentAst.getAssignmentForIdentifier)(context, nodeWithValueProp.name) : (0, _assignmentAst.getInnerNodeFrom)(nodeWithValueProp);
fix(fixer) {
return [fixer.removeRange([node.callee.object.arguments[0].object.range[1], node.callee.object.arguments[0].property.range[1]]), fixer.replaceText(node.callee.property, "toHaveValue")];
}
if (!queryNode || !queryNode.callee) {
return {
isValidQuery: false,
isValidElement: false
};
}
});
},
const query = queryNode.callee.name || queryNode.callee.property.name;
const queryArg = queryNode.arguments[0] && queryNode.arguments[0].value;
// expect(element.value).not.toBe('foo') / toEqual / toStrictEqual
[`CallExpression[callee.property.name=/to(Be|(Strict)?Equal)$/][callee.object.object.callee.name=expect][callee.object.property.name=not][callee.object.object.arguments.0.property.name=value]`](node) {
context.report({
messageId,
node,
const isValidQuery = _queries.queries.includes(query);
fix(fixer) {
return [fixer.removeRange([node.callee.object.object.arguments[0].object.range[1], node.callee.object.object.arguments[0].property.range[1]]), fixer.replaceText(node.callee.property, "toHaveValue")];
const isValidElement = query.match(/^(get|find|query)ByRole$/) && ["textbox", "dropdown"].includes(queryArg);
return {
isValidQuery,
isValidElement
};
}
return {
// expect(element.value).toBe('foo') / toEqual / toStrictEqual
// expect(<query>.value).toBe('foo') / toEqual / toStrictEqual
// expect((await <query>).value).toBe('foo') / toEqual / toStrictEqual
[`CallExpression[callee.property.name=/to(Be|(Strict)?Equal)$/][callee.object.arguments.0.property.name=value][callee.object.callee.name=expect]`](node) {
const valueProp = node.callee.object.arguments[0].property;
const matcher = node.callee.property;
const queryNode = node.callee.object.arguments[0].object;
const {
isValidQuery,
isValidElement
} = validateQueryNode(queryNode);
function fix(fixer) {
return [fixer.remove(context.getSourceCode().getTokenBefore(valueProp)), fixer.remove(valueProp), fixer.replaceText(matcher, "toHaveValue")];
}
});
},
if (isValidQuery) {
context.report({
messageId,
node,
fix: isValidElement ? fix : undefined,
suggest: isValidElement ? undefined : [{
desc: `Replace ${matcher.name} with toHaveValue`,
fix
}]
});
}
},
//expect(element).toHaveAttribute('value', 'foo') / Property
[`CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=value][arguments.1][callee.object.callee.name=expect], CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=value][arguments.1][callee.object.object.callee.name=expect][callee.object.property.name=not]`](node) {
context.report({
messageId,
node,
// expect(element.value).not.toBe('foo') / toEqual / toStrictEqual
// expect(<query>.value).not.toBe('foo') / toEqual / toStrictEqual
// expect((await <query>).value).not.toBe('foo') / toEqual / toStrictEqual
[`CallExpression[callee.property.name=/to(Be|(Strict)?Equal)$/][callee.object.object.callee.name=expect][callee.object.property.name=not][callee.object.object.arguments.0.property.name=value]`](node) {
const queryNode = node.callee.object.object.arguments[0].object;
const valueProp = node.callee.object.object.arguments[0].property;
const matcher = node.callee.property;
const {
isValidQuery,
isValidElement
} = validateQueryNode(queryNode);
fix(fixer) {
return [fixer.replaceText(node.callee.property, "toHaveValue"), fixer.removeRange([node.arguments[0].range[0], node.arguments[1].range[0]])];
function fix(fixer) {
return [fixer.removeRange([context.getSourceCode().getTokenBefore(valueProp).range[0], valueProp.range[1]]), fixer.replaceText(matcher, "toHaveValue")];
}
});
}
if (isValidQuery) {
context.report({
messageId,
node,
fix: isValidElement ? fix : undefined,
suggest: isValidElement ? undefined : [{
desc: `Replace ${matcher.name} with toHaveValue`,
fix
}]
});
}
},
});
//expect(element).toHaveAttribute('value', 'foo') / Property
[`CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=value][arguments.1][callee.object.callee.name=expect], CallExpression[callee.property.name=/toHave(Attribute|Property)/][arguments.0.value=value][arguments.1][callee.object.object.callee.name=expect][callee.object.property.name=not]`](node) {
const matcher = node.callee.property;
const [prop, value] = node.arguments;
context.report({
messageId,
node,
fix(fixer) {
return [fixer.replaceText(matcher, "toHaveValue"), fixer.removeRange([prop.range[0], value.range[0]])];
}
});
}
};
};
exports.create = create;
{
"name": "eslint-plugin-jest-dom",
"version": "3.6.2",
"version": "3.6.3",
"description": "ESLint plugin to follow best practices and anticipate common mistakes when writing tests with jest-dom",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -17,3 +17,3 @@ <div align="center">

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-12-orange.svg?style=flat-square)](#contributors-)
[![All Contributors](https://img.shields.io/badge/all_contributors-13-orange.svg?style=flat-square)](#contributors-)
<!-- ALL-CONTRIBUTORS-BADGE:END -->

@@ -162,2 +162,3 @@ [![PRs Welcome][prs-badge]][prs]

<td align="center"><a href="http://www.antn.se"><img src="https://avatars0.githubusercontent.com/u/785676?v=4" width="100px;" alt=""/><br /><sub><b>Anton Niklasson</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=AntonNiklasson" title="Documentation">πŸ“–</a></td>
<td align="center"><a href="http://juzerzarif.com"><img src="https://avatars3.githubusercontent.com/u/22772637?v=4" width="100px;" alt=""/><br /><sub><b>Juzer Zarif</b></sub></a><br /><a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=juzerzarif" title="Code">πŸ’»</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/commits?author=juzerzarif" title="Tests">⚠️</a> <a href="https://github.com/testing-library/eslint-plugin-jest-dom/issues?q=author%3Ajuzerzarif" title="Bug reports">πŸ›</a></td>
</tr>

@@ -168,3 +169,2 @@ </table>

<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->

@@ -171,0 +171,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc