Skip to content

Replace some integration tests with unit tests to reduce reliance on requireUncached#862

Merged
jdmarshall merged 7 commits intonode-config:masterfrom
jdmarshall:requireUncached
Jan 16, 2026
Merged

Replace some integration tests with unit tests to reduce reliance on requireUncached#862
jdmarshall merged 7 commits intonode-config:masterfrom
jdmarshall:requireUncached

Conversation

@jdmarshall
Copy link
Collaborator

@jdmarshall jdmarshall commented Jan 10, 2026

Work for #860

This changeset fixes a silently broken test for Unicode BOM characters, adds coverage for the optional parameter to fromEnvironment(), and moves a heap of tests still being run on the Config singleton to run on loadFile() or loadFileConfigs() as appropriate. A few of these were already doing this but had been modified in-place.

This PR reduces the number of uses of requireUncached in the tests from 80 to 46. I believe all of the remaining cases are
for functionality that was not migrated to lib/util.js

Summary by CodeRabbit

  • Configuration Changes

    • Modified NODE_CONFIG_DIR handling to no longer automatically convert directory paths to absolute paths; directory values are now used as provided
  • Tests

    • Consolidated and expanded test coverage with comprehensive scenarios for configuration loading, environment variable handling, multiple file formats, and directory resolution

✏️ Tip: You can customize this high-level summary in your review settings.

- 2-config-test.js
- 20-multiple.js
- 22-files-order.js

To reduce use of requireUncached for node-config#860
- 4-array-merging.js
- 7-unicode-situations.js Note: this fixes a test that was silently failing after earlier refactoring
- x-config-js-test-transpiled.js
- x-config-test-ts.js
- x-config-test-ts-module-exports.js

To reduce use of requireUncached for node-config#860
@coderabbitai
Copy link

coderabbitai bot commented Jan 10, 2026

📝 Walkthrough

Walkthrough

This PR consolidates test coverage by removing multiple individual test files and a configuration fixture, relocating their test cases into a comprehensive unified test suite in test/0-util.js. A minor production code change removes automatic absolute path conversion for NODE_CONFIG_DIR in Load.fromEnvironment.

Changes

Cohort / File(s) Summary
Production code
lib/util.js
Removed automatic conversion of NODE_CONFIG_DIR to absolute path in Load.fromEnvironment, allowing the value to remain unchanged at that stage
Consolidated test suite
test/0-util.js
Added comprehensive test coverage consolidating scenarios from 12+ deleted test files, including array merging, Unicode/BOM handling, gitCrypt integration, environment variable loading, NODE_ENV enumeration, app instances, multiple file format parsing, directory resolution, and config precedence
Deleted test files
test/2-config-test.js, test/4-array-merging.js, test/7-unicode-situations.js, test/10-gitcrypt-test.js, test/14-node_env-load-multiple-files.js, test/17-toml.js, test/19-custom-environment-variables.js, test/20-multiple-config.js, test/22-files-order.js, test/x-config-js-test-transpiled.js, test/x-config-test-ts-module-exports.js, test/x-config-test-ts.js
Removed individual test suites covering file format parsing, environment variables, multiple directories, and TypeScript modules; functionality consolidated into test/0-util.js
Deleted test fixtures
test/10-config/default.json, test/19-config/custom-environment-variables.json, test/19-config/default.js
Removed test configuration and fixture files no longer needed after test consolidation

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • markstos

Poem

🐰 Hop along with tests unified and tight,
Scattered files now gathered in one file's light!
From NODE_CONFIG_DIR paths absolute freed,
A cleaner test home is all that we need!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: moving integration tests that relied on requireUncached to unit tests using loadFile/loadFileConfigs APIs, reducing requireUncached usage from 80 to 46.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

}

let configDir = env.initParam('NODE_CONFIG_DIR');
configDir = configDir && _toAbsolutePath(configDir);
Copy link
Collaborator Author

@jdmarshall jdmarshall Jan 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is 1) incorrect because this can be a colon-separated value, and 2) duplicates work that is repeated in the next phase of the load operation anyway, after the paths have been split for iterating.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In @test/0-util.js:
- Around line 1246-1266: The tests expect util.loadFileConfigs to treat
appInstance values (including numeric 3) and to load JSON files before YAML so
YAML can override JSON; update the load logic in util.loadFileConfigs (and any
helper that builds filenames or iterates extensions) to coerce appInstance to
string when constructing instance-specific filenames and to enforce an extension
load order that loads .json (and .js) before .yml/.yaml so YAML overrides take
effect; ensure the code path that merges configs preserves later-file precedence
so values from YAML replace earlier JSON values.
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 848cde5 and d119202.

