UNPKG

2.03 kBMarkdownView Raw
1chai-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
6Installation
7===========
8
9`npm install --save-dev chai-subset`
10
11Usage
12=====
13
14common.js
15```js
16var chai = require('chai');
17var chaiSubset = require('chai-subset');
18chai.use(chaiSubset);
19```
20
21in your spec.js
22```js
23var obj = {
24 a: 'b',
25 c: 'd',
26 e: {
27 foo: 'bar',
28 baz: {
29 qux: 'quux'
30 }
31 }
32};
33
34expect(obj).to.containSubset({
35 a: 'b',
36 e: {
37 baz: {
38 qux: 'quux'
39 }
40 }
41});
42
43// or using a compare function
44expect(obj).containSubset({
45 a: (expectedValue) => expectedValue,
46 c: (expectedValue) => expectedValue === 'd'
47})
48
49// or with 'not'
50expect(obj).to.not.containSubset({
51 g: 'whatever'
52});
53```
54
55Also works good with arrays and `should` interface
56```js
57var list = [{a: 'a', b: 'b'}, {v: 'f', d: {z: 'g'}}];
58
59list.should.containSubset([{a:'a'}]); //Assertion error is not thrown
60list.should.containSubset([{a:'a', b: 'b'}]); //Assertion error is not thrown
61
62list.should.containSubset([{a:'a', b: 'bd'}]);
63/*throws
64AssertionError: expected
65[
66 {
67 "a": "a",
68 "b": "b"
69 },
70 {
71 "v": "f",
72 "d": {
73 "z": "g"
74 }
75 }
76]
77to contain subset
78[ { a: 'a', b: 'bd' } ]
79*/
80```
81
82and with `assert` interface
83```js
84assert.containSubset({a: 1, b: 2}, {a: 1});
85```