Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Feature Request] Generate expected output #41

Open
sazzer opened this issue Sep 15, 2022 · 1 comment
Open

[Feature Request] Generate expected output #41

sazzer opened this issue Sep 15, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@sazzer
Copy link

sazzer commented Sep 15, 2022

What problem are you trying to solve?

I understand that this is an assertion tool and not a snapshotting tool, but it's the best choice for asserting that the JSON returned from an API call is correct, and in general it works fantastically for this.

However, it is slightly clunky when you are first writing a test. Unless you know exactly what the JSON should look like, you either need to generate it manually and paste into the test or else go through one value at a time fixing up errors until the test passes. Both of which are awkward.

(And yes, I'm aware this is slightly lazy, but it's super convenient especially when there's a lot of large JSON responses to check)

Describe how you wish this package would help you solve the problem

It would be fantastic if there were some way that the library could output the full actual JSON in some cases. Maybe if the expected JSON were just the blank string, for example.

That way you could write a test of

	ja.Assertf(string(body), ``)

Run the test, copy the output JSON into the test and know that it's correct.

@sazzer sazzer added the enhancement New feature or request label Sep 15, 2022
@kinbiko
Copy link
Owner

kinbiko commented Sep 16, 2022

@sazzer Thanks for submitting this issue.

This package will print the whole string being given if it's not valid JSON. So if you pass in something that's ALMOST correct JSON (e.g. ">"+string(body)), then you should see the full JSON printed.

That said, it's not being pretty printed (because it can't pretty print something that it believes isn't JSON), so this works best for small payloads.

If you want a pretty printed string you can write something like:

var pretty bytes.Buffer
_ = json.Indent(&pretty, body, "", "\t")
t.Log(string(pretty.Bytes())) // will only print if test fails or -v flag is used

Let me know if this doesn't sufficiently solve your problem.

I'm not against implementing a Golden File-like solution -- I actually think that's a good idea -- but I don't think just printing the actual payload given a specific expected payload is the right developer experience for solving the core problem.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants