elm-explorations / test

Write unit and fuzz tests for Elm code.
https://package.elm-lang.org/packages/elm-explorations/test/latest
BSD 3-Clause "New" or "Revised" License
236 stars 40 forks source link

Add a way to create a test that expects runtime exception. #141

Closed malaire closed 3 years ago

malaire commented 4 years ago

For example:

test "modby 0 0 throws runtime exception" <|
    \_ -> Basics.modBy 0 0 |> Expect.runtimeException
harrysarson commented 3 years ago

What would the use case for this be?

malaire commented 3 years ago

For example in my malaire/elm-uint64 package I would prefer division-related functions to throw runtime exception on division-by-zero, but as such behavior can't be tested currently, and I only want to implement testable behavior, I was forced to return 0 instead on division-by-zero as that can be tested.

harrysarson commented 3 years ago

You could test this be creating an elm application that does division by 0 and then running it in node. One could run this node script as part of CI even.

try {
  Elm.Main.init();
} catch (e) {
  assert(...);
  return;
}
throw new Error("did not crash at runtime");

Given that getting a runtime crash requires some sort of hack on the part of the library, I think needing a separate test script for this behavior is probably not unreasonable.

malaire commented 3 years ago

Requiring using node directly is unreasonable. I accept that I need it undirectly (e.g. elm-test requires node) but I will never use node directly.

harrysarson commented 3 years ago

How does direct and undirect use of node differ? It is hard to run javascript with out node. I guess you could try and hook something up with deno if you were so inclined.

malaire commented 3 years ago

I consider any use of node/npm to be a serious bug. But as elm-test is the only way to run Elm tests I accept using it for now.

harrysarson commented 3 years ago

I think this would work in deno or in the browser which should cover you here.

I do not think an API such as you propose here is in scope for this package at this time unfortunately.

harrysarson commented 3 years ago

I am going to close this issue as it looks like there is nothing actionable we can do here and there is a work around. @malaire please do reopen if I am missing anything :)