-
Notifications
You must be signed in to change notification settings - Fork 668
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
Add convenience methods to encourage use for data-testid #1500
Comments
Another alternative is We also have Someone in #1498 suggested a single method with options. Eg I think there is some benefit to having a single method (eg, |
I like the idea of using What do you think about making find.byTestId('...')
find.byClass('...')
find.byQuerySelector('...') This way is more organized and readable, and |
Personally, the only change I would do, is to have predictable return types. How I find components should be my choice, as for example my current project has very strict rules on what attributes and classes get applied, so most of the searching is done by Components. This is not my choice. So if you query for Vue Instance (which you want to drop) you get that. (You could do complex assertions on props, things that are not in the DOM at all.) If you query for DOM, you get DOM. wrapper.find(query: string): DOMWrapper {}
wrapper.findComponent(query: string | ref | name): VueWrapper {} PS: |
Maybe just encouraging people to use a dti helper is adequate. I agree that breaking changes is never a fun thing. Regarding findByComponent, if we can support it for Vue 3, we should - the expectation is after finding a component you can access the vm though, which doesn’t look like it’s possible at this point, at least not in the way as v2. Does your current project do find(Foo).vm/props frequently, or is it more just find(Foo).exists() @dobromir-hristov ? Is this likely to be difficult to migrate to Vue 3? |
I thought about this a bit more. I am leaning towards @dobromir-hristov's argument. As a developer, I personally like libraries that push you to develop using certain styles, practices etc. I'd rather have someone smarter than me make the important decisions, like how best to find a DOM element. I'm not sure VTU should be this opinionated, at least by default. Some options are:
|
@lmiller1990 I like the idea of extendability, like how Chai has their plugin system... extend the assertion type and a using method to add it on. I guess this would be similar to the |
Yep, giving users ability to extend and build on what we provide seems like a good idea. Everyone has their own version of readable tests... we have some discussion for vtu next about this topic here: vuejs/test-utils#55. |
Ok, so we will have some kind of plugin system, see vuejs/test-utils#82 The idea of a repo with community plugins with a For those who like this practice (like me) you can make an extension, or you can use the awesome testing-library which supports this out of the box. |
As suggested here, I'm opening up this issue to suggest the addition of methods that simplify the usage of test data attributes.
Using
data-testid
(ordata-test
, the name specifics is not the goal of the issue) might become one of the suggested ways to query components in VTU. In short, with a test-related data attribute we make the relationship between test and code under test resilient and more explicit (among other benefits).What problem does this feature solve?
However, right now it is quite hard to type, prone to errors, and even slow to read:
wrapper.find('[data-testid="fooBar"]')
Not to mention how this would look if we needed to add some variables and backticks to the mix.
What does the proposed API look like?
I see two main options to avoid this problem, and thus make the recommended way a bit more comfortable:
To provide a different method:
findByTestId('fooBar')
.Is easy to grasp and self-explanatory, but splits the find() API in two. This is the approach used in the Testing Library family (source).
To make data-testid the default behavior for
find
, so thatfind('fooBar')
becomesfind('[data-testid="fooBar"]')
internally in VTU.This would mean that
find('button')
would no longer return a button element, but rather the element withdata-testid="button"
. This might be undesirable.Thoughts?
The text was updated successfully, but these errors were encountered: