Skip to content

Unable to perform Vitest integrations tests using $env/dynamic/private  #8180

@kyllerss

Description

@kyllerss

Describe the bug

I finally got around to migrating away from import.meta.env.* for all of my environment variables and onto the SvelteKit-recommended $end/dynamic|static/public|private. I had a number of integration tests I had written in vitest that are now breaking with the following error:

FAIL  tests/integration/hooks.test.ts [ tests/integration/hooks.test.ts ]
Error: Cannot import $env/dynamic/private into client-side code
 ❯ Context.load node_modules/@sveltejs/kit/src/exports/vite/index.js:304:12
 ❯ Object.load node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:40201:46
 ❯ loadAndTransform node_modules/vite/dist/node/chunks/dep-67e7f8ab.js:36553:24

My integration tests import a number of back-end modules (eg. database access code, etc...).

I am surprised by the fact that the integration tests are being treated as client code that is tripping up the server-client serialization safety mechanism.

I am at a loss as to what to do here. Any recommendations on how to address this issue? Is there any way I can turn off this safety mechanism for my integration tests? Is there something entirely different I should be doing to do database integration tests?

PS. My reproduction project is using the stock configuration that comes from a new project. It calls the vitest tests unit tests. I am not sure if I should be reading too much into that. If vitest is not the right place for writing these types of tests, what alternatives should I look into?

Reproduction

The reproduction test illustrates the problem when importing a class that makes a reference to $env/dynamic/private.

https://github.com/kyllerss/sveltekit-issue-8180

Logs

No response

System Info

System:
    OS: Linux 5.15 KDE neon 5.26 5.26
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
    Memory: 9.68 GB / 15.34 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 16.18.0 - ~/apps/node-v16.18.0-linux-x64/bin/node
    npm: 8.19.2 - ~/apps/node-v16.18.0-linux-x64/bin/npm
  Browsers:
    Brave Browser: 108.1.46.144
    Chrome: 108.0.5359.124
    Firefox: 108.0
  npmPackages:
    @sveltejs/adapter-auto: ^1.0.0 => 1.0.0 
    @sveltejs/kit: ^1.0.0 => 1.0.0 
    svelte: ^3.54.0 => 3.55.0 
    vite: ^4.0.0 => 4.0.1

Severity

serious, but I can work around it

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingp1-importantSvelteKit cannot be used by a large number of people, basic functionality is missing, etc.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions