Skip to content

Bug: document calls should fail during ssr test #2020

@webistomin

Description

@webistomin

Describe the bug
Correct me if I'm wrong, but during ssr any access to document should fail. I want to test my component in a node environment, but vue test utils throws an error.

It works if I change environment to JSDOM, but then my component will not throw an error.

ReferenceError: document is not defined ❯ createInstance ../../../node_modules/.pnpm/@vue+test-utils@2.3.1/node_modules/@vue/test-utils/dist/vue-test-utils.esm-bundler.mjs:8020:14 ❯ Module.renderToString ../../../node_modules/.pnpm/@vue+test-utils@2.3.1/node_modules/@vue/test-utils/dist/vue-test-utils.esm-bundler.mjs:8281:15 

I think we shouldn't use document inside createInstance function

https://github.com/vuejs/test-utils/blob/main/src/createInstance.ts#L114
https://github.com/vuejs/test-utils/blob/main/src/createInstance.ts#L119

To Reproduce
https://stackblitz.com/edit/vitest-dev-vitest-nnsmqh?initialPath=__vitest__

Expected behavior
Tests work in node environment

Related information:

System: OS: Windows 10 10.0.19044 CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11700KF @ 3.60GHz Memory: 3.58 GB / 15.83 GB npmPackages: vitest: ^0.29.2 => 0.29.2 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions