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

Better toHaveExactText #568

Open
grosch opened this issue Jul 3, 2022 · 4 comments
Open

Better toHaveExactText #568

grosch opened this issue Jul 3, 2022 · 4 comments

Comments

@grosch
Copy link

grosch commented Jul 3, 2022

Description

The toHaveExactText test is pretty fragile. If I use this HTML

<some-tag>hello</some-tag>

Then I can safely expect(spectator.query('some-tag')).toHaveExactText('hello').

However, as soon as some-tag gets a ton of new attributes, and then somebody's IDE reformats like so:

<some-tag lots of attributes here>
  hello
</some-tag>

Then the test fails. I don't think that it should, because what I'm testing is for the text that's visible. In the second example, the DOM returns that string as ' hello ', i.e. there's a single space at the start and end.

I can't use toHaveText instead because that does a contains check, vs. an actual "does this string match". As an example, toHaveText would return true if the text was actually "Othello" because it's then doing a contains.

Proposed solution

Add a second optional parameter to toHaveExactText that specifies whether or not to trim the text. It would of course default to false to prevent any existing code base from breaking.

Alternatives considered

Could have something like toHaveExactTrimmedText

Do you want to create a pull request?

No

@levsim2016
Copy link

levsim2016 commented Jul 11, 2022

That would be really nice to have this. I have similar problems by using just toHaveText, so instead i use something similar to expect(spectator.query('.some-class').textContent.trim()).toBe('expected text'). In my opinion that's uncomfortable

@levsim2016
Copy link

@matheo do you have any thoughts on this issue?

@matheo
Copy link
Contributor

matheo commented Oct 3, 2022

@levsim2016 seems legit to have an option to trim these cases,
cc @keitoaino

@TheSteinn
Copy link

This looks to have been addressed in the above PR. Can this be closed now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants