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

Stringify objects better #84

Merged
merged 11 commits into from
Mar 20, 2016
Merged

Stringify objects better #84

merged 11 commits into from
Mar 20, 2016

Conversation

searls
Copy link
Member

@searls searls commented Mar 19, 2016

Right now, a lot of logging in explain and verify will give up and print [Object object] sort of strings, when it'd be much rather to print them out. I forked and published this module to do most of the heavy lifting

  • give us a way to print objects better
  • make sure we plug it in everywhere we inspect objects for logging
  • test it
  • add a td.matchers.create() API, as the scheme for printing their names becomes more complicated
  • convert td's built-in matchers to call through to td.matchers.create()
  • pass the test on better matchers printing.
  • document the matchers.create API
  • contact @BaseCase and @BrianGenisio

@searls
Copy link
Member Author

searls commented Mar 20, 2016

Looks like it works. The previous system would blow up on circular objects (b/c it was using native JSON), would jam everything into one line, and would double-quote keys b/c JSON.

So the output of an argument changes from: {"joe":5,"jill":[1,"2",3]} to {joe: 5, jill: [1, "2", 3]}

@searls
Copy link
Member Author

searls commented Mar 20, 2016

I just realized that failed verifications that contain matchers are reallllllly ugly.

If I say:

someTestDouble(55)

td.verify(someTestDouble(td.matchers.isA(String)))

I get this monstrosity:

Unsatisfied verification on test double.

  Wanted:
    - called with `({
  __matches: function (actual) {
          if (type === Number) {
            return _.isNumber(actual);
          } else if (type === String) {
            return _.isString(actual);
          } else if (type === Boolean) {
            return _.isBoolean(actual);
          } else {
            return actual instanceof type;
          }
        }
})`.

  But was actually called:
    - called with `(55)`.

I think we should probably figure out a custom toString for matcher args; maybe based on function name?

@searls
Copy link
Member Author

searls commented Mar 20, 2016

Looks like we're good to merge! The above monstrosity will now print much more nicely as:

"""
      Unsatisfied verification on test double.

        Wanted:
          - called with `(isA(String))`.

        But was actually called:
          - called with `(55)`.
"""

Huzzah!!

@searls
Copy link
Member Author

searls commented Mar 20, 2016

cc / @BrianGenisio & @BaseCase -- since I've chatted with both of you about custom matchers before, I wanted to give you the heads up that this new feature is available.

searls added a commit that referenced this pull request Mar 20, 2016
@searls searls merged commit 793a7fc into master Mar 20, 2016
@searls searls deleted the better-tostring branch March 20, 2016 15:48
@BaseCase
Copy link

@searls Nifty! I'll see about supporting this in testdouble-chai sometime soon. 🆒

@searls
Copy link
Member Author

searls commented Mar 20, 2016

@BaseCase I'm also thinking about adding some metadata to the errors that verify throws. I assume that you're catching the errors to map to chai assertions?

@BaseCase
Copy link

@searls Right now it catches all verify errors and then uses td.explain to figure out what assertion message to print, but extra info right on the error object would totally be useful.

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

Successfully merging this pull request may close these issues.

None yet

2 participants