Yargs Configuration Extends Test Module

raw JSON →
1.0.1 verified Sat Apr 25 auth: no javascript

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.

error Unexpected object received when requiring 'yargs-test-extends' directly.
cause The package's `main` entry points to `index.json`, causing `require()` or `import` to return a plain JavaScript object (parsed JSON), not a module exporting functions or a default configuration object interpreted by `yargs`.
fix
Understand that direct 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'
cause The `yargs-test-extends` package has not been installed, but `yargs`'s `.config({ extends: 'yargs-test-extends' })` is attempting to resolve it.
fix
Ensure 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.
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.
fix Avoid using 'yargs-test-extends' in production code. For dynamic configuration in your own `yargs` applications, create your own module that exports a configuration object or points to a custom JSON file.
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'`.
fix If you need to inspect the raw configuration, direct import/require is functional. However, if you intend for `yargs` to interpret it as an extension, you must use `yargs().config({ extends: 'yargs-test-extends' })`.
npm install yargs-test-extends
yarn add yargs-test-extends
pnpm add yargs-test-extends

Demonstrates `yargs` loading configuration from an external Node.js module specified by `extends`.

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);