Skip to content

Ability to "unset" an environment variable with stubEnv (or a new function) #5787

@dietrich-iti

Description

@dietrich-iti

Clear and concise description of the problem

As a developer using Vitest, I want to be able to use the environment variable stubbing feature to make a given variable be "unset", so that I can easily test my code's behavior when environment variables are not present.

Suggested solution

Expand the functionality of vi.stubEnv(name: string, value: string), changing the second argument to value: string | undefined. If undefined is given as the value, remove the named variable from process.env.

Alternative

Alternatively, a new function like vi.stubEnvUnset(name: string) that does the same thing.

Additional context

My current workaround is to use beforeAll to store the process.env entries in question and then delete them, and use afterAll to restore them.

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions