Skip to content

Difficult to annotate test failures #58

@ChickenProp

Description

@ChickenProp

It would be nice to be able to add a hint when a golden test fails. Sometimes just "run hgold -u path/to/golden-files", since not everyone is familiar with the framework. Sometimes something more detailed like "you can update the golden output if ..., but otherwise the code needs to be fixed".

That seems hard right now, partly because the only way to run a golden test is with the Example instance. Here's what I've currently got:

let hint = "Run `hgold -u path/to/golden-files`"
result <- Hspec.Core.evaluateExample golden undefined ($ ()) undefined
case result.resultStatus of               
  Hspec.Core.Failure
    loc
    (Hspec.Core.ExpectedButGot Nothing expected actual) ->
      throwM $
        Hspec.Core.Failure
          loc
          ( Hspec.Core.ExpectedButGot
              (Just $ result.resultInfo ++ "\n\n" ++ hint)
              expected
              actual
          )
  Hspec.Core.Success -> pure ()                                                                                                               
  _ -> throwM result.resultStatus

with golden :: Golden Value and import Test.Hspec.Core.Spec qualified as Hspec.Core.

That is, I have to run evaluateExample manually to get an Result in IO. Then I can't do much with that directly, so I unpack it to get a ResultStatus, which has an Exception instance. So I put the failure message from the Result in an unused field in the ResultStatus, along with the hint, and throw the new status.

(This would also be less bad if hspec let me do more with a Result. There's instance Example Result, and instance Example (IO ()). But not instance Example (IO Result) or instance Exception Result.)

A simple improvement for me would be if runGolden and GoldenResult were exported. Then I could match on MismatchOutput and throw ExpectedButGot myself. I wouldn't get access to the "Files golden and actual not match" message that way but that's no big deal.

There might be some other way to improve it too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions