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

[feature-request] support for expectations based on sinon matcher #92

Closed
eauc opened this issue Mar 30, 2017 · 6 comments
Closed

[feature-request] support for expectations based on sinon matcher #92

eauc opened this issue Mar 30, 2017 · 6 comments

Comments

@eauc
Copy link

eauc commented Mar 30, 2017

Hi,

I've been looking for a chai plugin to:

  • deep-match objects containing functions,
  • deep-match objects partially,
  • deep-match arborescences with custom matchers inserted at some places.

In short, I want what Jasmine.any() gives me in expectations.
With chai's colored diff it would be only better.

I've found a few (chai-fuzzy, chai-subset ...), but none that allow easy matching of a function property, or insertion of custom matcher... (interested if you know any that do).

Sinon Matchers actually do exactly what I want on the matching side.

I'm wondering if you would consider adding expectations to your plugin, to support the use of sinon.match directly.

For the time being I've written a prototype for my team:

const Assertion = _chai.Assertion;
Assertion.addMethod("lookLike", function (like) {
	this.assert(
		sinon.match(like).test(this._obj),
		"expected #{this} to look like #{exp} but got #{act}",
		"expected #{this} to not look like #{act}",
		like,
		this._obj,
		true
	);
});

Then we can use it like this:

expect(result).to.lookLike({
	method: "GET",
	url: this.expectedUrl,
	onSuccess: sinon.match.func,
});

The exact semantics would need more thinking and discussion, but do you think it has a place in your module (since it's called sinon-chai ;) ), or should I make my own separately ?

@domenic
Copy link
Collaborator

domenic commented Mar 30, 2017

Isn't this calledWithMatch? All the other asserters should also accept matchers wherever sinon does.

@eauc
Copy link
Author

eauc commented Mar 30, 2017

The point actually is to use this without any spy.
Sometime you just want to match the return of your function-under-test with sinon-match ;)

@eauc
Copy link
Author

eauc commented Mar 30, 2017

something like this:

// WHEN
const result = webModel.getScriptDescription(this, {onSuccess() {}});
// THEN
expect(result).to.lookLike({
	method: "GET",
	url: this.expectedUrl,
	onSuccess: sinon.match.func,
});

@domenic
Copy link
Collaborator

domenic commented Mar 30, 2017

This library is strictly dedicated to exposing the existing Sinon assertions through Chai. If you want to use matchers without spies, you'll need to first open such an issue on Sinon, and then we'll work on exposing it here if they accept that feature.

@domenic domenic closed this as completed Mar 30, 2017
@cincodenada
Copy link
Contributor

cincodenada commented Oct 8, 2021

Hello! I don't know when it was added, but current Sinon has Sinon.assert.match which does allow using matchers without spies. Unfortunately this collides with Chai's native .match, so we'd have to come up with a different name, but it would be really nice to have this supported in sinon-chai - if it seems useful I can take a look at putting up a PR for it. As of now, I have a bunch of Chai assertions and then something like this, which works but is a bummer:

Sinon.assert.match({example: 'present'}, {example: 'present', otherField: 'alsoPresent'})

For names, I haven't come up with any good options. sinonMatch is clear and somewhat guessable but doesn't match anything else. looksLike as above isn't bad. should.be.similarTo? should.matchMatcher would make sense if it weren't so silly-sounding.

@cincodenada
Copy link
Contributor

cincodenada commented Apr 14, 2022

I looked into implementing this, and although there is a Sinon assertion now, due to the way sinon-chai works, there's not a straightforward way to implement this in sinon-chai because we don't actually depend on sinon directly at all - with spies we can use the object being asserted against to get a reference to Sinon, but with exampleThing.should.match({key: "value"}) there's no reference to Sinon to attach to, so we'd have to do depend on our own version of Sinon which could conflict with the user's version, or we'd have to provide a way to inject the user's sinon into sinon-chai.

Then I remembered that Sinon's matchers are actually broken out into their own library, samsam, and it turns out there's already a Chai plugin that integrates samsam. So I ended up just using that alongside sinon-chai. which works great, I can do should.deep.match(Sinon.match.func) or what have you and it all works a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants