Vue-test-utils: Feature request: Add setStubs method

Created on 28 Nov 2017  Â·  8Comments  Â·  Source: vuejs/vue-test-utils

It would be great to implement a setStubs method. In most cases we use a global wrapper which is modified in specific tests with setData, setProps, setComputed and so on.

I find this approach quite clean and readable but when I want to stub components I have to recreate the wrapper only for this reason.

Is there a plan to implement it? It is even possible?

Thanks!

feature request

Most helpful comment

It is a bit redundant, for me, reproducing over and over again the almost same wrapper.

I understand the feeling, but can only recommend to write clean, isolated tests nontheless.

the boilerplate you have to write is not more, and it's not really a performance issue either unless the component is monstrously big and doing heavy computations in created or something.

All 8 comments

This is possible.

Assuming you'd like to restore stubs, we'd need to keep track of all the stubbed components. I'd like to get other opinions on whether this would be useful for users.

There's a config object that you can use to set stubs—https://vue-test-utils.vuejs.org/en/api/config.html

Does this solve your problem?

That is the object we use. My proposal was modify a wrapper created in the beforeEach as we do with setData, setProps...

Without this method we always have to recreate the full wrapper. Maybe is a wrong workflow. I don't know.

Aha, I understand. I believe this will be difficult to implement.

In most cases we use a global wrapper which is modified in specific tests with setData, setProps, setComputed and so on.

That doesn't sound like a good idea to me. Whe you reuse the same wrapper for multiple tests, you can easily introduce false positives/negatives into your tests by missing that state leaks from one test into the other.

Every test should start with a clean, freshly created wrapper.

Concerning the concrete question, my option is this: There's no "real-life" way in which the implementation of a component could switch during runtime, so I don't think we should provide a way to do that in tests. Such a function would allow tests to mess with internals in a way that can't happen in real life which, again, could lead to false results.

Sidenote: I'm not a fan of setComputed for the same reason.

Yeah. I am always learning best practices and happy to see different points of view. But there are cases when a component always have a shared scenario. It is a bit redundant, for me, reproducing over and over again the almost same wrapper.

I am not forcing you to develop this feature. I just only thinking out loud. 😄

It is a bit redundant, for me, reproducing over and over again the almost same wrapper.

I understand the feeling, but can only recommend to write clean, isolated tests nontheless.

the boilerplate you have to write is not more, and it's not really a performance issue either unless the component is monstrously big and doing heavy computations in created or something.

I'm closing this. As @LinusBorg said, it would encourage reusing a wrapper between tests, which could cause leaks between tests.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lusarz picture lusarz  Â·  3Comments

AustinGil picture AustinGil  Â·  3Comments

benm-eras picture benm-eras  Â·  3Comments

matt-sanders picture matt-sanders  Â·  3Comments

kjugi picture kjugi  Â·  3Comments