Registry / testing / env-test

env-test

JSON →
library1.0.0jsnpmunverified

The `env-test` package is a small, specialized utility designed to set the `NODE_ENV` environment variable to `'test'` specifically when running Mocha tests. Its primary use case is through Mocha's `--require` flag, often configured in `mocha.opts`, to ensure that test environments consistently have `process.env.NODE_ENV = 'test'` without needing to add this line to every test file or rely on shell commands that might be skipped. Released as `1.0.0`, this package has seen no updates in over nine years, indicating it is an abandoned project. While it still functions for its narrow purpose, modern Node.js projects typically utilize more comprehensive environment management solutions like `dotenv`, `cross-env`, or `env-cmd` for loading `.env` files and conditionally setting environment variables across different platforms and contexts.

npm install env-test
INSTALL
IMPORT
SIG · ENV-TEST
E
env-test
testingjavascriptv1.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Side-effect require
mocha --require env-test
import 'env-test';
This package is intended to be required by a test runner like Mocha for its side-effects (setting process.env.NODE_ENV). It does not export any symbols for direct import into JavaScript/TypeScript files. Attempting to use an ES module 'import' will generally not work as intended for this package's core function as a runtime hook.
No direct import/require for symbols
/* no direct code import needed, used via CLI */
const envTest = require('env-test');
The package's function is purely side-effect based (modifying process.env). There are no exported functions, classes, or objects to be imported or required programmatically. Its utility is in being loaded by `--require`.

This quickstart demonstrates how to set up `env-test` with Mocha using `mocha.opts` to automatically ensure `NODE_ENV` is set to 'test' for your test runs.

{ "name": "my-project", "devDependencies": { "mocha": "^10.0.0", "env-test": "^1.0.0" }, "scripts": { "test": "mocha" } } // 1. Install the package // npm install --save-dev env-test mocha // 2. Create or edit test/mocha.opts // --require env-test // --recursive // --timeout 5000 // --ui bdd // 3. Create a test file (e.g., test/example.test.js) const assert = require('assert'); describe('Environment Test', function() { it('should have NODE_ENV set to \'test\'', function() { assert.strictEqual(process.env.NODE_ENV, 'test', 'NODE_ENV should be \'test\'.'); }); it('should demonstrate a basic test', function() { const a = 1; const b = 2; assert.strictEqual(a + b, 3, '1 + 2 should equal 3'); }); }); // 4. Run tests from your terminal: npm test // NODE_ENV will be automatically set to 'test' by env-test before tests run.
Debug
Known issues
deprecatedThe `env-test` package is abandoned and has not been updated in over nine years. While it may still function, it is not actively maintained and may not be compatible with future Node.js versions or complex modern build pipelines. Consider more actively maintained alternatives.
fix
Migrate to `cross-env` for setting environment variables in `package.json` scripts (`cross-env NODE_ENV=test mocha`) or `dotenv` for loading `.env` files in your test setup if more complex environment management is needed.
affects: >=1.0.0
gotchaThis package specifically sets `NODE_ENV` to 'test'. If you need other environment variables or more dynamic environment configuration, `env-test` is insufficient. Its scope is very narrow.
fix
For more flexible environment variable management (e.g., loading from `.env` files, setting multiple variables, or cross-platform compatibility), use packages like `dotenv` (programmatically) or `env-cmd` / `cross-env` (via npm scripts).
affects: >=1.0.0
gotchaUsing `--require env-test` sets `NODE_ENV` early in the process lifecycle. If other scripts or configuration files attempt to set `NODE_ENV` later or unconditionally, it might lead to unexpected overwrites or conflicts.
fix
Ensure that `env-test` is the sole or primary source for setting `NODE_ENV` to 'test' in your testing workflow. Review your `package.json` scripts and other test setup files for conflicting environment variable assignments.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Cannot find module 'env-test'
`env-test` is not installed or not found in `node_modules`.
fix
Ensure `env-test` is installed as a dev dependency: `npm install --save-dev env-test`.
mocha --require env-test` does not set NODE_ENV
The `mocha.opts` file is not being loaded, or the `--require` flag is incorrectly specified, or another process is overwriting `NODE_ENV`.
fix
Verify that your `mocha.opts` file is in the correct location (`test/mocha.opts` by default, or specified with `--opts <path>`). Double-check the `--require env-test` line for typos. Add `console.log(process.env.NODE_ENV);` to your test setup to debug when `NODE_ENV` is being set.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
env-test — npm install env-test · libregistry