1 | chai-subset [![npm version](https://badge.fury.io/js/chai-subset.svg)](https://badge.fury.io/js/chai-subset) [![Build Status](https://travis-ci.org/debitoor/chai-subset.svg?branch=master)](https://travis-ci.org/debitoor/chai-subset) [![devDependency Status](https://david-dm.org/debitoor/chai-subset/dev-status.svg)](https://david-dm.org/debitoor/chai-subset#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/debitoor/chai-subset/badge.svg?service=github)](https://coveralls.io/github/debitoor/chai-subset) [![NSP Status](https://nodesecurity.io/orgs/debitoor/projects/eb6fec04-2b26-4462-b4ff-08d952da3065/badge)](https://nodesecurity.io/orgs/debitoor/projects/eb6fec04-2b26-4462-b4ff-08d952da3065)
|
2 | ===========
|
3 |
|
4 | "containSubset" object properties matcher for [Chai](http://chaijs.com/) assertion library
|
5 |
|
6 | Installation
|
7 | ===========
|
8 |
|
9 | `npm install --save-dev chai-subset`
|
10 |
|
11 | Usage
|
12 | =====
|
13 |
|
14 | common.js
|
15 | ```js
|
16 | var chai = require('chai');
|
17 | var chaiSubset = require('chai-subset');
|
18 | chai.use(chaiSubset);
|
19 | ```
|
20 |
|
21 | in your spec.js
|
22 | ```js
|
23 | var obj = {
|
24 | a: 'b',
|
25 | c: 'd',
|
26 | e: {
|
27 | foo: 'bar',
|
28 | baz: {
|
29 | qux: 'quux'
|
30 | }
|
31 | }
|
32 | };
|
33 |
|
34 | expect(obj).to.containSubset({
|
35 | a: 'b',
|
36 | e: {
|
37 | baz: {
|
38 | qux: 'quux'
|
39 | }
|
40 | }
|
41 | });
|
42 |
|
43 | // or using a compare function
|
44 | expect(obj).containSubset({
|
45 | a: (expectedValue) => expectedValue,
|
46 | c: (expectedValue) => expectedValue === 'd'
|
47 | })
|
48 |
|
49 | // or with 'not'
|
50 | expect(obj).to.not.containSubset({
|
51 | g: 'whatever'
|
52 | });
|
53 | ```
|
54 |
|
55 | Also works good with arrays and `should` interface
|
56 | ```js
|
57 | var list = [{a: 'a', b: 'b'}, {v: 'f', d: {z: 'g'}}];
|
58 |
|
59 | list.should.containSubset([{a:'a'}]); //Assertion error is not thrown
|
60 | list.should.containSubset([{a:'a', b: 'b'}]); //Assertion error is not thrown
|
61 |
|
62 | list.should.containSubset([{a:'a', b: 'bd'}]);
|
63 | /*throws
|
64 | AssertionError: expected
|
65 | [
|
66 | {
|
67 | "a": "a",
|
68 | "b": "b"
|
69 | },
|
70 | {
|
71 | "v": "f",
|
72 | "d": {
|
73 | "z": "g"
|
74 | }
|
75 | }
|
76 | ]
|
77 | to contain subset
|
78 | [ { a: 'a', b: 'bd' } ]
|
79 | */
|
80 | ```
|
81 |
|
82 | and with `assert` interface
|
83 | ```js
|
84 | assert.containSubset({a: 1, b: 2}, {a: 1});
|
85 | ```
|