Verify icon indicating copy to clipboard operation
Verify copied to clipboard

VerifyJson as .json

Open 304NotModified opened this issue 1 year ago • 3 comments

Is the feature request related to a problem

Currently VerifyJson performs the following actions

  • Convert to JToken (if necessary).
  • Apply ignore member by name for keys.
  • PrettyPrint the resulting text.
  • But also, makes a .txt file without real JSON

This is confusing, also in the code review.

Describe the solution

I would like to see:

VerifyJson (with option) performs the following actions

  • Convert to JToken (if necessary).
  • Apply ignore member by name for keys.
  • PrettyPrint the resulting text.
  • Prints as JSON to a .json file

Describe alternatives considered

  • Call inner methods of Verify (not sure what is public for this)
  • Or, use Verify() instead of VerifyJson and do the pretty pretting en ignoring by myself (sounds like a lot of copy paste)

304NotModified avatar Apr 30 '24 11:04 304NotModified

the Verify* methods are about the input. not the output. essentially convenience methods to resolve the ambiguity of passing everything to Verify. if you want to default to json as the output file extension, you should use UseStrictJson https://github.com/VerifyTests/Verify/blob/main/docs/serializer-settings.md#usestrictjson

SimonCropp avatar Apr 30 '24 12:04 SimonCropp

Could I pass UseStrictJson to the VerifyJson method?

I tested it, and indeed the UseStrictJson is what I need, but I can't set it for all the tests (so no ModuleInitializer)

the Verify* methods are about the input. not the output

I don't really understand this. We use snapshot tests for the content we control. In this case it's the response content of our own API. Am I using the wrong methods for that?

304NotModified avatar Apr 30 '24 13:04 304NotModified

Could I pass UseStrictJson to the VerifyJson method?

currently, no. Happy to accept a PullRequest that adds it. but it should probably be as a parameter overload

but I can't set it for all the tests (so no ModuleInitializer)

Do you mean "you dont want to set it for all test" or "you dont know how to set it for all tests"?

I don't really understand this. We use snapshot tests for the content we control. In this case it's the response content of our own API

The output of verify is not json for readability reasons. for example json has to have many characters escaped. take newlines: if you have a long string with many newlines, json requires that those newlines are escaped and the resulting snapshot has one huge blob of text. this also makes it more difficult to compare changes inside the text. for readability it is better to place (non-encoded) newlines in the snapshot. the same goes for quotes

Am I using the wrong methods for that?

I cant tell from this thread. Basically anything that can be serialized will be done so in the most human readable form. that goes for VerifyJson

SimonCropp avatar Apr 30 '24 23:04 SimonCropp

The output of verify is not json for readability reasons. for example json has to have many characters escaped

Ah clear. Well in this case the input is already JSON, so that one is safe :)

but I can't set it for all the tests (so no ModuleInitializer)

Do you mean "you dont want to set it for all test" or "you dont know how to set it for all tests"?

Don't want, as I prefer not to change other snapshottests

Happy to accept a PullRequest that adds it. but it should probably be as a parameter overload

Could you please give me a hint where to add this? And why not an option on VerifySettings (e.g. VerifySettings.UseStrictJson())?

304NotModified avatar May 02 '24 10:05 304NotModified

And what is the conceptual difference between VerifySettings and VerifierSettings?

304NotModified avatar May 02 '24 10:05 304NotModified

the main entry point is Verifier.Verify*.

VerifierSettings applies to all usages of the class Verifier. ie it is static

VerifySettings applies to a usage of the method Verify. ie it is not static.

so u use VerifierSettings to config for all usages. usually called in a module init. and use VerifySettings to config for a single (or group) of usages.

there is also SettingsTask which mirrors VerifySettings, but it enable a fluent api

SimonCropp avatar May 02 '24 10:05 SimonCropp

i added an instance based UseStrictJson. will that suffice?

https://github.com/VerifyTests/Verify/blob/main/docs/serializer-settings.md#instance

SimonCropp avatar May 09 '24 10:05 SimonCropp