Skip to content

Commit 7ddcfeb

Browse files
feat: adds test.for, it.for, and describe.for bindings (#28)
Note: - These have the test context passed as an argument, and preserves the array's tuple to be passed in as the first argument. This removes the need for the -N suffix bindings and allows use of the non-BuiltIn bindings - We should deprecate `Each.*` and remove it in the next major. - `describe.for` requires vitest v3+
1 parent af2c43b commit 7ddcfeb

2 files changed

Lines changed: 218 additions & 0 deletions

File tree

src/Vitest.res

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,117 @@ module Each: EachType = {
845845
)
846846
}
847847

848+
module type ForType = {
849+
let test: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => unit) => unit
850+
let testAsync: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => promise<unit>) => unit
851+
852+
let it: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => unit) => unit
853+
let itAsync: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => promise<unit>) => unit
854+
855+
let describe: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => unit) => unit
856+
let describeAsync: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => promise<unit>) => unit
857+
}
858+
859+
module For: ForType = {
860+
module Ext = {
861+
type test
862+
type it
863+
type describe
864+
865+
@module("vitest") @val
866+
external test: test = "test"
867+
868+
@module("vitest") @val
869+
external it: it = "it"
870+
871+
@module("vitest") @val
872+
external describe: describe = "describe"
873+
874+
@send
875+
external testObj: (
876+
~test: test,
877+
~cases: array<'a>,
878+
) => (~name: string, ~f: @uncurry ('a, testCtx) => unit, ~timeout: Js.undefined<int>) => unit =
879+
"for"
880+
881+
@send
882+
external testObjAsync: (
883+
~test: test,
884+
~cases: array<'a>,
885+
) => (
886+
~name: string,
887+
~f: @uncurry ('a, testCtx) => promise<unit>,
888+
~timeout: Js.undefined<int>,
889+
) => unit = "for"
890+
891+
@send
892+
external itObj: (
893+
~it: it,
894+
~cases: array<'a>,
895+
) => (~name: string, ~f: @uncurry ('a, testCtx) => unit, ~timeout: Js.undefined<int>) => unit =
896+
"for"
897+
898+
@send
899+
external itObjAsync: (
900+
~it: it,
901+
~cases: array<'a>,
902+
) => (
903+
~name: string,
904+
~f: @uncurry ('a, testCtx) => promise<unit>,
905+
~timeout: Js.undefined<int>,
906+
) => unit = "for"
907+
908+
@send
909+
external describeObj: (
910+
~describe: describe,
911+
~cases: array<'a>,
912+
) => (~name: string, ~f: @uncurry ('a, testCtx) => unit, ~timeout: Js.undefined<int>) => unit =
913+
"for"
914+
915+
@send
916+
external describeObjAsync: (
917+
~describe: describe,
918+
~cases: array<'a>,
919+
) => (
920+
~name: string,
921+
~f: @uncurry ('a, testCtx) => promise<unit>,
922+
~timeout: Js.undefined<int>,
923+
) => unit = "for"
924+
}
925+
926+
@inline
927+
let test = (cases, name, ~timeout=?, f) =>
928+
Ext.testObj(~test=Ext.test, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)
929+
930+
@inline
931+
let testAsync = (cases, name, ~timeout=?, f) =>
932+
Ext.testObjAsync(~test=Ext.test, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)
933+
934+
@inline
935+
let it = (cases, name, ~timeout=?, f) =>
936+
Ext.itObj(~it=Ext.it, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)
937+
938+
@inline
939+
let itAsync = (cases, name, ~timeout=?, f) =>
940+
Ext.itObjAsync(~it=Ext.it, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)
941+
942+
@inline
943+
let describe = (cases, name, ~timeout=?, f) =>
944+
Ext.describeObj(~describe=Ext.describe, ~cases)(
945+
~name,
946+
~f,
947+
~timeout=timeout->Js.Undefined.fromOption,
948+
)
949+
950+
@inline
951+
let describeAsync = (cases, name, ~timeout=?, f) =>
952+
Ext.describeObjAsync(~describe=Ext.describe, ~cases)(
953+
~name,
954+
~f,
955+
~timeout=timeout->Js.Undefined.fromOption,
956+
)
957+
}
958+
848959
module Todo = {
849960
type todo_describe
850961
type todo_test

tests/for.test.res

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
open Vitest
2+
3+
let sumObj = [{"a": 3, "b": 5, "sum": 8}, {"a": 6, "b": 2, "sum": 8}]
4+
let sum2 = [(1, "1"), (6, "6")]
5+
let sum3 = [(1, 3, "4"), (6, 3, "9")]
6+
let sum4 = [(1, 2, 8, "11"), (6, 3, 8, "17"), (5, 9, 2, "16")]
7+
let sum5 = [(1, 3, 8, 6, "18"), (6, 3, 2, 4, "15"), (5, 9, 2, 3, "19")]
8+
9+
For.test(sumObj, "test: sum $a+$b=$sum", (i, t) => {
10+
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
11+
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
12+
})
13+
14+
For.test(sum2, "test: %i=%s", ((a, b), t) => {
15+
expect(t, a->Js.Int.toString)->Expect.toBe(b)
16+
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
17+
})
18+
19+
For.test(sum3, "test: sum %i+%i=%s", ((a, b, sum), t) => {
20+
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
21+
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
22+
})
23+
24+
For.test(sum4, "test: sum %i+%i+%i=%s", ((a, b, c, sum), t) => {
25+
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
26+
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
27+
})
28+
29+
For.test(sum5, "test: sum %i+%i+%i+%i=%s", ((a, b, c, d, sum), t) => {
30+
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
31+
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
32+
})
33+
34+
For.testAsync(sumObj, "testAsync: sum $a+$b=$sum", async (i, t) => {
35+
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
36+
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
37+
})
38+
39+
For.testAsync(sum2, "testAsync: %i=%s", async ((a, b), t) => {
40+
expect(t, a->Js.Int.toString)->Expect.toBe(b)
41+
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
42+
})
43+
44+
For.testAsync(sum3, "testAsync: sum %i+%i=%s", async ((a, b, sum), t) => {
45+
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
46+
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
47+
})
48+
49+
For.testAsync(sum4, "testAsync: sum %i+%i+%i=%s", async ((a, b, c, sum), t) => {
50+
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
51+
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
52+
})
53+
54+
For.testAsync(sum5, "testAsync: sum %i+%i+%i+%i=%s", async ((a, b, c, d, sum), t) => {
55+
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
56+
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
57+
})
58+
59+
For.it(sumObj, "it: sum $a+$b=$sum", (i, t) => {
60+
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
61+
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
62+
})
63+
64+
For.it(sum2, "it: %i=%s", ((a, b), t) => {
65+
expect(t, a->Js.Int.toString)->Expect.toBe(b)
66+
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
67+
})
68+
69+
For.it(sum3, "it: sum %i+%i=%s", ((a, b, sum), t) => {
70+
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
71+
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
72+
})
73+
74+
For.it(sum4, "it: sum %i+%i+%i=%s", ((a, b, c, sum), t) => {
75+
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
76+
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
77+
})
78+
79+
For.it(sum5, "it: sum %i+%i+%i+%i=%s", ((a, b, c, d, sum), t) => {
80+
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
81+
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
82+
})
83+
84+
For.itAsync(sumObj, "itAsync: sum $a+$b=$sum", async (i, t) => {
85+
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
86+
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
87+
})
88+
89+
For.itAsync(sum2, "itAsync: %i=%s", async ((a, b), t) => {
90+
expect(t, a->Js.Int.toString)->Expect.toBe(b)
91+
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
92+
})
93+
94+
For.itAsync(sum3, "itAsync: sum %i+%i=%s", async ((a, b, sum), t) => {
95+
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
96+
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
97+
})
98+
99+
For.itAsync(sum4, "itAsync: sum %i+%i+%i=%s", async ((a, b, c, sum), t) => {
100+
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
101+
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
102+
})
103+
104+
For.itAsync(sum5, "itAsync: sum %i+%i+%i+%i=%s", async ((a, b, c, d, sum), t) => {
105+
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
106+
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
107+
})

0 commit comments

Comments
 (0)