-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add assertion that just compares specific items in the tree #845
Comments
A bit like Sinon.JS matchers? |
I'd find this pretty useful 👍 |
Yes. I was thinking of Sinon when coming up with this. |
So far, I've just used |
I do like that Sinon allows you to attach predicates. What if we allowed the same thing, but they exactly matched existing assertions: t.match(obj, {
foo: t.match.is('foo') // obj.foo must be strict equal
bar: t.match.deepEqual({baz: 'quz'}), // traditional deepEquals, must only have the baz property
baz: t.match.throws(), // a rejected promise?
quz: t.match.regex(),
promise: t.match.then.deepEqual() // same as t.match, but for promise values?
}); Essentially, We could of course provide shortcuts. Primitives would shortcut to t.match(obj, {
foo: 'bar', // equivalent to t.match.is('bar')
bar: { // not the same as above. Equivalent to `t.match` instead of `deepEquals`.
baz: 'quz' // We only check the baz, property, ignoring the rest
}
}); |
It would be awesome to allow you to build reusable predicates this way, so I think you expose the predicate builder on const isIdentifier = name => test.match({
type: 'Identifier',
name
});
const isT = isIdentifier('t');
const isAssertion = name => test.match({
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: isT,
property: isIdentifier(name)
}
});
const isThrowsAssertion = isAssertion('throws');
test(t => {
// ...
t.match(node, isThrowsAssertion)
}); |
The last proposal is not more reusable that just creating a new function. const isIdentifier = name => ({
type: 'Identifier',
name
});
const isT = isIdentifier('t');
const isAssertion = name => ({
type: 'CallExpression',
callee: {
type: 'MemberExpression',
object: isT,
property: isIdentifier(name)
}
});
const isThrowsAssertion = isAssertion('throws');
test(t => {
// ...
t.match(node, isThrowsAssertion)
}); (exact same thing, just removed |
Your right. |
I like the idea of |
I don't think we would add two separate function names. The question is whether leaf nodes of the |
I did not mean the name I liked the idea behind |
To be fair I've always found |
Yeah, I vote for implementing the API described in the initial posting. t.match seems too complicated to fit the simplicity of the rest of ava's APIs |
I agree with with implementing the proposed |
Chai uses |
See #845 (comment). A lot of them use match for this, some use match for strings (and some for both). |
I've always used: https://www.npmjs.com/package/chai-subset
|
Let's do |
Let me get my hands dirty on this. |
@leewaygroups cool! I'm rereading the thread now and I can't figure out where we landed with deep objects. Perhaps we'll follow https://lodash.com/docs/4.16.6#isMatch as suggested by @jfmengels in #845 (comment)? We already use https://lodash.com/docs/4.16.6#isEqual in |
First, I agree with the idea of maintaining a simple interface so I think the name Since lodash is one of the dependencies used, @jfmengels suggestion My take is, adopting a simple name What do you think? |
Please be efficient with dependencies. |
That's a show-stopper, unfortunately. Why do you consider it to be more robust than
We don't depend on all of Lodash, so regardless we'd be adding a new dependency. That said, using modules of the same high-quality origin for our assertions seems like a good idea. |
Are we green 🚥 for |
|
The pros for Lodash is compelling and green. After previous converstaions went with lodash. I'll checkin the changeset for review soon. @mightyiam @novemberborn Thanks for the follow up. |
@mightyiam How do you get the full object printed in terminal with I'm testing tree like structure with same method, getting output like this:
That's painful that I cannot recognize what is failing
|
As of #1341 we're no longer using |
Finally I wrapped an isMatch() for logging ... const im = require('lodash.ismatch')
const chalk = require('chalk')
const inspect = require('util').inspect
function isMatch (actual, expected) {
const res = im(actual, expected)
if (res === false) {
console.log(chalk.green('\nexpected:'))
console.log(inspect(expected, { depth: null }))
console.log(chalk.red('\nactual:'))
console.log(inspect(actual, { depth: null }))
console.log()
}
return res
} |
Did this get resolved? Is there a way to do partial matches? The suggestion by @frantic1048 and @mightyiam both produce erroneous output in the console when used with |
@norbertkeri nope. It requires a solid proposal as to how this would work, ensuring the same kind of diff output as With regards to @frantic1048's example, if you can pass it the |
I can use |
By erroneous you mean "not enough detail"? That's a hard one to solve, hence the approach advocated by others. The real solution is to have a partial-match assertion, of course. |
No, using this code: t.true(isMatch(result, { currentUser: { email: 'hello@world.com' }})); Produces this output:
Which all comes from the |
@norbertkeri Which version of Here's my usage of doing partial matching: I just wrapped lodash's isMatch a little to generate detailed log. It is not very ergonomic but prints enough data for debugging. |
@frantic1048 the example from the comment. Could you show me what output do you get when the test fails (actual doesn't match expected)? |
@norbertkeri For example, I deleted one line of input text to parse(), which makes
(
|
Ok I'm not sure what's going in my code then, the output I get is very different (noted above). Will take a look later, thanks. |
@IssueHunt has funded $80.00 to this issue.
|
@novemberborn has rewarded $72.00 to @futpib. See it on IssueHunt
|
This would be especially handy for AST type tests.
Consider the following
It would be nice to have a sort of "
deepStrictEqual
but only for the portion of the graph I specify"I am going to use
t.like
:In the above, I'm basically saying "I want to verify it's a
MemberExpression
Node, whoseobject
property is anIdentifier
with namefoo
".loc
,start
,end
are ignored throughout, as is theproperty
property.IssueHunt Summary
futpib has been rewarded.
Backers (Total: $80.00)
Submitted pull Requests
like
assertion (fix #845)Tips
The text was updated successfully, but these errors were encountered: