[Feature Request] Override ENV values in tests
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Quiet
- Tech stack
- bun, typescript
- Domain
- testing
Research direction
Start by reading the varlock/env initialization and generated schema type entry points, then inspect existing test utilities and environment coercion, validation, and redaction paths. The issue proposes an override helper but does not identify implementation files or tests, so first trace how ENV is resolved and how test mode is determined. Done means a typed helper can apply validated overrides, restore prior values, enforce the test-only or opt-in restriction, and register sensitive values for redaction.
Written by the indexing model from the issue text.
Description
Description
A supported way to override ENV values in tests. ENV from varlock/env is read-only, so a test that needs one flag flipped for one case has to replace the whole module.
Motivation
Say an app has a boolean flag FEATURE_X_DISABLED. To test both branches with bun test, the test has to replace the module:
import * as actual from "varlock/env";
let featureXDisabled = false;
mock.module("varlock/env", () => ({
...actual,
ENV: new Proxy(actual.ENV, {
get: (target, key) => (key === "FEATURE_X_DISABLED" ? featureXDisabled : Reflect.get(target, key)),
}),
}));
This depends on the test runner's module mocking, has to be set up before the code under test is imported, and bypasses coercion and validation for the overridden value.
Proposed Solution
A test helper in varlock/env (or varlock/testing), for example:
import { overrideEnv } from "varlock/env";
const restore = overrideEnv({ FEATURE_X_DISABLED: true });
// ...
restore();
- Typed against the generated schema types.
- Values are coerced and validated like loaded ones (so
"true"becomestrue, and an invalid value fails loudly). - Only allowed when the resolved environment is
test(or behind an explicit opt-in), so it can't flip config in production. - Sensitive overrides are registered for redaction like loaded values.
Alternatives
- Module mocking as above.
- Setting
process.envbefore import and re-resolving; slow, and does not work onceENVis initialised.
Additional Information
Found while moving a Bun monorepo's hand-written zod env parsing (which was mutable) to varlock.
- Dominant language
- TypeScript
- Stars
- 4.9k
- Forks
- 140
- Avg merge
- 2d 3h
- Merged PRs (30d)
- 35
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dmno-dev/varlock
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
dmno-dev/varlock#1279 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
dmno-dev/varlock#1100 · 3 comments ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 78/100
dmno-dev/varlock#1278 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
dmno-dev/varlock#1275 · 1 comment ·
Maintainers usually reply within 1 day
-
core:varlock RFC
Difficulty 5/5 Over a week Newbie friendliness 28/100
dmno-dev/varlock#1258 · 1 comment ·
Maintainers usually reply within 1 day
All issues in dmno-dev/varlock
Similar issues
-
documentation good first issue roadmap
Difficulty 1/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
resend/react-email#3876 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
siyuan-note/siyuan#21324 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
himynameisdave/bsky-profanity-counter#59 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100