-
Notifications
You must be signed in to change notification settings - Fork 117
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 object includes a string key and exclusion test for objects #86
Add object includes a string key and exclusion test for objects #86
Conversation
@@ -258,10 +258,25 @@ expect([ 1, 2, 3 ]).toExclude(4) | |||
Asserts the given `object` contains all keys and values in `value`, recursively. The `comparator` function, if given, should compare two objects and either `return false` or `throw` if they are not equal. It defaults to `assert.deepEqual`. | |||
|
|||
```js | |||
expect({ a: 1, b: 2 }).toInclude('a') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm not really a fan of overloading APIs to take multiple types. I'd rather see a separate .toIncludeKeys
that takes an array of strings, for example.
I do like the |
@@ -334,8 +334,8 @@ class Expectation { | |||
|
|||
toExclude(value, compareValues, message) { | |||
assert( | |||
isArray(this.actual) || typeof this.actual === 'string', | |||
'The "actual" argument in expect(actual).toExclude() must be an array or a string' | |||
isArray(this.actual) || isObject(this.actual) || typeof this.actual === 'string', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i wonder if there's a way we could make toExclude
call into toInclude
, or have them share a core function, rather than duplicating the checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Check my latest commit and tell me what you think.
This reverts commit b8fed4b.
45a4514
to
096af85
Compare
@@ -293,77 +293,47 @@ class Expectation { | |||
return this | |||
} | |||
|
|||
toInclude(value, compareValues, message) { | |||
toInclude(value, compareValues, message, _exclude = false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely not a fan of the _exclude
param - this adds an obscure boolean param to the public API. Could we refactor both toInclude
and toExclude
to call into a private (closed-over) function instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is a precedence for this already included for the Expectation
class? My next instinct would to be to create a TestUtils
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, ammended the last commit with the core implementation in TestUtils
.
afce62d
to
57f2343
Compare
57f2343
to
c103bcd
Compare
*/ | ||
export const containsHelper = (actual, value, compareValues, exclude, funcName, message) => { | ||
if (compareValues == null) | ||
compareValues = isEqual |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could this be done with default values in the signature instead?
In general I think this looks great overall - I like the reduced code duplication, and the new docs/tests are solid. |
value | ||
) | ||
} | ||
containsHelper( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this probably sounds silly, but I think I'd prefer to leave the branching in this method rather than tuck it away under containsHelper
. It personally makes it easier for me to read the code and figure out e.g. what kinds of actual
values toInclude
accepts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, doesn't sound silly. Would you prefer duplicating the logic or adding a private boolean switch as an undocumented parameter to switch the behavior like I had before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My vote is to duplicate the logic rather than adding public API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to see it with the logic duplicated for now. We can take another pass at it later if it becomes ridiculously long.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds great 👍
Hi @calebmer – thanks for your work here! I'm sorry to be slow on the response. Just got back from 2 weeks away. I like the docs updates, additional tests, and support for objects in |
} | ||
|
||
assert( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 Well done. This is some de-duplication I can live with!
First of all, thank you so much for writing this package. It makes writing tests so nice 😊. In addition this codebase is some of the best code I have ever read.
I just ran into two things which I expected to exist but didn't, this PR adds these things. Specifically this PR adds one feature and fixes one bug.
The feature this PR adds is a simple property string test for
toInclude
. Now, the user can ignore the value of an object and just test if a property is included in an object like so:expect({ a: 1 }).toInclude('a')
.This feature also fixes a bug where objects were not enabled to be used by
toExclude
making it asymmetrical totoInclude
which tests objects.