Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions src/Vitest.res
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,117 @@ module Each: EachType = {
)
}

module type ForType = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add describe sets too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

It looks like it was introduced in vitest v3: vitest-dev/vitest#7253, should I add it with a warning that it's not compatible with v2?

Copy link
Owner

@cometkim cometkim May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should bump vitest version in the next release.

It doesn't have breaking changes that could affect rescript-vitest users? Let me check peerDeps and histories first.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://vitest.dev/guide/migration#vitest-3

There are some breaking changes, but mostly I can handle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, when you go to bump it for devDependencies I forgot to push these tests:

For.describe(sumObj, "sum $a+$b=$sum", (i, t) => {
  expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
  expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
})

For.describe(sum2, "%i=%s", ((a, b), t) => {
  expect(t, a->Js.Int.toString)->Expect.toBe(b)
  expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
})

For.describe(sum3, "sum %i+%i=%s", ((a, b, sum), t) => {
  expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
  expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.describe(sum4, "sum %i+%i+%i=%s", ((a, b, c, sum), t) => {
  expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
  expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.describe(sum5, "sum %i+%i+%i+%i=%s", ((a, b, c, d, sum), t) => {
  expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
  expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.describeAsync(sumObj, "sum $a+$b=$sum", async (i, t) => {
  expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
  expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
})

For.describeAsync(sum2, "%i=%s", async ((a, b), t) => {
  expect(t, a->Js.Int.toString)->Expect.toBe(b)
  expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
})

For.describeAsync(sum3, "sum %i+%i=%s", async ((a, b, sum), t) => {
  expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
  expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.describeAsync(sum4, "sum %i+%i+%i=%s", async ((a, b, c, sum), t) => {
  expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
  expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.describeAsync(sum5, "sum %i+%i+%i+%i=%s", async ((a, b, c, d, sum), t) => {
  expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
  expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, descritbe has no test context

let test: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => unit) => unit
let testAsync: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => promise<unit>) => unit

let it: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => unit) => unit
let itAsync: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => promise<unit>) => unit

let describe: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => unit) => unit
let describeAsync: (array<'a>, string, ~timeout: int=?, ('a, testCtx) => promise<unit>) => unit
}

module For: ForType = {
module Ext = {
type test
type it
type describe

@module("vitest") @val
external test: test = "test"

@module("vitest") @val
external it: it = "it"

@module("vitest") @val
external describe: describe = "describe"

@send
external testObj: (
~test: test,
~cases: array<'a>,
) => (~name: string, ~f: @uncurry ('a, testCtx) => unit, ~timeout: Js.undefined<int>) => unit =
"for"

@send
external testObjAsync: (
~test: test,
~cases: array<'a>,
) => (
~name: string,
~f: @uncurry ('a, testCtx) => promise<unit>,
~timeout: Js.undefined<int>,
) => unit = "for"

@send
external itObj: (
~it: it,
~cases: array<'a>,
) => (~name: string, ~f: @uncurry ('a, testCtx) => unit, ~timeout: Js.undefined<int>) => unit =
"for"

@send
external itObjAsync: (
~it: it,
~cases: array<'a>,
) => (
~name: string,
~f: @uncurry ('a, testCtx) => promise<unit>,
~timeout: Js.undefined<int>,
) => unit = "for"

@send
external describeObj: (
~describe: describe,
~cases: array<'a>,
) => (~name: string, ~f: @uncurry ('a, testCtx) => unit, ~timeout: Js.undefined<int>) => unit =
"for"

@send
external describeObjAsync: (
~describe: describe,
~cases: array<'a>,
) => (
~name: string,
~f: @uncurry ('a, testCtx) => promise<unit>,
~timeout: Js.undefined<int>,
) => unit = "for"
}

@inline
let test = (cases, name, ~timeout=?, f) =>
Ext.testObj(~test=Ext.test, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)

@inline
let testAsync = (cases, name, ~timeout=?, f) =>
Ext.testObjAsync(~test=Ext.test, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)

@inline
let it = (cases, name, ~timeout=?, f) =>
Ext.itObj(~it=Ext.it, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)

@inline
let itAsync = (cases, name, ~timeout=?, f) =>
Ext.itObjAsync(~it=Ext.it, ~cases)(~name, ~f, ~timeout=timeout->Js.Undefined.fromOption)

@inline
let describe = (cases, name, ~timeout=?, f) =>
Ext.describeObj(~describe=Ext.describe, ~cases)(
~name,
~f,
~timeout=timeout->Js.Undefined.fromOption,
)

@inline
let describeAsync = (cases, name, ~timeout=?, f) =>
Ext.describeObjAsync(~describe=Ext.describe, ~cases)(
~name,
~f,
~timeout=timeout->Js.Undefined.fromOption,
)
}

module Todo = {
type todo_describe
type todo_test
Expand Down
107 changes: 107 additions & 0 deletions tests/for.test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
open Vitest

let sumObj = [{"a": 3, "b": 5, "sum": 8}, {"a": 6, "b": 2, "sum": 8}]
let sum2 = [(1, "1"), (6, "6")]
let sum3 = [(1, 3, "4"), (6, 3, "9")]
let sum4 = [(1, 2, 8, "11"), (6, 3, 8, "17"), (5, 9, 2, "16")]
let sum5 = [(1, 3, 8, 6, "18"), (6, 3, 2, 4, "15"), (5, 9, 2, 3, "19")]

For.test(sumObj, "test: sum $a+$b=$sum", (i, t) => {
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
})

For.test(sum2, "test: %i=%s", ((a, b), t) => {
expect(t, a->Js.Int.toString)->Expect.toBe(b)
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
})

For.test(sum3, "test: sum %i+%i=%s", ((a, b, sum), t) => {
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.test(sum4, "test: sum %i+%i+%i=%s", ((a, b, c, sum), t) => {
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.test(sum5, "test: sum %i+%i+%i+%i=%s", ((a, b, c, d, sum), t) => {
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.testAsync(sumObj, "testAsync: sum $a+$b=$sum", async (i, t) => {
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
})

For.testAsync(sum2, "testAsync: %i=%s", async ((a, b), t) => {
expect(t, a->Js.Int.toString)->Expect.toBe(b)
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
})

For.testAsync(sum3, "testAsync: sum %i+%i=%s", async ((a, b, sum), t) => {
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.testAsync(sum4, "testAsync: sum %i+%i+%i=%s", async ((a, b, c, sum), t) => {
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.testAsync(sum5, "testAsync: sum %i+%i+%i+%i=%s", async ((a, b, c, d, sum), t) => {
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.it(sumObj, "it: sum $a+$b=$sum", (i, t) => {
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
})

For.it(sum2, "it: %i=%s", ((a, b), t) => {
expect(t, a->Js.Int.toString)->Expect.toBe(b)
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
})

For.it(sum3, "it: sum %i+%i=%s", ((a, b, sum), t) => {
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.it(sum4, "it: sum %i+%i+%i=%s", ((a, b, c, sum), t) => {
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.it(sum5, "it: sum %i+%i+%i+%i=%s", ((a, b, c, d, sum), t) => {
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.itAsync(sumObj, "itAsync: sum $a+$b=$sum", async (i, t) => {
expect(t, i["a"] + i["b"])->Expect.toBe(i["sum"])
expect(t, i["a"] + i["b"] + 1)->Expect.not->Expect.toBe(i["sum"])
})

For.itAsync(sum2, "itAsync: %i=%s", async ((a, b), t) => {
expect(t, a->Js.Int.toString)->Expect.toBe(b)
expect(t, (a + 1)->Js.Int.toString)->Expect.not->Expect.toBe(b)
})

For.itAsync(sum3, "itAsync: sum %i+%i=%s", async ((a, b, sum), t) => {
expect(t, (a + b)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.itAsync(sum4, "itAsync: sum %i+%i+%i=%s", async ((a, b, c, sum), t) => {
expect(t, (a + b + c)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + c + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})

For.itAsync(sum5, "itAsync: sum %i+%i+%i+%i=%s", async ((a, b, c, d, sum), t) => {
expect(t, (a + b + c + d)->Js.Int.toString)->Expect.toBe(sum)
expect(t, (a + b + c + d + 1)->Js.Int.toString)->Expect.not->Expect.toBe(sum)
})