question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected behaviour when watching

See original GitHub issue

Version

1.0.0-beta.13

Reproduction link

https://github.com/tbuteler/vue-test-utils-issue

Steps to reproduce

Clone the repository, then run:

yarn install
yarn run tests

What is expected?

Component:

<template>
  <div id="app">
    <div>
      <pre>data.text: <em>{{ text }}</em></pre>
    </div>
    <!-- Tests fail in 1.0.0-beta.13 with .fails portion of the code -->
    <div class="fails">
      <pre>computed.text: <em>{{ computedText }}</em></pre>
    </div>
  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      text: '',
      basket: []
    }
  },
  computed: {
    computedText () {
      return this.text
    }
  },
  watch: {
    text () {
      this.basket.push(this.computedText)
    }
  }
}
</script>

Test:

import { shallow } from '@vue/test-utils'
import App from '../src/App.vue'

describe('App.vue', () => {
  it('updates computed properties', () => {
    const wrapper = shallow(App)
    expect(wrapper.vm.text).toBe('')
    expect(wrapper.vm.basket).toEqual([])
    wrapper.setData({ text: 'foo' })
    expect(wrapper.vm.text).toBe('foo')
    expect(wrapper.vm.computedText).toBe('foo')
    expect(wrapper.vm.basket).toEqual(['foo']) // Fails
  })
})

In the text watch handler, I expected the computed property to have been updated to the latest value of text.

What is actually happening?

The computed property is somehow lagging behind. Curiously, this only happens when the computed property is actually being rendered inside the template, otherwise it behaves as expected.


This issue was not present in 1.0.0-beta.12, regardless of what the template contains. From the release information, I would suspect it has something to do with the new sync functionality.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tbutelercommented, Apr 17, 2018

Tested this today with my actual suite and its components, which are far more complex than the examples above and everything worked as expected. Thanks for your effort!

1reaction
eddyerburghcommented, Mar 31, 2018

This is a bug caused by dependencies running in the incorrect order. I’ve created a fix that will be released in the next version.

For the moment, you will need to set sync to false and use Vue.nextTick:

it('updates computed properties', (done) => {
  const wrapper = shallow(App, {
    sync: false
  })
  Vue.nextTick(() => {
    expect(wrapper.vm.basket).toEqual(['foo']) // Fails
    done()
  })
})

Alternatively, you can use beta.12.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expected and Unexpected Behaviors - YouTube
Watch later · Share · Copy link.
Read more >
Expected and Unexpected Behaviour: Introduction - YouTube
This video explains the meaning of expected and unexpected behaviors. This is an important foundational social skills to teach young ...
Read more >
Zones of Regulation - Expected and Unexpected Behaviors ...
Zones of Regulation - Expected and Unexpected Behaviors with Mrs. Connell & Mrs. Conner. 16K views 1 year ago.
Read more >
Hidden Rules and Expected and Unexpected Behavior
Hidden Rules and Expected and Unexpected Behavior. null watching now · null total views. Playback speed. Popout. Overflow menu. Keyframe Interval:.
Read more >
Teaching Expected and Unexpected Behaviors
Expected behavior is simply behavior that is normal, reasonable and anticipated. Unexpected behavior is behavior that is out of the norm, and is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found