rubocop / rubocop-capybara

Code style checking for Capybara files.
https://docs.rubocop.org/rubocop-capybara
MIT License
41 stars 8 forks source link

Cop idea: disallow using `exact_text` when `Capybara.exact = true` #6

Open boris-petrov opened 2 years ago

boris-petrov commented 2 years ago
Capybara.exact = true
expect(page).to have_link(exact_text: 'foobar')

It would be nice to have a check that disallows passing exact_text to such matchers as that's unneeded - Capybara.exact makes these matcher work like so by default. That is, the above is equivalent to expect(page).to have_link('foobar').

A couple of things to note: 1) this is perhaps difficult to implement as Capybara.exact = true is generally set in a different file spec_helper.rb. Perhaps an option to the plugin could be passed, not sure? 1) there are exceptions to this. If the text contains new lines, exact_text must be passed (because of https://github.com/teamcapybara/capybara/issues/2533).

bquorning commented 2 years ago
  1. I think we’d need to implement this via the cop configuration. The Capybara.exact= is dynamic code which is not executed by RuboCop and, as you mention, may not exist in the same file as the offense.

Just to be sure I understand this – is it correct that when Capybara.exact = true is set, we should change

expect(page).to have_link(exact_text: 'foobar')

into

expect(page).to have_link('foobar')

But the following should not be changed:

expect(page).to have_link(exact_text: <<~TEXT)
  foo
  bar
TEXT
boris-petrov commented 2 years ago

@bquorning sorry for the late reply.

Yes, that's correct. Also expect(page).to have_link(exact_text: "foo\nbar") shouldn't be changed. Also expect(page).to have_link(text: 'foobar') should not be changed because that doesn't look for an exact match but a partial one.

And yes, a cop-configuration is perhaps the way to go for this.

P.S. Code similar to find('td', exact_text: 'foo') and expect(page).to have_css('.some-class', exact_text: 'foo') also shouldn't be changed.