Skip to content

Latest commit

 

History

History
77 lines (62 loc) · 1.75 KB

propContains.md

File metadata and controls

77 lines (62 loc) · 1.75 KB
layout title excerpt groups redirect_from version_added
page-api
assert.propContains()
Check that an object contains certain properties.
assert
/assert/propContains/
2.18.0

propContains( actual, expected, message = "" )

Check that an object contains certain properties.

name description
actual Expression being tested
expected Known comparison value
message (string) Short description of the actual expression

The propContains assertion compares only the subset of properties in the expected object, and tests that these keys exist as own properties with strictly equal values.

This method is recursive and allows partial comparison of nested objects as well.

See also

Examples

QUnit.test('example', assert => {
  const result = {
    foo: 0,
    vehicle: {
      timeCircuits: 'on',
      fluxCapacitor: 'fluxing',
      engine: 'running'
    },
    quux: 1
  };

  assert.propContains(result, {
    foo: 0,
    vehicle: { fluxCapacitor: 'fluxing' }
  });

  function Point (x, y) {
    this.x = x;
    this.y = y;
  }

  assert.propContains(
    new Point(10, 20),
    { y: 20 }
  );

  assert.propContains(
    [ 'a', 'b' ],
    { 1: 'b' }
  );

  const nested = {
    north: [ /* ... */ ],
    east: new Point(10, 20),
    south: [ /* ... */ ],
    west: [ /* ... */ ]
  };

  assert.propContains(nested, { east: new Point(10, 20) });
  assert.propContains(nested, { east: { x: 10, y: 20 } });
  assert.propContains(nested, { east: { x: 10 } });
});