-
Notifications
You must be signed in to change notification settings - Fork 88
universal/isomorphic example #8
Comments
As you found, we check that That said, we definitely need a way to test server-side code. I'm closing this issue, but we should definitely carry this conversation on. Are you able to create a new feature request in vue-test-utils using the issue creator? |
Thanks for your response @eddyerburgh. I managed to hack my way round the problem and test both the client and server branches of my logic by creating a helper that overrides import { createLocalVue, mount } from 'vue-test-utils'
const setLocalVueEnv = (localVue, isServer) => {
Object.defineProperty(localVue.prototype, '$isServer', {
get: () => isServer
})
}
const EnvTextComponent = {
render(createElement) {
const text = this.$isServer ? 'server' : 'client'
return createElement('span', text)
}
}
test('client env', () => {
const isServer = false
const localVue = createLocalVue()
setLocalVueEnv(localVue, isServer) // not required obviously
const wrapper = mount(EnvTextComponent, { localVue })
expect(wrapper.vm.$isServer).toBe(isServer)
expect(wrapper.text()).toBe('client')
})
test('server env', () => {
const isServer = true
const localVue = createLocalVue()
setLocalVueEnv(localVue, isServer)
const wrapper = mount(EnvTextComponent, { localVue })
expect(wrapper.vm.$isServer).toBe(isServer)
expect(wrapper.text()).toBe('server')
}) ...perhaps passing some options to const localVue = createLocalVue({ node: true }) // could use 'server' or 'isServer' here?
const wrapper = mount(SomeComponent, { localVue }) I will create a new feature request in |
Done: vuejs/vue-test-utils#427 |
How can I test Vue components in a
node
environment?Jest provides a few mechanisms for setting the test environment to either
jsdom
(default) ornode
.The simplest and most flexible way to do this is by adding a docblock comment to the top of a test file:
However, when doing so
vue-test-utils
throws the error:My use case:
I have written a custom
directive
that performs different operations on theclient
andserver
.I am using the Vue instance property
$isServer
to determine which function to call based on the environment that the directive is run in:When run in the default
jsdom
test environment,$isServer
is alwaysfalse
so I am only able to test theclientHandler
branch of my directive.How do I test the
server
branch of this directive?The text was updated successfully, but these errors were encountered: