-
Notifications
You must be signed in to change notification settings - Fork 266
feat: expose everything on wrapper.vm to help testing script setup
#931
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<template> | ||
<div id="root"> | ||
<div id="msg">{{ msg }}</div> | ||
<div>{{ other }}</div> | ||
</div> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { defineComponent, ref } from 'vue' | ||
|
||
export default defineComponent({ | ||
name: 'Hello', | ||
|
||
setup(props, { expose }) { | ||
const other = ref('other') | ||
expose({ other }) | ||
return { | ||
msg: ref('Hello world'), | ||
other | ||
} | ||
} | ||
}) | ||
</script> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<script setup lang="ts"> | ||
// imported components are also directly usable in template | ||
import { ref } from 'vue' | ||
import Hello from './Hello.vue' | ||
|
||
const count = ref(0) | ||
const inc = () => { | ||
count.value++ | ||
} | ||
|
||
defineExpose({ | ||
count | ||
}) | ||
</script> | ||
|
||
<template> | ||
<button @click="inc">{{ count }}</button> | ||
<Hello /> | ||
</template> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { mount } from '../src' | ||
import Hello from './components/Hello.vue' | ||
import DefineExpose from './components/DefineExpose.vue' | ||
import ScriptSetupExpose from './components/ScriptSetup_Expose.vue' | ||
import ScriptSetup from './components/ScriptSetup.vue' | ||
|
||
describe('expose', () => { | ||
it('access vm on simple components', async () => { | ||
const wrapper = mount(Hello) | ||
|
||
expect(wrapper.vm.msg).toBe('Hello world') | ||
}) | ||
|
||
it('access vm on simple components with custom `expose`', async () => { | ||
const wrapper = mount(DefineExpose) | ||
|
||
// other is exposed vie `expose` | ||
expect(wrapper.vm.other).toBe('other') | ||
// can access `msg` even if not exposed | ||
expect(wrapper.vm.msg).toBe('Hello world') | ||
}) | ||
|
||
it('access vm with <script setup> and defineExpose()', async () => { | ||
const wrapper = mount(ScriptSetupExpose) | ||
|
||
await wrapper.find('button').trigger('click') | ||
expect(wrapper.html()).toContain('1') | ||
// can access `count` as it is exposed via `defineExpose()` | ||
expect(wrapper.vm.count).toBe(1) | ||
}) | ||
|
||
it('access vm with <script setup> even without defineExpose()', async () => { | ||
const wrapper = mount(ScriptSetup) | ||
|
||
await wrapper.find('button').trigger('click') | ||
expect(wrapper.html()).toContain('1') | ||
// can access `count` even if it is _not_ exposed | ||
expect(wrapper.vm.count).toBe(1) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { defineComponent, ref } from 'vue' | ||
|
||
import { mount } from '../src' | ||
|
||
describe('vm', () => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
"compilerOptions": { | ||
"lib": ["DOM", "ES2020"], | ||
"skipLibCheck": true | ||
} | ||
}, | ||
"exclude": ["tests/expose.spec.ts"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. volar can't figure out that we magically expose more things on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this isn't really something than can be fixed - it makes little sense for Volar to have something special for test utils. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I think there was an issue even with regular |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could also consider alternatives like:
wrapper.proxy
, allowing to doexpect(wrapper.proxy.count).toBe(2)
, without changingwrapper.vm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expect(wrapper.proxy.count).toBe(2)
doesn't seem ideal - unless you know Vue andscript setup
very well, it'd be hard to understand (and even document). I think tests should be written the same way, regardless of whether you are using script setup, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I feel the same. I think testing different types of components should not change the way tests are written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally support @cexbrayat point - but being a devil's advocate - component implementation specific already leaks into VTU: for example you can't use
data()
option for class components, you won't be able to usecomponents
param with functional components, etc. I'm a bit concerned that we are doing exceptions here, althoughscript setup
is designed to be "blackboxed"