Skip to content

Commit

Permalink
Merge pull request #10 from ahrefs/add-subtest
Browse files Browse the repository at this point in the history
Introduce a `subtest` function and make `Promise.test` return just `unit`
  • Loading branch information
jchavarri authored May 14, 2024
2 parents da32b11 + 6bd7e9a commit 2b805fd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Unreleased

- Add bindings for `node:test` and `node:assert/strict`
- Introduce a `subtest` function and make `Promise.test` return just `unit`,
[#10](https://github.com/ahrefs/melange-fest/pull/10)

# 0.1.0

- Add `Fest.ok`
- Add bindings for `node:test` and `node:assert/strict`
8 changes: 6 additions & 2 deletions src/fest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ module Promise = struct
(** {{:https://v2.ocaml.org/manual/bindingops.html }Monadic binding operator} for promises *)
let ( let* ) p f = Js.Promise.then_ f p

external test : string -> (unit -> unit Js.Promise.t) -> unit Js.Promise.t
external test : string -> (unit -> unit Js.Promise.t) -> unit = "test"
[@@mel.module "node:test"]
(** Create a top-level test with a given name and callback function that runs the test and returns a promise. *)

external subtest : string -> (unit -> unit Js.Promise.t) -> unit Js.Promise.t
= "test"
[@@mel.module "node:test"]
(** Create a test with a given name and callback function that runs the test and returns a promise. *)
(** Create a subtest with a given name and callback function that runs the test and returns a promise. It is supposed to be used inside a {!test} function call. *)
end

type assertion
Expand Down
13 changes: 5 additions & 8 deletions test/bindings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ module Promise = struct
open Fest.Promise

let inner input output =
test
subtest
("Math.defaultRound: " ^ Js.Float.toString input)
(fun () ->
expect
|> equal output (input |> Js.Math.round |> int_of_float |> string_of_int);
Js.Promise.resolve ())

let (_ : unit Js.Promise.t) =
let* () =
test "promise" (fun () ->
let* () = inner 0. "0" in
inner 0.055 "0")
in
Js.Promise.resolve ()
let () =
test "promise" (fun () ->
let* () = inner 0. "0" in
inner 0.055 "0")
end

0 comments on commit 2b805fd

Please sign in to comment.