-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
vm.$isServer returns false when using Jest in testEnvironment Node #9232
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
Most of the tests in vue are written for front end, so it makes sense to have that variable to false by default. If you are interested in the check, it's done at src/core/util/env.js (search for isServerRendering). With vue test utils, you can change cc @eddyerburgh maybe you know something else I'm missing here |
vue-server-renderer sets Shouldn't vue respect Jest's |
no, because it would require people to explicitly mock |
@posva it may be worth noting, I am using There's no info anywhere about using |
I think the issue is with docs, mainly., but the general ones for SSR, nothing jest-specific. vue-server-renderer does set We need to update docs here: https://ssr.vuejs.org/guide/build-config.html#server-config It's demonstrated in the hackernew repo (here) but shoul definitely be mentioned in the docs explicitly, of course.. |
@LinusBorg thanks, this fixed my issue, however, I am still confused as to why this is required only in the jest environment, and not in normal node |
I would assume it's because jest runs tests in individual sandboxed processes, so the code bundled by bundleRenderer, when run inside of a test, doesn't get the VUE_ENV variable that was assigned in another process during test setup. The "normal node" code, on the other hand, runs the renderer in the same process, so it can access the env var that My change to the webpack config will inject the env variable as a static string into the server bundle, so it doesn't really access Bur for example, your jest test runs fine when setting the env variable externally:
|
I needed Eventually I've found a solution which might be interesting for others, therefore I'll share it here: I created a function which hotswaps a Vue instance's whole prototype and replaces it with a proxy of itself which returns function enableSsr(vm) {
Object.setPrototypeOf(
vm,
new Proxy(Object.getPrototypeOf(vm), {
get: (target, key, receiver) => key === '$isServer'
? true
: Reflect.get(target, key, receiver)
})
)
} You can use it like this: const vm = new Vue(...)
enableSsr(vm) If you have to test mixins or similar stuff that relies on new Vue({
beforeCreate() {
enableSsr(this)
},
// ...
}) Hope this helps. 🙂 |
Version
2.5.21
Reproduction link
https://github.com/futureaus/servue/tree/min-rep
Steps to reproduce
npm install --dev
jest
What is expected?
.vue file to render correctly
What is actually happening?
This only happens when using Jest, to test in a normal node environment use
node tests/normal.js
Jest, or an error with the environment detection of Vue is causing
global.process.env.VUE_ENV
to returnundefined
instead of'server'
.Jest is running in
"testEnvironment" : "node"
The $isServer is being used in
lib/imports/headify.js
The text was updated successfully, but these errors were encountered: