-
Notifications
You must be signed in to change notification settings - Fork 266
Setup and defineProps #862
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
May be you need to rethink your testing approach. There is no need to test that props/state have some value, you need to test things closer to user interaction. For example, pass some prop, find an element and assert that text value of this element equals to prop value. Or if you have a function, that makes some modal visible, just emit the click event, find the modal and assert that it is visible. Make some input and test the output. Something like this. |
Thanks @standbyoneself! Suggested advice is a big no-no for me (nonetheless thank you, really appreciate your time and effort to help) - I'm using vue-test-utils for my unit tests, and most of the time I want to assert that my code controls state properly, not really interested in markups and all that integrational nature of it (I use cypress on that level, if needed). Now I wonder - is this a new direction that this tool is taking or just your personal opinion ? Again, hard time finding anything about that in docs (release candidate yet, so its probably understandable). |
Okay, understand you. May be you want to use HelloWorld.vue <template>
<div class="hello-world"></div>
</template>
<script setup>
const someData = 'data';
const someMethod = () => console.log('something cool');
defineExpose({ someData, someMethod });
</script> HelloWorld.spec.js import { shallowMount } from '@vue/test-utils';
import HelloWorld from '@/components/HelloWorld.vue';
describe('HelloWorld.vue', () => {
it('does something cool', () => {
const wrapper = shallowMount(HelloWorld);
console.log(wrapper.vm);
});
}); Outputs:
Here the link to the docs: https://v3.vuejs.org/api/sfc-script-setup.html#defineexpose |
@standbyoneself thanks a lot for that link (and even more for example!) - it makes much more sense now. I wasn’t aware that components defined with Thanks again kind stranger! |
Thanks @standbyoneself for helping here! I just want to correct one statement if someone ends up here:
You can access everything on |
@cexbrayat Hey there! Am I really wrong? Hello.vue: <template></template>
<script>
import { reactive, ref } from '@vue/reactivity';
export default {
name: 'HelloWorld',
setup() {
const number = ref(123);
const array = reactive([1, 2, 3]);
const method = () => {};
},
}
</script> HelloWorld.spec.js: import { shallowMount } from '@vue/test-utils'
import HelloWorld from '@/components/HelloWorld.vue'
describe('HelloWorld.vue', () => {
it('', () => {
const wrapper = shallowMount(HelloWorld);
console.log(wrapper.vm);
});
}); Stdout:
|
The |
@cexbrayat Really. My bad. Fixed my previous comment. Thanks! |
I knew the setup scirpt is closed by default and we need to use Just wondering if there is any workaround for VTU to expose everything by default just within testing scope. |
@axe-me Agreed. Now that script setup is stable, we'll be able to take a look |
Reopening this issue - this + #806 renders |
I may have a solution for the exposition problem: see PR #931 |
I have a hard time testing components that use setup method (either in
<script>
or<script setup>
) like that:test:
Component seems to be mounted properly, though there is no
vm.state.count
orvm.msg
on it :( All of this works when I move these declarations outside of the setup:Am I missing something, like some extra call, await or something ?
Versions:
The text was updated successfully, but these errors were encountered: