Skip to content

testing $state, $derived, and $effect.root does not behave as expected #17149

@win-chen

Description

@win-chen

Describe the bug

Hello, in my attempt to recreate the tests in https://svelte.dev/docs/svelte/testing, I've run into test cases that do not behave as expected. Here is the test file with two tests "runes" and "effect.root":

import { flushSync } from "svelte";
import { describe, expect, it } from "vitest";

describe("svelte", () => {
  it("runes", () => {
    let state = $state("hello");
    let der = $derived(state.length);

    state = "bye";
    flushSync();

    expect(der).toBe(3);
  });

  it("effect.root", () => {
    const cleanup = $effect.root(() => {
      expect(true).toBe(false);
    });
    cleanup();
  });
});
  1. "runes"
    Expected behavior: passes with der = 3.
    Actual behavior: fails with der = 5 instead of 3.

  2. "effect.root"
    Expected behavior: fails due to true !== false
    Actual behavior: fails with "expected any number of assertion, but got none"

I believe the tests are configured correctly because non-svelte tests using $state would fail on $state not found.
Please lmk if this is actually configuration error or if expectations are incorrect.
Tests ran with svelte version 5.40.0, 5.41.0, and 5.43.0 - same behaviors.

Reproduction

Repo with the reproduction:
https://github.com/win-chen/svelte-test-bug

Run tests:

npm install
npm run test

View "src/test.svelte.spec.ts" for the tests in question. There is a third test in this file which is just the copy of the "Effect" test in the docs https://svelte.dev/docs/svelte/testing

Logs

System Info

System:
    OS: macOS 14.6.1
    CPU: (8) arm64 Apple M2
    Memory: 79.09 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.14.0 - /Users/winchen/.nvm/versions/node/v22.14.0/bin/node
    Yarn: 1.22.22 - /Users/winchen/.nvm/versions/node/v22.14.0/bin/yarn
    npm: 10.9.2 - /Users/winchen/.nvm/versions/node/v22.14.0/bin/npm
    pnpm: 10.15.0 - /Users/winchen/.nvm/versions/node/v22.14.0/bin/pnpm
  Browsers:
    Chrome: 142.0.7444.135
    Safari: 17.6
  npmPackages:
    svelte: ^5.41.0 => 5.43.6

Severity

annoyance

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