-
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
Proposal: Official way of targeting elements #1234
Comments
Notes on
I also think
What about I feel like this should be left untouched. The |
I'm currently using (my own) v-test directive for that exact purpose:
can be used like this: |
I made my own fork of jest-serializer-vue with this and other features |
Hmm @TheJaredWilcurt is this issue mostly concerning snapshot testing and Edd’s jest serializer? Can we close it? I don’t think it’s part of VTU’s core functionality. |
Hi @TheJaredWilcurt , sorry no-one followed up on this. There are a bunch of new maintainers on board now and we are trying to catch up on all the outstanding issues. Regarding this issue, I personally do not think there needs to be one "official" way to get elements. I don't think there is any "best" practice for something like this, since everyone's applications and tests are different. These are just my thoughts, I know some (a lot?) of people like to have a guideline on "how to do ____", so I'm interested to see what other people think. |
@JessicaSachs No, sorry for the poor wording. I've edited my comment, and the original post. The snapshots are just one part of this proposal. VTU's repo would still be responsible for adding in the more convenient shorthand of // Will target all elements with a data-test="pageTitle"
wrapper.find({ test: 'pageTitle' }) @lmiller1990 Yes you can use any DOM selector to target an element for your test. And that is useful in many scenarios. However, when adding a token to your DOM solely to make it easier to target for your tests, it is a best practice to use an identifier token unique to testing (such as Pros to using a unique identifier:
Cons to using a unique identifier:
Part of this issue is to identify and address the cons so that there can be a clear "best practice" to recommend people use for the default. The other approaches should still be offered and documented, so people can use best judgement to deviate from Using a unique identifier, specific to testing, to target DOM elements is a widely considered best practice. Resources:
|
I agree using I also would be in support of documenting some best practices around testing. I don't think we should pick an "official" way of targeting elements, and make that the only way to find elements. Vue lets you use I'm happy to accept a PR adding better support for search for These are just my opinions - I'm happy to go with whatever works for the majority of users. What do you think about these comments? |
My two cents: For the time being, I agree with Lachlan's view on not providing "the" right way of doing things – we're currently in "damage control" mode with VTU, and the main goal now is to reach 1.0 in a timely manner. After that, I'm sure we can discuss what's the best (if any) way of doing certain things, and I'd like very much to reduce some duplication in VTU – we have several tools that serve the same purpose, and I believe this might be confusing.
I don't think this would happen, mostly because in Vue Testing Library we target the DOM, so as long as the attribute is still there, we're good. And last but not least, thanks @TheJaredWilcurt for the well thought-out proposal – it is yelding interesting debates and let us focus on the big picture. Keep'em coming! :) |
I thought about this a bit more. I do think there is value in simplifying For the Vue 3 compat version of this library. I think we should drop For the beta and Vue 2 compat, I don't see much benefit in remove ref/name now. We could consider adding a depreciation, if we decide to remove them for Vue 3 compat. |
Just my two cents: Targeting by component is not ideal if we have more than one in the DOM. <template>
<component-a ref="a">
<component-b ref="b" />
<component-b ref="b" />
<component-b ref="b" />
</component-a>
<template> If you are to do I do think we need to think of a more versatile, less error prone way of finding elements, but as of now I cant suggest it :/ |
It's tough. Each solution was imperfect, so we added another alternative. Now we have 4 problems instead of 1 😆 |
Since taking on the
Shorthand proposal:
Something like this could be converted into documentation. |
Hi @lmiller1990 @TheJaredWilcurt 👋 Is this proposal moving forward? I'm also in a situation that would benefit from a I'd gladly move on with a PR if it's OK for you 🚀 |
What is your suggestion @angelogulina? Which proposal are you referring to (there are quite a few here)? Changing how It sounds like this proposal is more about giving people guidance on how to write their tests (eg, docs) than actually changing the library? Is my understanding correct? There is something like this in v2 docs: https://vue-test-utils.vuejs.org/v2/guide/conditional-rendering.html#finding-elements. I would like to share guides between the two, if possible. V2 docs are more about "how to test". V1 is really just "here is the API, good luck" which is really not great. (Editing to be more concise). |
@lmiller1990, I didn't read the Issue as a documentation one, thanks for making it clearer. To reduce the scope of my proposal then (which would probably need a separate Issue), it would be to have a I think it covers many use-cases. What do you think about it? |
Also, if you need any help on the documentation side, I'd be happy to take something! |
I think VTU should regain it's focus as a simple utility library; not an all encompassing library. It is Vue Test Utils, after all, not Vue Testing Library (which already exists). While I personally am opinionated in how I write my apps and tests, I don't think we should be too opinionated about how other people write their apps. Adding this method on top of Finally, if you do want What I do this is valuable is talking about why As far as guides go, I'd suggest focus on v2 docs. This is the future; @afontcu has a roadmap here. The v2 docs are much more well designed; we talk about how to use VTU, not just "here is the API... good luck". This includes things like why using a data-test selector is valuable. Specifically @angelogulina, (or anyone reading this is motivated to contribute), here are 4 things that need to be done.
|
I was really hoping to have the shorthand options added into VTU:
The proposed custom "Find" methods increases the API you have to learn. I don't really like that approach/recommendation. Targeting elements with |
@TheJaredWilcurt I agree with Lachlan's reasoning, which is why I added plugins to vue-test-utils-next a few months ago. Here's how to register a plugin (from my vite starter) import { config } from '@vue/test-utils'
const DataTestIdPlugin = (wrapper) => {
function findByTestId(selector) {
const dataSelector = `[data-testid='${selector}']`
const element = wrapper.element.querySelector(dataSelector)
if (element) {
return element
}
return null
}
return {
findByTestId
}
}
config.plugins.VueWrapper.install(DataTestIdPlugin) and here's a usage const wrapper = mount(Counter)
const buttonCounterEl = wrapper.findByTestId('button-counter') // [data-testid="button-counter"] So you should have no problem building on top of VTU for this! We're hoping people take advantage of the plugin API to build convenience methods that suit their style. |
You could also add it to V1 by just doing
|
I'm still sad this convenience was never adopted. VTU would be so much nicer to work with if this was part of the default API. I have a million I still think this is a massive DX failure. Unit testing should be simpler than this. |
You could consider using the two higher level APIs mentioned in the docs
(testing library or Cy). We don’t recommend using VTU directly in the docs
anymore.
We recommend using @testing-library/vue for testing components in
applications, as its focus aligns better with the testing priorities of
applications. ***@***.***/test-utils only if you are building advanced
components that require testing Vue-specific internals.
https://vuejs.org/guide/scaling-up/testing.html#mounting-libraries
…On Tue, Aug 30, 2022 at 8:38 PM The Jared Wilcurt ***@***.***> wrote:
I'm still sad this convenience was never adopted. VTU would be so much
nicer to work with if this was part of the default API. I have a million
.find('[data-test="token"]') all over my codebases and it's so ugly to
look at every time and a pain to type. And switching between .find and
.findComponent seems completely arbitrary and annoying to have to
distinguish.
I still think this is a massive DX failure. Unit testing should be simpler
than this.
—
Reply to this email directly, view it on GitHub
<#1234 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAVL4BCTPSKXPEFEGTW5M63V32SQZANCNFSM4HNHIHHA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
What problem does this feature solve?
Currently there are several ways of targeting elements demonstrated in the docs (good). But people are confused as what the "best practice" would be and how to remove test tokens from their code in snapshots and production builds.
What does the proposed API look like?
data-
attribute for use. Something like<h1 data-test="pageTitle">
.jest-serializer-vue
now automatically removes alldata-test="whatever"
values from snapshots.data-test
's from the production build.The text was updated successfully, but these errors were encountered: