Skip to content

fix(prefer-in-document): check that argument exists before accessing #242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/__tests__/lib/rules/prefer-empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as rule from "../../../rules/prefer-empty";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
ruleTester.run("prefer-empty", rule, {
valid: [
`expect().toBe(true)`,
`expect(element.innerHTML).toBe('foo')`,
`expect(element.innerHTML).toBe(foo)`,
`expect(element.innerHTML).toBe(foo + bar)`,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/lib/rules/prefer-focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as rule from "../../../rules/prefer-focus";
const ruleTester = new RuleTester();
ruleTester.run("prefer-focus", rule, {
valid: [
`expect().toBe(true)`,
`expect(input).not.toHaveFocus();`,
`expect(input).toHaveFocus();`,
`expect(document.activeElement).toBeNull()`,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/lib/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function invalidCase(code, output) {
}

const valid = [
"expect().toBe(true)",
...["getByText", "getByRole"].map((q) => [
`expect(screen.${q}('foo')).toBeInTheDocument()`,
`expect(${q}('foo')).toBeInTheDocument()`,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/lib/rules/prefer-prefer-to-have-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const errors = [{ messageId: "use-to-have-class" }];
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
ruleTester.run("prefer-to-have-class", rule, {
valid: [
`expect().toBe(true)`,
`const el = screen.getByText("foo"); expect(el).toHaveClass("bar")`,
`const el = screen.getByText("foo"); expect(el.class).toEqual(foo)`,
`const el = screen.getByText("foo"); expect(el).toHaveAttribute("class")`,
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/lib/rules/prefer-to-have-attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as rule from "../../../rules/prefer-to-have-attribute";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
ruleTester.run("prefer-to-have-attribute", rule, {
valid: [
"expect().toBe(true)",
"expect(element.foo).toBeTruthy()",
"expect(element.getAttributeNode()).toBeNull()",
`expect(element.getAttribute('foo')).toBeGreaterThan(2)`,
Expand All @@ -44,8 +45,7 @@ ruleTester.run("prefer-to-have-attribute", rule, {
output: `expect(element).toHaveAttribute('foo', expect.stringContaining('bar'));`,
},
{
code:
"expect(element.getAttribute('foo')).toContain(`bar=${encodeURIComponent(baz.id)}`);",
code: "expect(element.getAttribute('foo')).toContain(`bar=${encodeURIComponent(baz.id)}`);",
errors: [
{
message: "Use toHaveAttribute instead of asserting on getAttribute",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/lib/rules/prefer-to-have-style.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const errors = [
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
ruleTester.run("prefer-to-have-style", rule, {
valid: [
`expect().toBe(true)`,
`expect(el).toHaveStyle({foo:"bar"})`,
`expect(el.style).toMatchSnapshot()`,
`expect(el.style).toEqual(foo)`,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/lib/rules/prefer-to-have-text-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as rule from "../../../rules/prefer-to-have-text-content";
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
ruleTester.run("prefer-to-have-text-content", rule, {
valid: [
`expect().toBe(true)`,
`expect(string).toBe("foo")`,
`expect(element).toHaveTextContent("foo")`,
`expect(container.lastNode).toBe("foo")`,
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/lib/rules/prefer-to-have-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020 } });
const errors = [{ messageId: "use-to-have-value" }];
ruleTester.run("prefer-to-have-value", rule, {
valid: [
`expect().toBe(true)`,
`expect(screen.getByRole("radio").value).toEqual("foo")`,
`expect(screen.queryAllByRole("checkbox")[0].value).toStrictEqual("foo")`,
`async function x() { expect((await screen.findByRole("button")).value).toBe("foo") }`,
Expand Down
8 changes: 7 additions & 1 deletion src/rules/prefer-in-document.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ function usesToHaveLengthZero(matcherNode, matcherArguments) {
}

export const create = (context) => {
const alternativeMatchers = /^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual|toBeTruthy|toBeFalsy)$/;
const alternativeMatchers =
/^(toHaveLength|toBeDefined|toBeNull|toBe|toEqual|toBeTruthy|toBeFalsy)$/;
function getLengthValue(matcherArguments) {
let lengthValue;

Expand Down Expand Up @@ -216,6 +217,11 @@ export const create = (context) => {
node
) {
const arg = node.callee.object.arguments[0];

if (!arg) {
return;
}

const queryNode =
arg.type === "AwaitExpression" ? arg.argument.callee : arg.callee;
const matcherNode = node.callee.property;
Expand Down