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

Adding a compare function to be used in expected object template #66

Merged
merged 2 commits into from
Sep 5, 2017
Merged
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ expect(obj).to.containSubset({
}
}
});
//or with 'not'

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

// or with 'not'
expect(obj).to.not.containSubset({
g: 'whatever'
});
Expand Down
3 changes: 3 additions & 0 deletions lib/chai-subset.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
if (typeof(eo) === 'object' && eo !== null && ao !== null) {
return compare(eo, ao);
}
if (typeof(eo) === 'function') {
return eo(ao);
}
return ao === eo;
});
}
Expand Down
14 changes: 14 additions & 0 deletions test/unit/chai-subset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ describe('circular objects', function() {
});
});

describe('object with compare function', function() {
it('should pass when function returns true', function () {
expect({a: 5}).to.containSubset({a: a => a});
});

it('should fail when function returns false', function () {
expect({a: 5}).to.not.containSubset({a: a => !a});
});

it('should pass for function with no arguments', function () {
expect({a: 5}).to.containSubset({a: () => true});
});
});

describe('comparison of non objects', function () {
it('should fail if actual subset is null', function () {
expect(null).to.not.containSubset({a: 1});
Expand Down