Vue-test-utils: Error not thrown inside computed property

Created on 3 Jun 2020  路  4Comments  路  Source: vuejs/vue-test-utils

Version

1.0.3

Reproduction link

https://codesandbox.io/s/naughty-hofstadter-qsycv?file=/src/components/HelloWorld.spec.js

Steps to reproduce

  1. Write a component with a computed property
  2. throws an error inside the computed property
  3. Try to test this error with "toThrowError"

What is expected?

Error in computed property would be thrown

What is actually happening?

Error in computed property wouldn't be thrown

bug

All 4 comments

This could be to do with the custom error handler. https://github.com/vuejs/vue-test-utils/blob/dev/packages/test-utils/src/error.js. Vue (or VTU) could be swallowing the error.

I have tried fixing this once before but no luck.

Related: https://github.com/vuejs/vue-test-utils/issues/1503

I actually do not think this is an issue with VTU, but instead Jest. I noticed in your code sandbox that an async function was passed into expect. This unfortunately doesn't work quite like most would expect it to behave. This issue as well as this stack overflow post should provide clarity.

In short, this will work:

    try {
      const wrapper = shallowMount(HelloWorld, {
        propsData: {
          color: "not-in-map"
        }
      });
      await wrapper.vm.$nextTick();
    }catch(e){
      expect(e).toEqual(new Error(`Color not found: not-in-map`))
    }

As well as a few other methods discussed here

I think you might be correct.

Might close this one, I don't think we can fix this in VTU; the above post should be a good enough work-around.

Works perfect, thx!

Was this page helpful?
0 / 5 - 0 ratings