git.iko.soy/rowtype-yoga/purescript-vitest
on main help sign in
Use PureScript Eq for equality assertions
commit454f5539e5600efca39f1d035e2b86fbb493a528 authorMark <mark.eibes@gmail.com> dateSun 12 Jul 2026, 15:19 (3 months ago) parent0f6831e
3 files changed+31−3
unifiedsplit
src/ViTest/Expect.js+8−1 src/ViTest/Expect.purs+4−2 test/Test/Main.purs+19
src/ViTest/Expect.js+8−1
@@ -10,7 +10,14 @@ export const unsafeStringify = x => {
1010 }
1111}
1212
13−export const expectToEqualImpl = x => y => () => expect(x).toEqual(y)
13+export const expectToEqualFailureImpl = actual => expected => () => {
14+ const constructorName = value => value?.constructor?.name ?? typeof value;
15+ throw new Error(
16+ `PureScript Eq assertion failed.\n` +
17+ `Expected (${constructorName(expected)}):\n${unsafeStringify(expected)}\n` +
18+ `Received (${constructorName(actual)}):\n${unsafeStringify(actual)}`
19+ );
20+}
1421
1522export const expectToSatisfyImpl = value => predicate => () => {
1623 const result = predicate(value);
src/ViTest/Expect.purs+4−2
@@ -15,10 +15,12 @@ infixl 1 expectToBe as ====
1515
1616foreign import unsafeStringify :: forall a. a -> String
1717
18−foreign import expectToEqualImpl :: forall a. a -> a -> Effect Unit
18+foreign import expectToEqualFailureImpl :: forall a. a -> a -> Effect Unit
1919
2020expectToEqual :: forall a. Eq a => a -> a -> Aff Unit
21−expectToEqual a b = liftEffect $ expectToEqualImpl a b
21+expectToEqual actual expected
22+ | actual == expected = pure unit
23+ | otherwise = liftEffect $ expectToEqualFailureImpl actual expected
2224
2325infixl 1 expectToEqual as ===
2426
test/Test/Main.purs+19
@@ -2,10 +2,28 @@ module Test.Main where
22
33import Prelude
44
5+import Control.Monad.Error.Class (throwError)
6+import Data.Either (Either(..))
57import Effect (Effect)
8+import Effect.Aff (attempt)
9+import Effect.Exception (error)
610import ViTest (describe, test, viTest)
11+import ViTest.Expect ((===))
712import ViTest.Expect.Snapshot (expectToMatchFileSnapshot)
813
14+data Direction = North | South
15+
16+derive instance Eq Direction
17+
18+equalitySpec :: Effect Unit
19+equalitySpec = describe "ViTest.Expect equality" do
20+ test "uses PureScript Eq for nullary constructors" do
21+ North === North
22+ result <- attempt (North === South)
23+ case result of
24+ Left _ -> pure unit
25+ Right _ -> throwError (error "North === South unexpectedly passed")
26+
927snapshotSpec :: Effect Unit
1028snapshotSpec = describe "ViTest.Expect.Snapshot" do
1129 test "writes the baseline on first run and diffs it on subsequent runs"
@@ -16,3 +34,4 @@ snapshotSpec = describe "ViTest.Expect.Snapshot" do
1634main :: Unit
1735main = viTest do
1836 snapshotSpec
37+ equalitySpec