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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove potential false positive assertions #33288

Merged
merged 5 commits into from May 11, 2021

Conversation

spicalous
Copy link
Contributor

querySelector() returns null but expect(document.querySelector('...')).toBeDefined() checks that the value is not undefined. This can lead to false positive tests

@spicalous spicalous requested a review from a team as a code owner March 6, 2021 14:22
@XhmikosR
Copy link
Member

XhmikosR commented Mar 6, 2021

Can you check if there are more cases please?

@spicalous
Copy link
Contributor Author

spicalous commented Mar 6, 2021

Added more cases I could find in unit tests for assertions against values that are assigned from .querySelector(...)

@XhmikosR
Copy link
Member

XhmikosR commented Mar 8, 2021

Thanks for the PR. There must be more instances, from a quick look. At least for getInstance.

@spicalous
Copy link
Contributor Author

Added in ee7e291 👍

@spicalous
Copy link
Contributor Author

Do we still want these changes?

I can rebase to resolve conflicts so it can be ready to merge, otherwise I will close this PR

@spicalous spicalous closed this Apr 17, 2021
@GeoSot GeoSot reopened this Apr 18, 2021
@GeoSot
Copy link
Member

GeoSot commented Apr 18, 2021

@twbs/js-review this might has to be in 5-stable

@spicalous
Copy link
Contributor Author

Thanks @GeoSot please let me know when is the best time to rebase, it would be great if I can avoid doing it multiple times 👍

@GeoSot
Copy link
Member

GeoSot commented Apr 18, 2021

in case of stable-5 is a good time to be rebased. But it doesn't depends by me and only. Thanks for your understanding

querySelector() returns null but

expect(document.querySelector('...')).toBeDefined()

tests that the value is not undefined
@spicalous spicalous force-pushed the remove-false-positive-assertions branch from ee7e291 to bc8be0e Compare April 18, 2021 23:09
@spicalous
Copy link
Contributor Author

I have rebased the changes against master

@XhmikosR
Copy link
Member

Sorry for the delay @spicalous but we are only a few volunteers.

This PR can wait after 5.0.0, since it doesn't fix any bug. I hope you understand.

The same applies to your other PR you closed, you should reopen it until someone has a look at it.

@spicalous
Copy link
Contributor Author

No problemos, just give me a shout when this needs to be rebased again 👍

@GeoSot
Copy link
Member

GeoSot commented Apr 19, 2021

@spicalous I saw about 17 more deDefined checks. If it is easy, please make a check on them too

@spicalous
Copy link
Contributor Author

When looking through some of the matchers, I think it's not very clear when some return null or undefined

What do you guys think about using toBeTruthy / toBeFalsy ?

@XhmikosR
Copy link
Member

toBeTruthy() coerces the values AFAICT so it's a little moot IMHO. Either we are always explicit or not :)

@spicalous
Copy link
Contributor Author

@GeoSot I have updated more cases

Regarding these ones, I think they provide no value and can be removed. What do you guys think?

expect(offset).toBeDefined()

expect(position).toBeDefined()

expect(scrollSpy).toBeDefined()

@spicalous
Copy link
Contributor Author

spicalous commented Apr 21, 2021

toBeTruthy() coerces the values AFAICT so it's a little moot IMHO. Either we are always explicit or not :)

So the original reason for this PR was that I noticed the assertions could result in a failing test to pass e.g. in the case where element is null
expect(element).toBeDefined() === TEST PASS false positive
expect(element).not.toBeNull() === TEST FAIL correct
expect(element).toBeFalsy() === TEST FAIL correct

Similar case for truthy checks

Given that a property is null, we assert .not.toBeNulll (null !== actual)
What happens if behaviour is changed
e.g.
instead of property = null, someone may delete property
In this case, the assertion would pass as undefined !== null and we end up with false positives again

In these cases, wouldnt truthy and falsy assertions be better?

Ah... Javascript..... :)

@GeoSot GeoSot added this to Inbox in v5.0.1 via automation Apr 22, 2021
v5.0.1 automation moved this from Inbox to Approved Apr 28, 2021
@XhmikosR XhmikosR merged commit 052def4 into twbs:main May 11, 2021
v5.0.1 automation moved this from Approved to Done May 11, 2021
@spicalous spicalous deleted the remove-false-positive-assertions branch June 22, 2021 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
v5.0.1
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants