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

Suggestion: un-deprecate attachToDocument #1578

Closed
alexmiddeleer opened this issue Jun 12, 2020 · 13 comments
Closed

Suggestion: un-deprecate attachToDocument #1578

alexmiddeleer opened this issue Jun 12, 2020 · 13 comments

Comments

@alexmiddeleer
Copy link
Contributor

alexmiddeleer commented Jun 12, 2020

I feel the most common use case of attachTo is still attachToDocument, since that is necessary for testing any Vue code that utilizes document.querySelector and similar methods, even if run in total isolation. By un-deprecating attachToDocument, we prevent developers who are new to the library from having to hunt for the following code.

    const elem = document.createElement('div')
    if (document.body) {
      document.body.appendChild(elem)
    }
   wrapper = mount(Component, {
    attachTo: elem
   })
@afontcu
Copy link
Member

afontcu commented Jun 12, 2020

Hi! 👋 Thanks for bringing this up. While it is true that attaching to document is probably the most common scenario, attachTo provides more flexibility to cover other cases, so we'd want to stick with it.

OTOH, it would be great to add this snippet in docs. Would you like to do so? 😄

@alexmiddeleer
Copy link
Contributor Author

alexmiddeleer commented Jun 12, 2020 via email

alexmiddeleer added a commit to alexmiddeleer/vue-test-utils that referenced this issue Jun 14, 2020
@just-boris
Copy link

+1 to the undeprecation suggestion.

What is the reason for all users to write 3 lines of boilerplate, if this can be an extra built-in option? What are the risks of having both attachTo and attachToDocument?

@Jeff-Duke
Copy link

+1 to the un-deprecation suggestion.
why unnecessarily increase boilerplate? why not have both methods?

@lmiller1990
Copy link
Member

Supporting both of these seems pretty reasonable. I think the original PR was to make it more robust, but we did not consider the additional boilerplate that would be introduced.

I wonder if we can have the best of both worlds?

mount(Foo, {
  attachTo: someEl
})

// what about adding support for?

mount(Foo, {
  attachTo: document
})

Thoughts? We get the best of both words, and it reads pretty nicely too. We could just check if value of the attachTo property is a document, and if it is, create a new element, append it to document.body and mount there.

@Jeff-Duke, @just-boris, @afontcu?

@just-boris
Copy link

As far as I understand how attachTo currently works, it is replacing the DOM node with the content from Vue. I tried using it with mount(Foo, { attachTo: document.body}) and it broke, because document.body becomes null for subsequent tests.

Having a separate explicit method is preferred because it is more clear than the difference between attachTo: document.body and attachTo: htmlElement behavior.

@afontcu
Copy link
Member

afontcu commented Jul 13, 2020

I feel it might be hard for newcomers to understand the differences between attachTo, attachToDocument, and even not using them at all.

That being said, if people feel the need to have this baked in, I'm okay with it 👍 as suggested right above, I'd keep it separate from attachTo (unless we find a way to make attachTo and attachToDocument behave similarly!)

@lmiller1990
Copy link
Member

Ok so we just re added the previous method back in? Should be easy. Would anyone like to take responsibility for this?

@JessicaSachs
Copy link
Collaborator

I personally prefer having attachTo just work with document.body and to solve the null error. I was just working with this a few days ago for Cypress' Component Testing solution (which wraps VTU)

@lmiller1990
Copy link
Member

There are use cases for attaching to a specific element apparently (adding Vue into existing applications with pre-existing HTML...)

@vvanpo
Copy link
Contributor

vvanpo commented Aug 17, 2020

In vue@3 and @vue/test-utils@2 using attachTo: document.body works just fine, as Vue 3's app.mount() method works differently from Vue 2's app.$mount(), where the former appends an element to the mount point whereas the latter replaces it. So if we wanted some consistency between the behaviours in version 1 vs. 2, supporting attachTo: document.body and doing something like if (attachTo instanceof HTMLBodyElement) { // append child <div /> as mountpoint might help future migrations a lot more than un-deprecating attachToDocument (which would be redundant to have in version 2).

@lmiller1990
Copy link
Member

We should make the migration process as seamless as possible.

I thikn we should port the above suggestion, and make attachTo: document.body work in this codebase too. It's basically the same amount of typing as attachToDocument: true and much more flexible. A PR implementing this would be welcome.

dannon added a commit to dannon/galaxy that referenced this issue Aug 18, 2020
Workflow test conversion, they pass.  A few chatty bits to clean up.
Make unit test galaxy object access quieter.
dannon added a commit to dannon/galaxy that referenced this issue Aug 24, 2020
Workflow test conversion, they pass.  A few chatty bits to clean up.
Make unit test galaxy object access quieter.
dannon added a commit to dannon/galaxy that referenced this issue Aug 31, 2020
Workflow test conversion, they pass.  A few chatty bits to clean up.
Make unit test galaxy object access quieter.
xanf added a commit to xanf/vue-test-utils that referenced this issue Sep 28, 2020
When using attachTo with document.body as a target do not replace
original content of body but append a new div instead

See vuejs#1578 for details and discussion
xanf added a commit to xanf/vue-test-utils that referenced this issue Sep 28, 2020
When using attachTo with document.body as a target do not replace
original content of body but append a new div instead

See vuejs#1578 for details and discussion
xanf added a commit to xanf/vue-test-utils that referenced this issue Sep 28, 2020
When using attachTo with document.body as a target do not replace
original content of body but append a new div instead

See vuejs#1578 for details and discussion
lmiller1990 pushed a commit that referenced this issue Oct 1, 2020
When using attachTo with document.body as a target do not replace
original content of body but append a new div instead

See #1578 for details and discussion
@lmiller1990
Copy link
Member

This was merged in #1699 and will go out in a few days.

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

7 participants