Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add findby variable declaration to prefer-find-by when auto fixing #197

Merged
merged 1 commit into from Jul 20, 2020

Conversation

gndelia
Copy link
Collaborator

@gndelia gndelia commented Jul 15, 2020

This PR fixes #167

The following scenario

// incorrect
const { getByText } = render(<Foo />)
await waitFor(() => getByText('foo'))

now becomes

// correct - and findByText is defined
const { getByText, findByText } = render(<Foo />)
await findByText('foo')

this should even work if they are in different levels. For instance

// incorrect
const { getByText } = render(<Foo />)
it('tests', async () => {
   await waitFor(() => getByText('foo'))
})

should be converted to

// correct - findByText is defined
// as long as the customRender is in any parent scope from the original code, this works
const { getByText, findByText } = customRender(<Foo />)
it('tests', async () => {
     await findByText('foo')
})

there are some scenarios that I assume they are out of scope, and I am seriously not sure how to fix them. For instance, if the query is passed from a function, like

function commonCode(getByText) {
  await waitFor(() => getByText(baz))
// do stuff
}

// later, in a test
commonCode(render().getByText)

or similar, I can't fix it.. but I guess that sounds like a non-realistic scenario.

Another that might be slightly more realistic (or actually, less complex) might be something like

const utils= render(baz)
const getByText = utils.getByText
await waitFor(() => getByText('foo'))

I am not fixing that either. I'd assume if they are storing the result from the render, then they are using that variable. These snippets seem mostly hacks to avoid the rule, rather than code that someone would write on its own.

and a demo gif!

2020-06-26_17-08-17

(The spaces are due to other eslint rules I've configured on the project I tested this 馃憖 )

P.S: I'm not using the fork 馃帀

@gndelia gndelia added the bug Something isn't working label Jul 15, 2020
@gndelia gndelia self-assigned this Jul 15, 2020
}],
output: `
const getByRole = render().getByRole
const submitButton = await findByRole('baz', { name: 'button' })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would turn runnable code into breaking code, right?
Because findByRole isn't defined.

If that's the case I would consider this a bad experience as it could potentially break a lot of code.

Copy link
Collaborator Author

@gndelia gndelia Jul 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would.
but it's was already hapenning before this PR. There are a couple of scenarios where the fix breaks.
This PR fixes one of them. This test you've commented is still unfixed. Not quite sure if we can't fix it, as it seems there are multiple scenarios to fix (like I've mentioned in the PR description) and the fix might become way complex. I think fixes should be for simple scenarios.

Maybe we went too far by making this rule autofixable? Not sure.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to that, I am not sure how often that would be a realistic scenario. I'd think that devs use destructuring or call the function from screen or the object returned from the render function. But not quite sure. I'm open to debate it.

I still want to enforce this PR does not introduce this scenario, but rather, fixes one of the many possible scenarios where findBy* method is added and might not be defined. the scenarios were there already, and the bug mentioned one of them (the one I've fixed)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks for the clarification.
For the record, I'm not against it.

I do agree that most devs will use screen, especially since we're recommended it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope no one uses Testing Library this way to be honest 馃槄 I think we are fine by covering all the other cases.

Copy link
Member

@Belco90 Belco90 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, thank you!


const ruleTester = createRuleTester({
ecmaFeatures: {
jsx: true,
},
});

function buildFindByMethod(queryMethod: string) {
return `${getFindByQueryVariant(queryMethod)}${queryMethod.split('By')[1]}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of doing this kind of "importing the function calculating something to check the same in the tests". This can lead to silent errors, so I prefer to hardcode or reimplement this kind of behaviors in testing side. Anyway, I think for this case it's fine.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally, agree, but I was using that function everywhere and it was so small... I couldn't resist the temptation (?)

@Belco90 Belco90 merged commit 993fd8c into master Jul 20, 2020
@Belco90 Belco90 deleted the gndelia/bug/prefer-find-by-fix-undeclared-variable branch July 20, 2020 11:05
@Belco90
Copy link
Member

Belco90 commented Jul 20, 2020

馃帀 This PR is included in version 3.3.2 馃帀

The release is available on:

Your semantic-release bot 馃摝馃殌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[prefer-find-by] When query methods come from destructuring, findBy* might not be defined
3 participants