Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added notContainSubset option to assert #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 34 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
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)
===========
# 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)

"containSubset" object properties matcher for [Chai](http://chaijs.com/) assertion library

Installation
===========
# Installation

`npm install --save-dev chai-subset`

Usage
=====
# Usage

common.js

```js
var chai = require('chai');
var chaiSubset = require('chai-subset');
var chai = require("chai");
var chaiSubset = require("chai-subset");
chai.use(chaiSubset);
```

in your spec.js

```js
var obj = {
a: 'b',
c: 'd',
e: {
foo: 'bar',
baz: {
qux: 'quux'
}
}
a: "b",
c: "d",
e: {
foo: "bar",
baz: {
qux: "quux"
}
}
};

expect(obj).to.containSubset({
a: 'b',
e: {
baz: {
qux: 'quux'
}
}
a: "b",
e: {
baz: {
qux: "quux"
}
}
});

// or using a compare function
expect(obj).containSubset({
a: (expectedValue) => expectedValue,
c: (expectedValue) => expectedValue === 'd'
})
a: expectedValue => expectedValue,
c: expectedValue => expectedValue === "d"
});

// or with 'not'
expect(obj).to.not.containSubset({
g: 'whatever'
g: "whatever"
});
```

Also works good with arrays and `should` interface

```js
var list = [{a: 'a', b: 'b'}, {v: 'f', d: {z: 'g'}}];
var list = [{ a: "a", b: "b" }, { v: "f", d: { z: "g" } }];

list.should.containSubset([{a:'a'}]); //Assertion error is not thrown
list.should.containSubset([{a:'a', b: 'b'}]); //Assertion error is not thrown
list.should.containSubset([{ a: "a" }]); //Assertion error is not thrown
list.should.containSubset([{ a: "a", b: "b" }]); //Assertion error is not thrown

list.should.containSubset([{a:'a', b: 'bd'}]);
list.should.containSubset([{ a: "a", b: "bd" }]);
/*throws
AssertionError: expected
[
Expand All @@ -80,6 +80,8 @@ to contain subset
```

and with `assert` interface

```js
assert.containSubset({a: 1, b: 2}, {a: 1});
assert.containSubset({ a: 1, b: 2 }, { a: 1 });
assert.notContainSubset({ a: 1, b: 2 }, { a: 3 });
```
4 changes: 4 additions & 0 deletions lib/chai-subset.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
new chai.Assertion(val, msg).to.be.containSubset(exp);
};

chai.assert.notContainSubset = function(val, exp, msg) {
new chai.Assertion(val, msg).not.to.be.containSubset(exp);
};

function compare(expected, actual) {
if (expected === actual) {
return true;
Expand Down
Loading