Giter Club home page Giter Club logo

Comments (16)

nathan-kansu avatar nathan-kansu commented on September 26, 2024 12

Whilst I agree a 'setComputed' method should be added, you actually don't need it.
Just add a 'computed' property along with your other mounting options, like so...

const wrapper = mount(Component, {
  ...
  computed: {
    foo() {
      return 'bar'
    },
  },
})

from vue-test-utils.

eddyerburgh avatar eddyerburgh commented on September 26, 2024 3

I agree, most of the time you should test it, but if there's a use case for setting a computed property I think we should add it.

I think @jackmellis described a use case well:

There have been times when a method relies on a computed property that is so complicated it's taken me far outside the purpose of my unit test just to get the right computed value!

from vue-test-utils.

jonnyparris avatar jonnyparris commented on September 26, 2024 2

Only just saw this after creating a similar issue on the Avoriaz repo. A setComputed method is exactly what I need.

from vue-test-utils.

Austio avatar Austio commented on September 26, 2024 1

@jonnyparris that is a fair point, i think with the feature we should add something to documentation with some potential gotchas with it.

from vue-test-utils.

jorySsense avatar jorySsense commented on September 26, 2024 1

Sure the works for mounting but you can't trigger the watcher on mount. You need to update the reactive data that the watcher is observing. If it's a computed value, or a getter thats mocked from the store you can't force the computed value to update.

from vue-test-utils.

blocka avatar blocka commented on September 26, 2024

from vue-test-utils.

Austio avatar Austio commented on September 26, 2024

I can see a pragmatism to this.

I do agree with @blocka though and think that the downside with setComputed is that it is encouraging the use of computed properties in the same way you would use something like props or data. You could end up in a situation where you are unit tested 100% by setting computed and have a component that does not work. Guess it is a black box vs white box test thing.

I'm interested in the scenario/thoughts for needing this and if it would be better to create a separate component that you pass the computed property to as a prop and then test that component?

from vue-test-utils.

jonnyparris avatar jonnyparris commented on September 26, 2024

@Austio why not make both options available and let people decide for themselves?
Ideally I could have unit level tests for all my computed and method functions (which encourages smaller single-purpose functions) and have a 'component integration test' that checks all of those unit tests playing well together.

from vue-test-utils.

eddyerburgh avatar eddyerburgh commented on September 26, 2024

I made a PR — #84

from vue-test-utils.

lusarz avatar lusarz commented on September 26, 2024

What do you think about adding something like computedMock mounting option:
mount(Component, { computedMock: { someVariable () {return 'mocked value'}}
and use extends inside mount:

const ExtendedComponent = {
  extends: Component,
  computed: params.computed
}
return mount(ExtendedComponent);

It's only skeleton of idea, I'm not sure if it's even possible.

from vue-test-utils.

eddyerburgh avatar eddyerburgh commented on September 26, 2024

I think we should add setComputed, because we already have setData, setMethods, and setProps

from vue-test-utils.

kleinjm avatar kleinjm commented on September 26, 2024

Can you please add this method to docs? Thank you

from vue-test-utils.

eddyerburgh avatar eddyerburgh commented on September 26, 2024

Thanks for reminding me. I'll add it to my list.

If you are able make a PR it would be very helpful!

from vue-test-utils.

jorySsense avatar jorySsense commented on September 26, 2024

any hope on bringing back this feature,

What if my computed value was set by getters from vuex and im mocking the getters?

And then i want to test a watcher that is listening for changes on that value?

I would want a way to force update the computed value

from vue-test-utils.

blocka avatar blocka commented on September 26, 2024

from vue-test-utils.

jorySsense avatar jorySsense commented on September 26, 2024

@nath-codes thats great for mounting, the issue is further explained #331

from vue-test-utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.