How to trace a failing assertion in a Purescript test

Viewed 78

I am doing first steps with Purescript and replicated "hello world" app.

Test passes. I broke the test deliberately.

module Test.Main where

import Prelude
import Effect (Effect)
import Euler (answer)
import Test.Assert (assert)

main :: Effect Unit
main = do
  assert (answer == 233161)

The handmade error is reported, but quality of the error report worries me because it doesn't help anyhow with spotting the error. There is a verbose JS stacktrace without any reference to origin file or Purescript module/function! This is a super simple app with 1 test, but how I am supposed to find that needle in a haystack of myriads of tests in a real app?

spago test
[info] Installation complete.
Compiling Test.Main
[info] Build succeeded.
/home/dan/demo/pure-script/1/output/Test.Assert/foreign.js:6
      if (!success) throw new Error(message);
                    ^

Error: Assertion failed
    at Object.main (/home/dan/demo/pure-script/1/output/Test.Assert/foreign.js:6:27)
    at Object.<anonymous> (/home/dan/demo/pure-script/1/.spago/run.js:3:32)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:282:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
[error] Tests failed: exit code: 1
1 Answers

As you can maybe infer from the README of purescript-assert, the Test.Assert module is only a super basic assertion library for authors of actual testing libraries (because they can't use their own library without causing circular dependencies).

It is therefore not supposed to give very good error stacks and location information. Packages that use assert often do a bunch of logging in between:

main = do
  Console.log "Testing answer"
  Console.log "assert answer is 42"
  assert $ answer == 42

  Console.log "assert answer is answer to everything"
  assert $ answer `isAnswerTo` "everything"

I recomment using purescript-spec for anything more serious. Unfortunately the debugging and testing story of PureScript is not the greatest, but the language really shines in other aspects.

Related