Registry / testing / test-as-package

test-as-package

JSON →
library2.0.4jsnpmunverified

test-as-package is a JavaScript/TypeScript utility designed for testing a local repository as if it were an already deployed npm package. This is particularly useful for internal monorepos or libraries where verifying the consumer-facing behavior, including CLI commands and package exports, is crucial without the need for actual publishing. The current stable version is 2.0.4, with patch releases occurring regularly for dependency updates and minor fixes; major versions are less frequent but typically introduce significant changes. A key differentiator is its ability to pack and install the local package into a temporary Node.js `node_modules` environment without modifying the project's `package.json` or `package-lock.json`, thus ensuring a clean testing environment that closely mirrors an actual installation. It offers both a convenient CLI interface for direct execution of test commands and a programmatic API for more granular control within test suites.

npm install test-as-package
INSTALL
IMPORT
SIG · TEST-AS-PACKAGE
T
test-as-package
testingjavascriptv2.0.4
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.

runPackageCli
import { runPackageCli } from 'test-as-package';
const { runPackageCli } = require('test-as-package');
The primary API function for programmatic testing of your package's CLI. CommonJS `require` is not supported for this ESM-first package.
CliOutput
import type { CliOutput } from 'test-as-package';
import { CliOutput } from 'test-as-package';
Type definition for the return value of `runPackageCli`, providing details like `exitCode`, `stdout`, and `stderr`. Always import as a type.
RunPackageCliOptions
import type { RunPackageCliOptions } from 'test-as-package';
import { RunPackageCliOptions } from 'test-as-package';
Type definition for the options object passed to `runPackageCli`, such as `commandArgs`. Always import as a type.

Demonstrates how to use `runPackageCli` within a test suite to execute your package's CLI, capture its output, and assert against its exit code, stdout, and stderr. This example assumes `@augment-vir/assert` and `@augment-vir/test` are also installed for testing utilities.

import { assert } from '@augment-vir/assert'; import { describe, it } from '@augment-vir/test'; import { runPackageCli } from 'test-as-package'; describe('my package CLI', () => { it('should execute a command and produce expected output', async () => { // Example: Imagine your package has a CLI that takes arguments and prints to stdout const cliOutputs = await runPackageCli({ commandArgs: ['my-command', 'arg1'], // Arguments to pass to your package's CLI // You can also specify environment variables or a working directory // env: { MY_ENV_VAR: 'some_value' }, // cwd: './my-cli-test-dir', }); // Assert that the command exited successfully (exit code 0) assert.strictEquals(cliOutputs.exitCode, 0); // Optionally, assert against stdout or stderr content assert.include(cliOutputs.stdout, 'my-command executed successfully with arg1'); assert.strictEquals(cliOutputs.stderr, ''); // Assert no errors were printed console.log('CLI exited with code:', cliOutputs.exitCode); console.log('CLI stdout:', cliOutputs.stdout); console.log('CLI stderr:', cliOutputs.stderr); }); });
test-as-package --version
Debug
Known issues
breakingThe package requires Node.js version 22 or higher. Ensure your development and CI/CD environments meet this requirement to avoid runtime errors.
fix
Upgrade your Node.js installation to version 22 or newer. Consider using nvm or a similar tool for managing Node.js versions, such as `nvm install 22 && nvm use 22`.
affects: >=2.0.0
breakingMajor versions (e.g., v1.0.0, v2.0.0) introduce potentially breaking changes to the API or CLI behavior. Detailed migration guides are not provided in the supplied release notes, so users should consult the GitHub releases or source code for specific changes.
fix
Carefully review the package's GitHub releases page and commit history when upgrading between major versions to identify specific breaking changes and necessary code adjustments.
affects: >=1.0.0
gotchaThis package is explicitly designed for testing purposes and should primarily be installed as a dev dependency (`npm i -D test-as-package`). It is not intended for runtime production use in your application's `dependencies`.
fix
Always use `npm install --save-dev test-as-package` or `yarn add --dev test-as-package` when adding this package to your project.
affects: >=0.0.1
gotchaWhen using the `test-as-package` CLI directly from the command line or `npm scripts`, you must pass the actual test command as an argument (e.g., `test-as-package mocha`). Running it without arguments will not execute your tests.
fix
Modify your `npm test` script to include your desired test runner, for example: `"test": "test-as-package jest"` or `"test": "test-as-package ts-node path/to/my-tests.ts"`.
affects: >=0.0.1
gotchaThe `test-as-package` utility operates by packing and installing your local package into a temporary environment. It explicitly does *not* modify your project's `package.json` or `package-lock.json`, which is a feature but might be unexpected if you anticipated these files to reflect the 'installed' state.
fix
Understand that `test-as-package` creates an isolated testing environment. If you need to test actual `package.json` modifications, consider dedicated integration testing against a deployed package or a more elaborate setup.
affects: >=0.0.1
Errors
Common errors & fixes
Error: Command not found: test-as-package
The `test-as-package` CLI executable is not in your system's PATH, or the package is not installed correctly.
fix
Ensure `test-as-package` is installed as a dev dependency (`npm i -D test-as-package`) and that your `npm scripts` or shell environment correctly resolves `node_modules/.bin` commands. If running directly, ensure `$(npm bin)/test-as-package` is used or global installation is performed (not generally recommended).
TypeError: runPackageCli is not a function
This typically occurs when `test-as-package` is imported using CommonJS `require()` syntax, which is not supported, or if there's a typo in the named import.
fix
Ensure your project is configured for ESM and use `import { runPackageCli } from 'test-as-package';`. Verify the symbol name `runPackageCli` is spelled correctly and that it is a named export.
Error: The "test-as-package" package requires Node.js version >=22.
The currently installed Node.js version does not meet the minimum requirement specified by the package's `engines` field.
fix
Update your Node.js environment to version 22 or newer. Use a Node Version Manager (NVM) to switch to or install the required version, e.g., `nvm install 22` and `nvm use 22`.
TypeScript Error: Module '"test-as-package"' has no exported member 'CliOutput'.
Attempting to import a type like `CliOutput` or `RunPackageCliOptions` as a value import instead of a type import, or the symbol name is incorrect.
fix
For type-only imports, ensure you are using `import type { CliOutput } from 'test-as-package';`. Double-check the exact casing and spelling of the type names against the package's type definitions.
Upgrade
Version history
2.0.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
test-as-package — npm install test-as-package · libregistry