Yargs Configuration Extends Test Module
raw JSON →The `yargs-test-extends` package is a specialized internal fixture designed for unit testing the `.config()` and `extends` functionality within the `yargs` command-line argument parsing library. It is not intended for general-purpose use in consumer applications, but rather serves as a static configuration source to verify `yargs`'s dynamic configuration loading capabilities. This package currently maintains version `1.0.1`. Its primary role is to demonstrate and verify that `yargs` can load configuration from an external Node.js module by inspecting its `main` entry point, which points to a static `index.json` file containing predefined configuration values. This mechanism tests `yargs`'s ability to extend configuration dynamically from installed packages rather than solely from local JSON files, ensuring robust configuration management within the `yargs` ecosystem.
Common errors
error Unexpected object received when requiring 'yargs-test-extends' directly. ↓
require('yargs-test-extends') or import config from 'yargs-test-extends' provides the raw JSON content. For yargs to process it as an extension, use yargs().config({ extends: 'yargs-test-extends' }). error Module not found: Can't resolve 'yargs-test-extends' ↓
yargs-test-extends is listed in your project's dependencies or devDependencies (if used only for testing) and installed via npm install yargs-test-extends or yarn add yargs-test-extends. Warnings
gotcha This package (`yargs-test-extends`) is a test fixture for the 'yargs' library and is not intended for general use in consumer applications. It serves a specific role in `yargs`'s internal testing suite. ↓
gotcha Attempting to import or require 'yargs-test-extends' directly will return the raw JSON object defined in its `index.json` file, not a processed `yargs` configuration object or functionality. Its `package.json` specifies `main: 'index.json'`. ↓
Install
npm install yargs-test-extends yarn add yargs-test-extends pnpm add yargs-test-extends Imports
- yargs-test-extends (as config extension string)
yargs().config({ extends: 'yargs-test-extends' }) - config (CommonJS direct require)
const config = require('yargs-test-extends'); - config (ESM direct import) wrong
import { c } from 'yargs-test-extends';correctimport config from 'yargs-test-extends';
Quickstart
const yargs = require('yargs');
const assert = require('assert');
// This example demonstrates how yargs loads configuration
// from the 'yargs-test-extends' module via its 'extends' mechanism.
// The 'yargs-test-extends' package's package.json specifies 'index.json' as its main.
// The index.json contains { "c": 201 }.
// The initial config sets 'a' to 2.
const argv = yargs()
.config({
a: 2,
extends: 'yargs-test-extends' // Yargs resolves this module's main file
})
.argv;
// Verify that the initial config 'a' is preserved.
assert.strictEqual(argv.a, 2, 'Value for `a` should be 2 from direct config.');
// Verify that 'c' is loaded from the extended module.
assert.strictEqual(argv.c, 201, 'Value for `c` should be 201 from extended module.');
console.log('Yargs successfully extended configuration from yargs-test-extends.');
console.log('Parsed argv:', argv);