vm.$isServer returns false when using Jest in testEnvironment Node
See original GitHub issueVersion
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?
× renders file correctly (2710ms)
● renders file correctly
ReferenceError: head is not defined
at Object.eval (eval at <anonymous> (node_modules/lodash.template/index.js:1089:12), <anonymous>:8:11)
at TemplateRenderer.renderSync (node_modules/vue-server-renderer/build.js:8020:16)
at RenderContext.done (node_modules/vue-server-renderer/build.js:8264:39)
at RenderContext.next (node_modules/vue-server-renderer/build.js:2459:19)
at RenderContext.cachedWrite [as write] (node_modules/vue-server-renderer/build.js:2323:9)
at RenderContext.next (node_modules/vue-server-renderer/build.js:2473:25)
at cachedWrite (node_modules/vue-server-renderer/build.js:2323:9)
at renderStringNode$1 (node_modules/vue-server-renderer/build.js:7599:5)
at RenderContext.renderNode (node_modules/vue-server-renderer/build.js:7402:5)
at RenderContext.next (node_modules/vue-server-renderer/build.js:2469:23)
at cachedWrite (node_modules/vue-server-renderer/build.js:2323:9)
at renderElement (node_modules/vue-server-renderer/build.js:7642:5)
at renderNode (node_modules/vue-server-renderer/build.js:7406:5)
at renderComponentInner (node_modules/vue-server-renderer/build.js:7524:3)
at renderComponent (node_modules/vue-server-renderer/build.js:7488:5)
at renderNode (node_modules/vue-server-renderer/build.js:7404:5)
undefined
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 4.839s, estimated 7s
Ran all test suites.
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 return undefined
instead of 'server'
.
Jest is running in "testEnvironment" : "node"
The $isServer is being used in lib/imports/headify.js
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Configuring Jest
This option provides the default configuration of fake timers for all tests. Calling jest.useFakeTimers() in a test file will use these ...
Read more >Web Worker - Jest - Cannot use 'import.meta' outside a module
In my setup (typescript + ts-jest) I prepended the following node option to make it work: NODE_OPTIONS=--experimental-vm-modules.
Read more >isServer is false when called on server - Unity Forum
Hi all, I'm having an odd scenario with Mirror / Unet where the isServer boolean is returning false when it's called from the...
Read more >Open Source Used In webexdevhub 1.0.0 - Cisco
This document contains licenses and notices for open source software used in this product. With respect to the free/open source software listed in...
Read more >Technology Archives - N47
Always use EventListener to be on safe side. It is fired on repository level. If you decide to use EventHandler because of simplicity,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I think the issue is with docs, mainly., but the general ones for SSR, nothing jest-specific.
vue-server-renderer does set
process.env.VUE_ENV = server
. But you don’t pass that environment variable into your app bundle, so inside of our bundle, it’s not set, and consequently,$isServer
returns false.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…
I needed
$isServer
to be true just for one individual Vue instance in my tests. Temporarily overridingVue.prototype.$isServer
was not an option since it is not writable.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
true
when asked for$isServer
:You can use it like this:
If you have to test mixins or similar stuff that relies on
$isServer
, you may need to run that function pretty early, in abeforeCreate
hook:Hope this helps. 🙂