Registry / testing / tcompare

tcompare

JSON →
library9.3.1jsnpmunverified

tcompare is a JavaScript/TypeScript library designed for deep object comparison, primarily intended for use within test frameworks. It provides a suite of comparison functions with varying levels of strictness, such as `match`, `same`, `strict`, `has`, and `matchOnly`, allowing developers to precisely define how objects should be evaluated against patterns. Beyond a simple boolean result, it generates human-readable diff strings and patch-style diffs, highlighting differences between actual and expected values. The library currently stands at version 9.3.1 and appears to maintain a release cadence in alignment with its parent project, `tap`, suggesting active development and maintenance. Its key differentiators include fine-grained control over comparison logic and robust diff generation capabilities, making it valuable for detailed assertion failure reporting.

npm install tcompare
INSTALL
IMPORT
SIG · TCOMPARE
T
tcompare
testingjavascriptv9.3.1
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.

match
import { match } from 'tcompare'
const { match } = require('tcompare')
While CommonJS `require` is supported, ES Modules are preferred for modern Node.js development and TypeScript.
Result
import type { Result } from 'tcompare'
import { Result } from 'tcompare'
Result is a type definition, not a runtime value. Use `import type` for clarity and to ensure it's stripped by TypeScript compilers.
MatchOnly
import { MatchOnly } from 'tcompare'
const MatchOnly = require('tcompare').MatchOnly
Classes like `MatchOnly` are exported directly and can be instantiated for programmatic control over comparisons.

This quickstart demonstrates the core `match` function, showcasing how to compare objects against patterns and interpret the `Result` object for both match status and diff output. It also illustrates direct usage of the `MatchOnly` class and a `same` comparison.

import { match, MatchOnly, same } from 'tcompare'; import type { Result } from 'tcompare'; // Basic usage with 'match' const object = { a: 1, b: 2, c: { d: 3 } }; const pattern = { a: Number, c: { d: 3 } }; const result: Result = match(object, pattern); if (!result.match) { console.log(`Item did not match pattern:\n${result.diff}`); } else { console.log(`It's a match!\n${result.diff}`); } // Using a specific comparison class directly const mo = new MatchOnly(object, { a: 1, b: 2, c: { d: 3 } }); console.log(`MatchOnly result: ${mo.match}`); // Should be true if object matches pattern exactly console.log(`MatchOnly diff:\n${mo.print()}`); // Example of a strict comparison const strictObject = { value: 1 }; const strictPattern = { value: '1' }; const strictResult: Result = same(strictObject, strictPattern); console.log(`Strict 'same' result (1 vs '1'): ${strictResult.match}`); console.log(`Strict 'same' diff:\n${strictResult.diff}`);
Debug
Known issues
gotchatcompare offers multiple comparison methods (`same`, `strict`, `has`, `match`, `matchOnly`, etc.) which differ significantly in their strictness and behavior regarding extra properties or type coercion. Choosing the wrong method can lead to unexpected test failures or false positives.
fix
Carefully review the documentation for each comparison method and select the one that precisely matches your assertion requirements. For example, `match` is very loose, `strict` is very strict, and `matchOnly` requires an exact property match.
affects: >=1.0.0
gotchaWhen using `match` or `has` methods, loose equality is often applied (e.g., `1` can match `'1'`). If strict type checking is required, ensure you are using `strict`, `hasStrict`, `matchStrict`, or `matchOnlyStrict`.
fix
Always append `Strict` to the comparison method name (e.g., `strict()`, `matchStrict()`) if JavaScript's `===` equality is desired for all comparisons within the object structure.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: (0 , tcompare_1.match) is not a function
Attempting to import CommonJS-style exports in an ESM context, or a bundling issue.
fix
Ensure your project is configured for ES Modules (e.g., `"type": "module"` in `package.json`) if using `import`. Verify that your build tools correctly transpile or bundle modules.
Property 'diff' does not exist on type 'boolean'.
Confusing the boolean return of older or simpler comparison utilities with tcompare's `Result` object.
fix
Remember that tcompare methods return a `Result` object `{ match: boolean, diff: string }`. Always access `result.match` for the boolean outcome and `result.diff` for the diff string. Ensure you have `import type { Result } from 'tcompare'` if using TypeScript.
Upgrade
Version history
9.3.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
tcompare — npm install tcompare · libregistry