Giter VIP home page Giter VIP logo

Comments (6)

AriPerkkio avatar AriPerkkio commented on July 17, 2024

However, Vitest does not currently support the --env-file flag, meaning developers who upgrade to Node.js 20 cannot take advantage of this new feature when running their tests.

If it's a Node CLI flag, it should be supported as usual. Try passing that flag to Node when running Vitest:

- tsx --env-file=.env run-e2e.ts
+ node --env-file=.env ./node_modules/vitest/vitest.mjs

If you wish to pass it only to test runners and not have it available in main thread (vitest.config.ts, global setup), pass it in configuration:

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    poolOptions: {
      threads: { execArgv: ["--env-file=.env"] },

      // Or another pool:
      forks: { execArgv: ["--env-file=.env"] },
    },
  },
});

from vitest.

fellipeutaka avatar fellipeutaka commented on July 17, 2024

However, Vitest does not currently support the --env-file flag, meaning developers who upgrade to Node.js 20 cannot take advantage of this new feature when running their tests.

If it's a Node CLI flag, it should be supported as usual. Try passing that flag to Node when running Vitest:

- tsx --env-file=.env run-e2e.ts
+ node --env-file=.env ./node_modules/vitest/vitest.mjs

If you wish to pass it only to test runners and not have it available in main thread (vitest.config.ts, global setup), pass it in configuration:

import { defineConfig } from "vitest/config";

export default defineConfig({
  test: {
    poolOptions: {
      threads: { execArgv: ["--env-file=.env"] },

      // Or another pool:
      forks: { execArgv: ["--env-file=.env"] },
    },
  },
});

Thanks! I just tested, but only the first solution using node --env-file=.env ./node_modules/vitest/vitest.mjs worked. But I keep wondering why the Vitest CLI doesn't accept this Node.js flag if tsx does.

from vitest.

sheremet-va avatar sheremet-va commented on July 17, 2024

tsx forwards all options to node because it's basically a Node wrapper. Vitest is a standalone application, so we accept only relevant options.

This lack of support necessitates the continued use of third-party packages for managing environment variables, which undermines the benefits provided by the new Node.js 20 feature.

This is not true because you can use loadEnv from vite which always comes with Vitest: https://vitejs.dev/guide/api-javascript.html#loadenv

Vitest also loads env automatically if you follow the convention: https://vitejs.dev/guide/env-and-mode.html#env-variables-and-modes

from vitest.

fellipeutaka avatar fellipeutaka commented on July 17, 2024

Vitest also loads env automatically if you follow the convention: https://vitejs.dev/guide/env-and-mode.html#env-variables-and-modes

In my case, I'm creating a Fastify app, so when I try to access import.meta.env, I get the following error:
Property 'env' does not exist on type 'ImportMeta'.
Even if I declare a new global type like this:

declare global {
  interface ImportMeta {
    readonly env: z.infer<typeof envSchema>;
  }
}

I still can't use import.meta.env because it return undefined

from vitest.

sheremet-va avatar sheremet-va commented on July 17, 2024

I still can't use import.meta.env because it return undefined

Vitest supports process.env, it is equal to import.meta.env in tests

In any way, you can use loadEnv from vite already without any additional packages: https://vitejs.dev/guide/api-javascript.html#loadenv

from vitest.

fellipeutaka avatar fellipeutaka commented on July 17, 2024

Vitest supports process.env, it is equal to import.meta.env in tests

In any way, you can use loadEnv from vite already without any additional packages: https://vitejs.dev/guide/api-javascript.html#loadenv

Great! So I'm gonna use loadEnv from vite. Thank you!

from vitest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.