Skip to content

Setup and defineProps #862

Closed
Closed
@mgibas

Description

@mgibas

I have a hard time testing components that use setup method (either in <script> or <script setup>) like that:

<script setup>
import { reactive } from 'vue'

defineProps({
  msg: String
})

const state = reactive({ count: 0 })
</script>

test:

import { mount } from '@vue/test-utils'
import HelloWorld from '@/components/hello-world.vue'

test("hellow world", () => {
  const wrapper = mount(HelloWorld, {
    props: {
      msg: 'Hello world'
    }   
  })  

  expect(wrapper.vm.msg).toEqual('Hello world')
  expect(wrapper.vm.state.count).toEqual(0)
})

Component seems to be mounted properly, though there is no vm.state.count or vm.msg on it :( All of this works when I move these declarations outside of the setup:

<script>
export default {
  props: {
    msg: String
  },
  data () {
    return {
      count: 0
    }
  }
}
</script>

Am I missing something, like some extra call, await or something ?

Versions:

"vue": "^3.2.2"
"@vue/test-utils": "^2.0.0-rc.12"
"@vue/compiler-sfc": "^3.2.2"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions