kit: Unable to perform Vitest integrations tests using $env/dynamic/private

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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 15 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I can confirm I am also having this issue in my .test.ts files running through vitest.

Funny thing is that the file im testing (user.ts - that exports a bunch of functions that do simple CRUD operations related to users in db) actually imports my prisma instance from $lib/server/db.ts and it works just fine. It’s only when I import a file with .svelte extension it actually throws.

Luckily in my case there is a workaround - I can move the component files out of the server directory.

When it comes to importing from $env/static/private I replaced it in my setupTests.ts file with:

vi.mock('$env/static/private', () => import.meta.env);

Hope that helps

@bartektelec lead me in the right direction. Posting this for others. We ended up using the described methods with setupTests.ts, however we intercepted the preferred SvelteKit environment variables.

import { vi } from 'vitest';

vi.mock('$env/dynamic/public', () => {
  return {
    env: {
      PUBLIC_NODE_ENV: 'test',
    }
  };
});

vi.mock('$env/dynamic/private', () => {
  return {
    env: {
      SUPER_SECRET_KEY: 'test',
    }
  };
});

I can confirm I am also having this issue in my .test.ts files running through vitest.

Funny thing is that the file im testing (user.ts - that exports a bunch of functions that do simple CRUD operations related to users in db) actually imports my prisma instance from $lib/server/db.ts and it works just fine. It’s only when I import a file with .svelte extension it actually throws.

Luckily in my case there is a workaround - I can move the component files out of the server directory.

When it comes to importing from $env/static/private I replaced it in my setupTests.ts file with:

vi.mock('$env/static/private', () => import.meta.env);

Hope that helps

for anyone who landed here struggling with running generic typescript scripts locally:

TEST=true pnpx vite-node src/scripts/foo.ts

this did the trick for me. Setting TEST=true disables the Cannot import $env/dynamic/private into client-side code error locally.

Mhhhm, anything that ends with .server.js/ts is treated as “this code is safe”. I guess we need to make the detection algorithm more sophisticated to also work with test files.