Registry / testing / a
library1.0jsnpmunverified

The `a` package provides a versatile mocking framework for JavaScript and TypeScript, designed to be used with any testing framework. It is currently stable at version 4.0.8, with recent updates including the addition of comprehensive TypeScript types in v4.0.7, indicating an active development cadence. Key differentiators include its ability to create both partial mocks (which fall back to the original function) and strict mocks (which throw on unexpected calls), alongside detailed argument expectation, advanced matching for arrays and objects, and control over repeat behaviors. This allows for precise control over test doubles, supporting TDD and BDD methodologies in unit testing scenarios. The framework provides flexible options for stubbing and verification of function calls.

npm install a
INSTALL
IMPORT
SIG · A
A
a
testingjavascriptv1.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.

a
import a from 'a';
import { a } from 'a';
The primary mocking utility is the default export of the 'a' package.
MockedFunction
import type { MockedFunction } from 'a';
import { MockedFunction } from 'a';
To get type information for functions returned by `a.mock()`, import `MockedFunction` as a type.
a (CommonJS)
const a = require('a');
import a from 'a'; // in CommonJS environments
For CommonJS modules, use `require()` to import the library. Direct ESM imports might fail in older Node.js environments without transpilation or `"type": "module"`.

Demonstrates creating strict and partial mocks, expecting arguments, controlling call repeats, and handling unexpected calls.

import a from 'a'; // Example 1: Strict mock with argument expectation and repeat controls const strictMock = a.mock(); strictMock.expect('firstArg').return('First Value').repeat(1); strictMock.expect('secondArg', 123).return('Second Value').repeatAny(); console.log('Strict Mock Call 1 (firstArg):', strictMock('firstArg')); // Should return 'First Value' // The next call with 'firstArg' would throw an error as its repeat(1) is exhausted. console.log('Strict Mock Call 2 (secondArg):', strictMock('secondArg', 123)); // Returns 'Second Value' console.log('Strict Mock Call 3 (secondArg):', strictMock('secondArg', 123)); // Returns 'Second Value' indefinitely try { strictMock('unexpected'); // This will throw an error: 'unexpected arguments' } catch (e: any) { console.error('Strict Mock Error:', e.message); } // Example 2: Partial mock, falling back to original function function originalFunction(input: string) { return `Original says: ${input}`; } const partialMock = a.mock(originalFunction); partialMock.expect('mockedInput').return('Mocked Override!'); console.log('Partial Mock Call 1 (mocked):', partialMock('mockedInput')); // Returns 'Mocked Override!' console.log('Partial Mock Call 2 (not mocked):', partialMock('any other input')); // Calls original: 'Original says: any other input'
Debug
Known issues
gotchaStrict mocks throw an `unexpected arguments` error if called with arguments that do not match any configured expectation, or if all expected calls for a specific argument set have been exhausted (unless `repeatAny()` is used).
fix
Always define expectations for all possible call patterns or use `mock.ignoreAll().return(...)` for a wildcard match, or `repeatAny()` for infinite repeats.
affects: >=1.0.0
gotchaWhen expecting objects or arrays, the `expect()` method performs a strict comparison. For objects, all leaf properties must match exactly. An empty object expectation (`expect({})`) will not match a non-empty actual object.
fix
Ensure that your expected object or array exactly matches the structure and values of the input you anticipate, or use `ignore()` for specific arguments if their value doesn't matter.
affects: >=1.0.0
gotchaThe `repeat(N)` method configures an expectation to be valid for exactly `N` calls. After `N` calls, subsequent matching calls will result in an `unexpected arguments` error, unless `repeatAny()` is used.
fix
If a mock expectation should be valid indefinitely, explicitly chain `.repeatAny()` to it. Otherwise, ensure your test logic does not call the mock more times than expected.
affects: >=1.0.0
Errors
Common errors & fixes
Error: unexpected arguments
A strict mock was called with arguments that do not match any defined expectation, or a repeated expectation was called more times than specified.
fix
Review the mock's `expect()` configurations. Ensure all valid call patterns are defined, or use `repeatAny()` if calls should be infinite. If it's a strict mock, ensure only expected arguments are passed.
TypeError: a.mock is not a function
The `a` object was imported incorrectly, or `a` is `undefined` because of an environment issue (e.g., trying to use ESM `import` in a pure CommonJS file without proper Node.js configuration).
fix
Verify that `a` is imported as the default export (`import a from 'a';` for ESM or `const a = require('a');` for CommonJS). Check your `package.json` for `"type": "module"` if using ESM.
Upgrade
Version history
1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
75 hits · last 30 days
node
64
OpenAI (training)
1
Resources
a — npm install a · libregistry