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

Support tmatch in toHaveBeenCalledWith #107

Open
jraede opened this issue Jun 16, 2016 · 4 comments
Open

Support tmatch in toHaveBeenCalledWith #107

jraede opened this issue Jun 16, 2016 · 4 comments

Comments

@jraede
Copy link

jraede commented Jun 16, 2016

Is there a way to do something like this:

expect(mySpy).toHaveBeenCalledWith('foo', 'bar', anythingOfType(Function));

Seems like we would have to modify the isEqual test helper.

@ljharb
Copy link
Collaborator

ljharb commented Jun 16, 2016

How would "of type" be determined? instanceof is both unreliable and doesn't work cross-realm - this code is what's actually required to reliably determine that something is a function.

The only API i can think of that might work is one where the user decides for themselves - ie, anythingOfType would need to take a predicate function that received the value, and returned a boolean to indicate whether it passed. Thoughts?

@jraede
Copy link
Author

jraede commented Jun 17, 2016

Yeah I think allowing a predicate function would be the most pluggable. In order to have the isEqual know whether to run the function or compare its equality we could add something like matchingPredicate(myPredicate) or matcher(myPredicate) which would return some sort of object that isEqual could recognize and run the predicate function.

It would be pretty cool to implement something like I did in simplecheck or just add support for that library, so we could do things like

expect(mySpy).toHaveBeenCalledWith('foo', 'bar', simplecheckMatcher({
     someProp: String,
     someOtherProp: oneOf(1, 2),
}));

@mjackson
Copy link
Owner

@jraede We already use tmatch under the hood in in toMatch. Maybe we could use it here as well? Seems like it does the same thing as simplecheck.

@mjackson mjackson changed the title "anything" argument for spy assertions? Support tmatch in toHaveBeenCalledWith Jul 26, 2016
@ZephD
Copy link

ZephD commented May 8, 2017

The ability to do partial object checks with toHaveBeenCalledWith would be fantastic, so +1.
Something like:

const obj = { a: 'a', b: 'b' };
const spy = expect.createSpy();
spy(obj);
expect(spy).toHaveBeenCalledWith({ a: 'a' })

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

4 participants