Skip to content

Vitest doesn't read env variables on test #33

@IT-WIBRC

Description

@IT-WIBRC

Need to add this line on the test property on the vitest.config.json:

import { loadEnv } from 'vite'
import { defineConfig } from 'vitest/config'

export default defineConfig(({ mode }) => ({
  test: {
    // mode defines what ".env.{mode}" file to choose if exists
    env: loadEnv(mode, process.cwd(), ''),
  },
}))

You can create a file .env.test with all your environment variables set for the test cases.

NB: Add a piece of advice to use vi.stubEnv from vi-stubenv to stub env variables for tests and don't forget to clear it with the vi.unstubAllEnvs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions