git.iko.soy/rowtype-yoga/purescript-vitest
on main help sign in
Release v2.0.0: Breaking changes and new features
3b2035bMark
spago.yaml+11−13
@@ -1,8 +1,19 @@
1+workspace:
2+ packageSet:
3+ registry: 61.3.1
4+
1package:
5package:
2 name: vitest
6 name: vitest
7+ publish:
8+ version: 2.0.0
9+ license: WTFPL
10+ location:
11+ githubOwner: rowtype-yoga
12+ githubRepo: purescript-vitest
3 dependencies:
13 dependencies:
4 - aff: ">=7.0.0 <9.0.0"
14 - aff: ">=7.0.0 <9.0.0"
5 - effect: ">=4.0.0 <5.0.0"
15 - effect: ">=4.0.0 <5.0.0"
16+ - either: ">=6.1.0 <7.0.0"
6 - exceptions: ">=6.1.0 <7.0.0"
17 - exceptions: ">=6.1.0 <7.0.0"
7 - js-promise: ">=1.0.0 <2.0.0"
18 - js-promise: ">=1.0.0 <2.0.0"
8 - js-promise-aff: ">=1.0.0 <2.0.0"
19 - js-promise-aff: ">=1.0.0 <2.0.0"
@@ -13,16 +24,3 @@ package:
13 - unsafe-coerce: ">=6.0.0 <7.0.0"
24 - unsafe-coerce: ">=6.0.0 <7.0.0"
14 - web-dom: ">=6.0.0 <7.0.0"
25 - web-dom: ">=6.0.0 <7.0.0"
15 - web-html: ">=4.1.0 <5.0.0"
26 - web-html: ">=4.1.0 <5.0.0"
16− publish:
17− version: 1.0.0
18− license: WTFPL
19− location:
20− githubOwner: rowtype-yoga
21− githubRepo: purescript-vitest
22− test:
23− main: Test.Main
24− dependencies: []
25−workspace:
26− packageSet:
27− registry: 61.3.1
28− extraPackages: {}
src/ViTest.purs+6−5
@@ -9,15 +9,16 @@ import Effect.Unsafe (unsafePerformEffect)
9import Promise (Promise)
9import Promise (Promise)
10import Promise.Aff as Promise
10import Promise.Aff as Promise
11
11
12−foreign import data ViTest :: Type
12+type ViTest = Unit
13
13
14−foreign import testImpl :: EffectFn2 String (Effect (Promise (Unit))) ViTest
14+foreign import testImpl :: EffectFn2 String (Effect (Promise (Unit))) Unit
15+
15−test :: String -> Aff Unit -> Effect ViTest
16+test :: String -> Aff Unit -> Effect Unit
16test desc = runEffectFn2 testImpl desc <<< Promise.fromAff
17test desc = runEffectFn2 testImpl desc <<< Promise.fromAff
17
18
18−foreign import describeImpl :: EffectFn2 String (Effect ViTest) ViTest
19+foreign import describeImpl :: EffectFn2 String (Effect Unit) Unit
19
20
20−describe :: String -> Effect ViTest -> Effect ViTest
21+describe :: String -> Effect Unit -> Effect Unit
21describe = runEffectFn2 describeImpl
22describe = runEffectFn2 describeImpl
22
23
23viTest :: Effect ViTest -> ViTest
24viTest :: Effect ViTest -> ViTest
src/ViTest/Expect.js+17
@@ -2,3 +2,20 @@ import {expect} from "vitest";
2
2
3export const expectToBeImpl = x => y => () => expect(x).toBe(y)
3export const expectToBeImpl = x => y => () => expect(x).toBe(y)
4
4
5+export const unsafeStringify = x => {
6+ try {
7+ return JSON.stringify(x, null, 2);
8+ } catch (e) {
9+ return String(x);
10+ }
11+}
12+
13+export const expectToEqualImpl = x => y => () => expect(x).toEqual(y)
14+
15+export const expectToSatisfyImpl = value => predicate => () => {
16+ const result = predicate(value);
17+ if (!result) {
18+ throw new Error(`Expected value to satisfy predicate, but it didn't.\nValue: ${unsafeStringify(value)}`);
19+ }
20+}
21+
src/ViTest/Expect.purs+14
@@ -12,3 +12,17 @@ expectToBe :: forall a. a -> a -> Aff Unit
12expectToBe a b = liftEffect $ expectToBeImpl a b
12expectToBe a b = liftEffect $ expectToBeImpl a b
13
13
14infixl 1 expectToBe as ====
14infixl 1 expectToBe as ====
15+
16+foreign import unsafeStringify :: forall a. a -> String
17+
18+foreign import expectToEqualImpl :: forall a. a -> a -> Effect Unit
19+
20+expectToEqual :: forall a. Eq a => a -> a -> Aff Unit
21+expectToEqual a b = liftEffect $ expectToEqualImpl a b
22+
23+infixl 1 expectToEqual as ===
24+
25+foreign import expectToSatisfyImpl :: forall a. a -> (a -> Boolean) -> Effect Unit
26+
27+expectToSatisfy :: forall a. a -> (a -> Boolean) -> Aff Unit
28+expectToSatisfy value predicate = liftEffect $ expectToSatisfyImpl value predicate
src/ViTest/Expect/Either.jsnew file+1
@@ -0,0 +1 @@
1+export const indexOfImpl = needle => haystack => haystack.indexOf(needle);
src/ViTest/Expect/Either.pursnew file+48
@@ -0,0 +1,48 @@
1+module ViTest.Expect.Either where
2+
3+import Prelude
4+
5+import Data.Either (Either(..), either)
6+import Effect.Aff (Aff)
7+import ViTest.Expect (expectToBe, expectToEqual)
8+
9+-- | Assert that a value is Right with a specific value
10+expectRight :: forall e a. Eq a => Show e => a -> Either e a -> Aff Unit
11+expectRight expected result = case result of
12+ Right actual -> expectToEqual expected actual
13+ Left err -> expectToBe true false -- Always fails, shows error in output
14+
15+-- | Assert that a value is Right (don't care about the value)
16+expectIsRight :: forall e a. Show e => Either e a -> Aff Unit
17+expectIsRight result = case result of
18+ Right _ -> expectToBe true true
19+ Left err -> expectToBe true false
20+
21+-- | Assert that a value is Left with a specific error
22+expectLeft :: forall e a. Eq e => Show a => e -> Either e a -> Aff Unit
23+expectLeft expected result = case result of
24+ Left actual -> expectToEqual expected actual
25+ Right val -> expectToBe true false
26+
27+-- | Assert that a value is Left (don't care about the error)
28+expectIsLeft :: forall e a. Show a => Either e a -> Aff Unit
29+expectIsLeft result = case result of
30+ Left _ -> expectToBe true true
31+ Right val -> expectToBe true false
32+
33+-- | Assert that a Left contains a specific substring in its error
34+expectLeftContains :: forall a. Show a => String -> Either String a -> Aff Unit
35+expectLeftContains needle result = case result of
36+ Left err ->
37+ if contains needle err
38+ then expectToBe true true
39+ else expectToBe true false -- Fails with message about not containing
40+ Right val -> expectToBe true false
41+ where
42+ contains :: String -> String -> Boolean
43+ contains n h = indexOf n h >= 0
44+
45+ indexOf :: String -> String -> Int
46+ indexOf = indexOfImpl
47+
48+foreign import indexOfImpl :: String -> String -> Int