chai-recursive-match

npm package Build Status Issues Semantic Release

Chaiアサーションで再帰的な比較を簡単に実行する

主な機能

このChaiプラグインはオブジェクトと配列のシームレスな再帰的比較を可能にするよう、Chaiのアサーション機能を拡張します。

ネストされた構造の簡潔で表現力豊かなテストを書くことができ、あらゆるレベルでデータの完全性を確保できます。

  • 🔎 再帰的等価性: 2つのオブジェクトまたは配列が再帰的に等価であることをアサートする。ネストされた値とその型を考慮する。
  • 📦 再帰的インクルージョン: ネストされた値がオブジェクトまたは配列内に存在することを検証する。構造の奥深くにある場合でも。
  • 🔧 カスタマイズ可能なMatcher: Chaiの豊富なライブラリからMatcherを使用して、型検証、文字列パターン、数値範囲など、ネストされた値に特定の条件を定義する。
  • ℹ️ 有用なエラーメッセージ: アサーションが失敗した場合、ネストされた構造内の相違点の正確なパスを示す明確かつ詳細なエラーメッセージが表示される。

インストール

Chaiでテストエクスペリエンスを向上させてみてはいかがでしょうか。

npm install -D chai-recursive-match

注: TypeScriptの型を個別にインストールする必要はありません。含まれています。

使用方法

import { use } from 'chai';
import { chaiRecursive } from 'chai-recursive-match';

use(chaiRecursive);

API

再帰的等価性

オブジェクトまたは配列がパターンに対してチェックされる。types.tsでパターンの型の定義を参照してください。

簡単な例

expect({ foo: { bar: 'baz' } }).to.recursive.equal({
  foo: to => to.recursive.equal({ bar: to => to.be.a('string') }),
});

配列の例

expect([{ foo: { bar: 'baz' } }, { foo: { bar: 'foobar' } }]).to.recursive.equal([
  { foo: to => to.recursive.equal({ bar: to.be.a('string') }) },
  { foo: to => to.recursive.equal({ bar: to.match(/^foo/) }) },
]);

完全な例

expect({
  num1: 1,
  num2: 2,
  arr1: [1, 2, 3],
  arr2: [{ id: 1 }, { id: 2 }],
  str1: 'hello 1',
  str2: 'hello 2',
  obj1: { key: 'a', value: 'A' },
  obj2: { key: 'b', value: 'B' },
  method1() {},
}).to.recursive.equal({
  num1: 1,
  num2: to => to.be.gt(1),
  arr1: [1, 2, 3],
  arr2: to => to.deep.contain({ id: 2 }),
  str1: 'hello 1',
  str2: to => to.match(/^hello/),
  obj1: { key: 'a', value: 'A' },
  obj2: to => to.recursive.equal({ key: 'b', value: to => to.be.a('string') }),
});

配列にメンバーがあることを確認する

これはrecursive.includeに似ていますが、値はパターンに完全に一致することが期待されています。

expect([
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
]).to.recursive.equal({ id: 1, name: to => to.contain('A') });

配列にメンバーがあることを確認する

これは、比較するメンバーを複数指定したrecursive.have()に似ています。

expect([
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 2, name: 'Carol' },
]).to.recursive.have.members([
  { id: 1, name: to => to.contain('A') },
  { id: 3, name: to => to.contain('C') },
]);

否定と一緒に使う

expect({ foo: { bar: 'baz' } }).to.not.recursive.equal({
  foo: to => to.recursive.equal({ bar: to => to.be.a('number') }),
});

再帰的インクルージョン

オブジェクトの例

expect({ foo: { bar: 'baz' }, num: 123 }).to.recursive.include({
  num: to => to.be.gt(100),
});

配列の例

expect([{ foo: { bar: 'baz' } }, { foo: { bar: 'foobar' } }]).to.recursive.include({
  foo: to => to.recursive.equal({ bar: to.match(/^foo/) }),
});

配列にメンバーが含まれているか確認する

expect([
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 2, name: 'Carol' },
]).to.recursive.include.members([{ name: to => to.contain('A') }, { name: to => to.contain('C') }]);

否定と一緒に使う

expect([{ foo: { bar: 'baz' } }, { foo: { bar: 'foobar' } }]).to.not.recursive.include({
  foo: to => to.recursive.equal({ bar: to.match(/^baz/) }),
});

TBD

  • 🚧 エラーメッセージに差分を表示する
  • 🚧 chai.assertインターフェイスをサポートする
  • 🚧 配列メソッドの追加サポート(例: to.recursive.have.ordered.members