Skip to content

Svelte 5: vitest + jsdom error: mount(...) is not available on the server #11394

@ChqThomas

Description

@ChqThomas

Describe the bug

When testing a component in the jsdom environment using Vitest, you get the following error: mount(...) is not available on the server. This error occurs when you try to instantiate a component using the new mount() function.

The issue is the same with happy-dom.

Reproduction

Simple reproduction available here: Stackblitz

Versions:

Example:

// @vitest-environment jsdom

import { expect, test } from 'vitest';
import Hello from '../src/Hello.svelte';
import { mount } from 'svelte';

test('instantiate component', () => {
  const target = document.createElement('div');
  document.body.appendChild(target);

  mount(Hello, {
    target,
  });

  expect(target.innerHTML).toContain('Hello');
});
 FAIL  test/mount.test.ts > instantiate component
Svelte error: lifecycle_function_unavailable
`mount(...)` is not available on the server
 ❯ Module.lifecycle_function_unavailable node_modules/svelte/src/internal/server/errors.js:9:16
      7|  */
      8| export function lifecycle_function_unavailable(name) {
      9|  const error = new Error(`${"lifecycle_function_unavailable"}\n${`\`${name}(...)\` is not available on the server`}`);
       |                ^
     10| 
     11|  error.name = 'Svelte error';
 ❯ Module.mount node_modules/svelte/src/index-server.js:29:24
 ❯ eval test/mount.test.ts:11:3

Logs

No response

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
    CPU: (24) x64 13th Gen Intel(R) Core(TM) i7-13700K
    Memory: 12.33 GB / 15.49 GB
    Container: Yes
    Shell: 3.6.4 - /usr/bin/fish
  Binaries:
    Node: 20.11.1 - /usr/bin/node
    Yarn: 1.22.19 - /usr/bin/yarn
    npm: 10.2.4 - /usr/bin/npm
    bun: 1.0.30 - ~/.bun/bin/bun
  npmPackages:
    rollup: ^3.7.0 => 3.20.0

Severity

blocking an upgrade

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions