LoginSignup
7
7

More than 5 years have passed since last update.

assert.deepEqual使用時の留意点

Posted at

node.jsとmochaの組み合わせてテストしていた時に気づいたこと。

assert.deepEqualについて。
オブジェクトの最下位層まで一致しているか確認してくれるのがassert.deepEqual。
例えばこんな感じのオブジェクトが対象となるテスト。

var example = {
  apartment: 'mansion111',
  room: {
    resident: 'Hanako',
    age: '26',
    properties: '',
    attr: {id: '1294'}
  }
};

こんなオブジェクトとassertしてくれるのがdeepEqual。
気づいた点が1つ。厳密な比較(===の比較)ではないということ。
まあassert.strictEqualassert.Equalがあるわけだから'strict'がなかったら厳密な比較じゃないの当たり前じゃんって言われたらそうなのですが。。。

厳密比較でないため、上のexampleオブジェクトのageは'26'で文字列で、数値の26と比較するしてもエラーは発生しない(テストが成功する)。

実際にassert.jsのコードも読んでみると

  // 7.4. Other pairs that do not both pass typeof value == 'object',
  // equivalence is determined by ==.
  } else if (typeof actual != 'object' && typeof expected != 'object') {
    return actual == expected;
}

ってなっている。

もしassert.deepEqualを使ってテストする場合は念のため頭の片隅に置いておくとよいかも。

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7