chai-equal-ignore-undefined-properties

npm npm code style: prettier CI Status

chai のディープエクオールオペレーションから undefined 値のキーを比較から無視する expect

理由

場合によって、undefined 値を持つプロパティを持つことがあります。このプラグインは、それらのプロパティを比較から無視するのに役立ちます。

循環参照の有無にかかわらず、オブジェクトとオブジェクトの配列の両方で動作します。

インストール

npm install chai-equal-ignore-undefined-properties --save-dev
yarn add chai-equal-ignore-undefined-properties --dev

使用方法

必要条件

const chai = require("chai");
const chaiIgnoreUndefinedProperties = require("chai-equal-ignore-undefined-properties");

chai.use(chaiIgnoreUndefinedProperties);

ES6 インポート

import chai from "chai";
import chaiIgnoreUndefinedProperties from "chai-equal-ignore-undefined-properties";

chai.use(chaiIgnoreUndefinedProperties);

TypeScript

import * as chai from "chai";
import chaiIgnoreUndefinedProperties from "chai-equal-ignore-undefined-properties";

chai.use(chaiIgnoreUndefinedProperties);

// The typings for chai-equal-ignore-undefined-properties are included with the package itself.

これらの例はすべて JavaScript 用です。

a) 除外

  1. オブジェクトから最上位レベルのプロパティを無視する
expect({ aa: undefined, bb: "b" }).to.equal({
  bb: "b",
  cc: undefined,
});
  1. 未定義の値を持つ配列内のプロパティを無視する
const expectedArray = [{ aa: undefined, bb: "b" }];
const actualArray = [{ cc: undefined, bb: "b" }];
expect(actualArray).to.deep.equal(expectedArray);
  1. 未定義の値を持つネストされたプロパティを無視する(ディープエクオール比較でのみ)
expect({
  topLevelProp: { aa: undefined, bb: "b" },
}).to.deep.equal({
  topLevelProp: { bb: "b", cc: undefined },
});
  1. 循環依存関係で動作します
const actualObject = { aa: undefined, bb: "b" };
actualObject.c = actualObject;

const expectedObject = { bb: "b", cc: undefined };
expectedObject.c = expectedObject;

expect(actualObject).to.deep.equal(expectedObject);

貢献

貢献は大歓迎です。ご質問がある場合は、こちらで問題を作成してください。

ライセンス

MIT