📒 Files selected for processing (26)
  • lib/util.js
  • test/0-util.js
  • test/10-config/default.json
  • test/10-gitcrypt-test.js
  • test/14-node_env-load-multiple-files.js
  • test/17-toml.js
  • test/19-config/custom-environment-variables.json
  • test/19-config/default.js
  • test/19-custom-environment-variables.js
  • test/2-config-test.js
  • test/20-multiple-config.js
  • test/22-files-order.js
  • test/4-array-merging.js
  • test/7-unicode-situations.js
  • test/config/bare-metal.json
  • test/config/cloud.json
  • test/config/development.json
  • test/config/encrypted.json
  • test/config/git-crypt-symmetric-key
  • test/config/local-cloud.json
  • test/config/local-development.json
  • test/config/test-bare-metal.json
  • test/config/test-development.json
  • test/x-config-js-test-transpiled.js
  • test/x-config-test-ts-module-exports.js
  • test/x-config-test-ts.js
💤 Files with no reviewable changes (16)
  • test/14-node_env-load-multiple-files.js
  • test/19-config/default.js
  • test/10-gitcrypt-test.js
  • lib/util.js
  • test/2-config-test.js
  • test/22-files-order.js
  • test/10-config/default.json
  • test/x-config-test-ts-module-exports.js
  • test/20-multiple-config.js
  • test/19-custom-environment-variables.js
  • test/x-config-test-ts.js
  • test/x-config-js-test-transpiled.js
  • test/7-unicode-situations.js
  • test/4-array-merging.js
  • test/17-toml.js
  • test/19-config/custom-environment-variables.json
🔇 Additional comments (9)
test/0-util.js (9)

373-393: Array replacement tests in extendDeep are well-designed.

The new tests properly verify that arrays are replaced wholesale (not merged element-by-element) and that both empty-to-full and full-to-empty replacements work correctly. The assertions confirm the result is correct and that the original object remains unmodified.


795-805: Test verifies fromEnvironment ignores process.env when argument provided.

This test correctly validates the new behavior: when an explicit argument is passed to Load.fromEnvironment(), it should use that value instead of reading from NODE_ENV. The test properly cleans up via try/finally.


807-819: Multiple nodeEnv enumeration test is correct.

Test verifies that comma-separated NODE_ENV values like 'mercury,apollo' are properly split into an array ['mercury', 'apollo']. The structure and assertions are sound.


1268-1293: Environment and local file loading tests are comprehensive.

Tests verify that nodeEnv-specific files (e.g., config/test.*), local files, and local-env combinations load correctly with expected merging. Assertions target specific fields that should come from different file sources, validating proper precedence.


1442-1483: Multiple nodeEnv tests validate enumeration, local variants, and left-right load order.

Tests verify that multiple nodeEnv values (e.g., ['development', 'cloud']) cause corresponding files to load in order, that local variants are included, and that values are merged with correct precedence (right-most wins, validated at line 1474-1482). The hostname-specific loading test (line 1463-1472) verifies that env-hostname combinations are resolved.


1485-1523: Multiple config directories tests validate path handling and merge order.

Tests verify that configs from multiple directories (delimited by Path.delimiter) are loaded and merged correctly, that both absolute and relative paths work together, and that empty entries don't cause errors. Assertions confirm values come from the correct directory and that resolution order is correct.


725-748: Encrypted file tests validate both success and error paths.

Tests verify that encrypted files return null when gitCrypt is disabled (lines 726-735) and throw an appropriate error when gitCrypt is not configured (lines 737-747). The regex match /Cannot parse config file/ is appropriately loose to avoid brittleness.


1295-1439: Filetypes coverage is extensive and validates format-specific parsing.

The new "filetypes" describe block tests loading from JS, JSON, JSONC, JSON5, CSON, YAML (.yaml/.yml), CoffeeScript, transpiled ESM, Hjson, XML, MJS, TypeScript, TOML, and .properties files. Each test loads a config and asserts a format-specific value exists with the correct data, validating that parsers work correctly.

Notable: The nested "for .ts files" describe block includes defensive testing for TypeScript—verifying that an existing .ts handler is not overwritten, which is good practice when dynamically registering require extensions.


703-707: BOM Unicode test added; fixture file verified.

The test correctly uses doesNotThrow to verify that config files with UTF-8 BOM prefixes parse without error. The fixture file at test/7-config/defaultWithUnicodeBOM.json exists and contains a UTF-8 BOM marker as expected.

@jdmarshall jdmarshall modified the milestones: 4.2, 4.3 Jan 10, 2026
@jdmarshall jdmarshall merged commit 0c3cfd6 into node-config:master Jan 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant