diff --git a/dune-project b/dune-project index 9f70928..5385cdd 100644 --- a/dune-project +++ b/dune-project @@ -6,7 +6,7 @@ (generate_opam_files true) -(maintainers "Javier Ch\195\161varri ") +(maintainers "Javier Chávarri ") (authors "glennsl glennsl@users.noreply.github.com" @@ -27,6 +27,6 @@ (description "Melange bindings for Jest JavaScript library.") (depends (melange - (>= "2.0.0")) + (>= "5.0.0")) (reason :with-test) (melange-webapi :with-test))) diff --git a/jest-dom/__tests__/jest_dom_test.ml b/jest-dom/__tests__/jest_dom_test.ml index 2e4dd39..52ea484 100644 --- a/jest-dom/__tests__/jest_dom_test.ml +++ b/jest-dom/__tests__/jest_dom_test.ml @@ -1,10 +1,9 @@ -[@@@mel.config { flags = [| "--preamble"; -{| +[@@@mel.config +{ flags = [| "--preamble"; {| /** * @jest-environment jsdom */ -|} -|] }] +|} |] }] open Jest open JestDom diff --git a/jest-dom/jestDom.ml b/jest-dom/jestDom.ml index f55c176..c34c673 100644 --- a/jest-dom/jestDom.ml +++ b/jest-dom/jestDom.ml @@ -13,7 +13,7 @@ module TextContent = struct type options = < normalizeWhitespace : bool Js.undefined > Js.t external makeOptions : ?normalizeWhitespace:bool -> unit -> options = "" - [@@mel.obj] + [@@mel.obj] end external expect : t -> expect = "expect" @@ -22,61 +22,68 @@ external not_ : expect -> expect = "not" [@@mel.get] let pass : (expect -> unit) -> expect -> Jest.assertion = fun assertion expect -> expect |. assertion |. fun _ -> Jest.pass -external _toBeDisabled : unit = "toBeDisabled" [@@mel.send.pipe: expect] +external _toBeDisabled : (expect[@mel.this]) -> unit = "toBeDisabled" +[@@mel.send] let toBeDisabled = _toBeDisabled |. pass -external _toBeEnabled : unit = "toBeEnabled" [@@mel.send.pipe: expect] +external _toBeEnabled : (expect[@mel.this]) -> unit = "toBeEnabled" [@@mel.send] let toBeEnabled = _toBeEnabled |. pass -external _toBeEmptyDOMElement : unit = "toBeEmptyDOMElement" - [@@mel.send.pipe: expect] +external _toBeEmptyDOMElement : (expect[@mel.this]) -> unit + = "toBeEmptyDOMElement" +[@@mel.send] let toBeEmptyDOMElement = _toBeEmptyDOMElement |. pass -external _toBeInTheDocument : unit = "toBeInTheDocument" - [@@mel.send.pipe: expect] +external _toBeInTheDocument : (expect[@mel.this]) -> unit = "toBeInTheDocument" +[@@mel.send] let toBeInTheDocument = _toBeInTheDocument |. pass -external _toBeInvalid : unit = "toBeInvalid" [@@mel.send.pipe: expect] +external _toBeInvalid : (expect[@mel.this]) -> unit = "toBeInvalid" [@@mel.send] let toBeInvalid = _toBeInvalid |. pass -external _toBeRequired : unit = "toBeRequired" [@@mel.send.pipe: expect] +external _toBeRequired : (expect[@mel.this]) -> unit = "toBeRequired" +[@@mel.send] let toBeRequired = _toBeRequired |. pass -external _toBeValid : unit = "toBeValid" [@@mel.send.pipe: expect] +external _toBeValid : (expect[@mel.this]) -> unit = "toBeValid" [@@mel.send] let toBeValid = _toBeValid |. pass -external _toBeVisible : unit = "toBeVisible" [@@mel.send.pipe: expect] +external _toBeVisible : (expect[@mel.this]) -> unit = "toBeVisible" [@@mel.send] let toBeVisible = _toBeVisible |. pass -external _toContainElement : t Js.nullable -> unit = "toContainElement" - [@@mel.send.pipe: expect] +external _toContainElement : t Js.nullable -> (expect[@mel.this]) -> unit + = "toContainElement" +[@@mel.send] let toContainElement element = element |. Js.Nullable.fromOption |. _toContainElement |. pass -external _toContainHTML : string -> unit = "toContainHTML" - [@@mel.send.pipe: expect] +external _toContainHTML : string -> (expect[@mel.this]) -> unit + = "toContainHTML" +[@@mel.send] let toContainHTML html = html |. _toContainHTML |. pass -external _toHaveAttribute : string -> string Js.undefined -> unit +external _toHaveAttribute : + string -> string Js.undefined -> (expect[@mel.this]) -> unit = "toHaveAttribute" - [@@mel.send.pipe: expect] +[@@mel.send] let toHaveAttribute attribute ?value = _toHaveAttribute attribute (Js.Undefined.fromOption value) |. pass -external _toHaveClass : string -> HaveClass.options Js.undefined -> unit +external _toHaveClass : + string -> HaveClass.options Js.undefined -> (expect[@mel.this]) -> unit = "toHaveClass" - [@@mel.send.pipe: expect] +[@@mel.send] let toHaveClass class_ ?options = _toHaveClass @@ -84,56 +91,65 @@ let toHaveClass class_ ?options = (Js.Undefined.fromOption options) |. pass -external _toHaveFocus : unit = "toHaveFocus" [@@mel.send.pipe: expect] +external _toHaveFocus : (expect[@mel.this]) -> unit = "toHaveFocus" [@@mel.send] let toHaveFocus = _toHaveFocus |. pass -external _toHaveFormValues : < .. > Js.t -> unit = "toHaveFormValues" - [@@mel.send.pipe: expect] +external _toHaveFormValues : < .. > Js.t -> (expect[@mel.this]) -> unit + = "toHaveFormValues" +[@@mel.send] let toHaveFormValues values = values |. _toHaveFormValues |. pass external _toHaveStyle : - ([ `Str of string | `Obj of < .. > Js.t ][@mel.unwrap]) -> unit = "toHaveStyle" - [@@mel.send.pipe: expect] + ([ `Str of string | `Obj of < .. > Js.t ][@mel.unwrap]) -> + (expect[@mel.this]) -> + unit = "toHaveStyle" +[@@mel.send] let toHaveStyle style = style |. _toHaveStyle |. pass external _toHaveTextContent : ([ `Str of string | `RegExp of Js.Re.t ][@mel.unwrap]) -> TextContent.options Js.undefined -> + (expect[@mel.this]) -> unit = "toHaveTextContent" - [@@mel.send.pipe: expect] +[@@mel.send] let toHaveTextContent content ?options = _toHaveTextContent content (Js.Undefined.fromOption options) |. pass external _toHaveValue : - ([ `Str of string | `Arr of string array | `Num of int ][@mel.unwrap]) -> unit - = "toHaveValue" - [@@mel.send.pipe: expect] + ([ `Str of string | `Arr of string array | `Num of int ][@mel.unwrap]) -> + (expect[@mel.this]) -> + unit = "toHaveValue" +[@@mel.send] let toHaveValue value = value |. _toHaveValue |. pass external _toHaveDisplayValue : ([ `Str of string | `RegExp of Js.Re.t | `Arr of string array ][@mel.unwrap]) -> + (expect[@mel.this]) -> unit = "toHaveDisplayValue" - [@@mel.send.pipe: expect] +[@@mel.send] let toHaveDisplayValue value = value |. _toHaveDisplayValue |. pass -external _toBeChecked : unit = "toBeChecked" [@@mel.send.pipe: expect] +external _toBeChecked : (expect[@mel.this]) -> unit = "toBeChecked" [@@mel.send] let toBeChecked = _toBeChecked |. pass -external _toBePartiallyChecked : unit = "toBePartiallyChecked" - [@@mel.send.pipe: expect] +external _toBePartiallyChecked : (expect[@mel.this]) -> unit + = "toBePartiallyChecked" +[@@mel.send] let toBePartiallyChecked = _toBePartiallyChecked |. pass external _toHaveAccessibleDescription : - ([ `Str of string | `RegExp of Js.Re.t ][@mel.unwrap]) -> unit - = "toHaveAccessibleDescription" - [@@mel.send.pipe: expect] + ([ `Str of string | `RegExp of Js.Re.t ][@mel.unwrap]) -> + (expect[@mel.this]) -> + unit = "toHaveAccessibleDescription" +[@@mel.send] -let toHaveAccessibleDescription content = content |. _toHaveAccessibleDescription |. pass +let toHaveAccessibleDescription content = + content |. _toHaveAccessibleDescription |. pass diff --git a/jest-dom/jestDom.mli b/jest-dom/jestDom.mli index ccf3aa2..c1c21e2 100644 --- a/jest-dom/jestDom.mli +++ b/jest-dom/jestDom.mli @@ -11,7 +11,7 @@ module TextContent : sig type options = < normalizeWhitespace : bool Js.undefined > Js.t external makeOptions : ?normalizeWhitespace:bool -> unit -> options = "" - [@@mel.obj] + [@@mel.obj] end external expect : t -> expect = "expect" diff --git a/jest/__tests__/expect_test.ml b/jest/__tests__/expect_test.ml index 83de35a..072695c 100644 --- a/jest/__tests__/expect_test.ml +++ b/jest/__tests__/expect_test.ml @@ -1,48 +1,35 @@ open Jest -type test_record = { - value: string -} +type test_record = { value : string } let () = + describe "Expect" (fun () -> + let open Expect in + test "toBe" (fun () -> expect (1 + 2) |> toBe 3); + test "toBeCloseTo" (fun () -> expect (1. +. 2.) |> toBeCloseTo 3.); + test "toBeSoCloseTo" (fun () -> + expect (1. +. 2.123) |> toBeSoCloseTo 3.123 ~digits:3); + test "toBeGreaterThan" (fun () -> expect 4 |> toBeGreaterThan 3); + test "toBeGreaterThanOrEqual" (fun () -> + expect 4 |> toBeGreaterThanOrEqual 4); + test "toBeLessThan" (fun () -> expect 4 |> toBeLessThan 5); + test "toBeLessThanOrEqual" (fun () -> expect 4 |> toBeLessThanOrEqual 4); + test "toBeSuperSetOf" (fun () -> + expect [| "a"; "b"; "c" |] |> toBeSupersetOf [| "a"; "c" |]); + test "toContain" (fun () -> expect [| "a"; "b"; "c" |] |> toContain "b"); + test "toContainEqual" (fun () -> + expect [| { value = "a" }; { value = "b" }; { value = "c" } |] + |> toContainEqual { value = "b" }); + test "toContainString" (fun () -> + expect "banana" |> toContainString "nana"); + test "toHaveLength" (fun () -> + expect [| "a"; "b"; "c" |] |> toHaveLength 3); + test "toEqual" (fun () -> expect (1 + 2) |> toEqual 3); + test "toMatch" (fun () -> expect "banana" |> toMatch "nana"); + test "toMatchRe" (fun () -> expect "banana" |> toMatchRe [%re "/ana/"]); + test "toThrow" (fun () -> expect (fun () -> assert false) |> toThrow); -describe "Expect" (fun () -> - let open Expect in - - test "toBe" (fun () -> - expect (1 + 2) |> toBe 3); - test "toBeCloseTo" (fun () -> - expect (1. +. 2.) |> toBeCloseTo 3.); - test "toBeSoCloseTo" (fun () -> - expect (1. +. 2.123) |> toBeSoCloseTo 3.123 ~digits:3); - test "toBeGreaterThan" (fun () -> - expect 4 |> toBeGreaterThan 3); - test "toBeGreaterThanOrEqual" (fun () -> - expect 4 |> toBeGreaterThanOrEqual 4); - test "toBeLessThan" (fun () -> - expect 4 |> toBeLessThan 5); - test "toBeLessThanOrEqual" (fun () -> - expect 4 |> toBeLessThanOrEqual 4); - test "toBeSuperSetOf" (fun () -> - expect [| "a"; "b"; "c" |] |> toBeSupersetOf [| "a"; "c" |]); - test "toContain" (fun () -> - expect [| "a"; "b"; "c" |] |> toContain "b"); - test "toContainEqual" (fun () -> - expect [| {value = "a"}; {value = "b"}; {value = "c"} |] |> toContainEqual {value = "b"}); - test "toContainString" (fun () -> - expect "banana" |> toContainString "nana"); - test "toHaveLength" (fun () -> - expect [| "a"; "b"; "c" |] |> toHaveLength 3); - test "toEqual" (fun () -> - expect (1 + 2) |> toEqual 3); - test "toMatch" (fun () -> - expect "banana" |> toMatch "nana"); - test "toMatchRe" (fun () -> - expect "banana" |> toMatchRe [%re "/ana/"]); - test "toThrow" (fun () -> - expect (fun () -> assert false) |> toThrow); - - (* test "toMatchInlineSnapshot" (fun () -> + (* test "toMatchInlineSnapshot" (fun () -> expect "foo" |> toMatchInlineSnapshot "\"foo\""); test "toMatchSnapshot" (fun () -> expect "foo" |> toMatchSnapshot); @@ -50,96 +37,80 @@ describe "Expect" (fun () -> expect "foo" |> toMatchSnapshotWithName "bar"); test "toThrowErrorMatchingSnapshot" (fun () -> expect (fun () -> Js.Exn.raiseError "foo error") |> toThrowErrorMatchingSnapshot); *) + test "not toBe" (fun () -> expect (1 + 2) |> not_ |> toBe 4); + test "not toBeCloseTo" (fun () -> + expect (1. +. 2.) |> not_ |> toBeCloseTo 4.); + test "not toBeSoCloseTo" (fun () -> + expect (1. +. 2.123) |> not_ |> toBeSoCloseTo 3.12 ~digits:3); + test "not toBeGreaterThan" (fun () -> + expect 4 |> not_ |> toBeGreaterThan 4); + test "not toBeGreaterThanOrEqual" (fun () -> + expect 4 |> not_ |> toBeGreaterThanOrEqual 5); + test "not toBeLessThan" (fun () -> expect 4 |> not_ |> toBeLessThan 4); + test "not toBeLessThanOrEqual" (fun () -> + expect 4 |> not_ |> toBeLessThanOrEqual 3); + test "not toBeSuperSetOf" (fun () -> + expect [| "a"; "b"; "c" |] |> not_ |> toBeSupersetOf [| "a"; "d" |]); + test "not toContain" (fun () -> + expect [| "a"; "b"; "c" |] |> not_ |> toContain "d"); + test "not toContainEqual" (fun () -> + expect [| { value = "a" }; { value = "b" }; { value = "c" } |] + |> not_ + |> toContainEqual { value = "d" }); + test "not toContainString" (fun () -> + expect "banana" |> not_ |> toContainString "nanan"); + test "not toHaveLength" (fun () -> + expect [| "a"; "b"; "c" |] |> not_ |> toHaveLength 2); + test "not toEqual" (fun () -> expect (1 + 2) |> not_ |> toEqual 4); + test "not toMatch" (fun () -> expect "banana" |> not_ |> toMatch "nanan"); + test "not toMatchRe" (fun () -> + expect "banana" |> not_ |> toMatchRe [%re "/anas/"]); + test "not toThrow" (fun () -> expect (fun () -> 2) |> not_ |> toThrow); - test "not toBe" (fun () -> - expect (1 + 2) |> not_ |> toBe 4); - test "not toBeCloseTo" (fun () -> - expect (1. +. 2.) |> not_ |> toBeCloseTo 4.); - test "not toBeSoCloseTo" (fun () -> - expect (1. +. 2.123) |> not_ |> toBeSoCloseTo 3.12 ~digits:3); - test "not toBeGreaterThan" (fun () -> - expect 4 |> not_ |> toBeGreaterThan 4); - test "not toBeGreaterThanOrEqual" (fun () -> - expect 4 |> not_ |> toBeGreaterThanOrEqual 5); - test "not toBeLessThan" (fun () -> - expect 4 |> not_ |> toBeLessThan 4); - test "not toBeLessThanOrEqual" (fun () -> - expect 4 |> not_ |> toBeLessThanOrEqual 3); - test "not toBeSuperSetOf" (fun () -> - expect [| "a"; "b"; "c" |] |> not_ |> toBeSupersetOf [| "a"; "d" |]); - test "not toContain" (fun () -> - expect [| "a"; "b"; "c" |] |> not_ |> toContain "d"); - test "not toContainEqual" (fun () -> - expect [| {value = "a"}; {value = "b"}; {value = "c"} |] |> not_ |> toContainEqual {value = "d"}); - test "not toContainString" (fun () -> - expect "banana" |> not_ |> toContainString "nanan"); - test "not toHaveLength" (fun () -> - expect [| "a"; "b"; "c" |] |> not_ |> toHaveLength 2); - test "not toEqual" (fun () -> - expect (1 + 2) |> not_ |> toEqual 4); - test "not toMatch" (fun () -> - expect "banana" |> not_ |> toMatch "nanan"); - test "not toMatchRe" (fun () -> - expect "banana" |> not_ |> toMatchRe [%re "/anas/"]); - test "not toThrow" (fun () -> - expect (fun () -> 2) |> not_ |> toThrow); - - test "expectFn" (fun () -> - expectFn raise (Invalid_argument "foo") |> toThrow); -); - -describe "Expect.Operators" (fun () -> - let open Expect in - let open! Expect.Operators in - - test "==" (fun () -> - expect (1 + 2) == 3); - test ">" (fun () -> - expect 4 > 3); - test ">=" (fun () -> - expect 4 >= 4); - test "<" (fun () -> - expect 4 < 5); - test "<=" (fun () -> - expect 4 <= 4); - test "=" (fun () -> - expect (1 + 2) = 3); - test "<>" (fun () -> - expect (1 + 2) <> 4); - test "!=" (fun () -> - expect (1 + 2) != 4); -); + test "expectFn" (fun () -> + expectFn raise (Invalid_argument "foo") |> toThrow)); -describe "ExpectJs" (fun () -> - let open ExpectJs in + describe "Expect.Operators" (fun () -> + let open Expect in + let open! Expect.Operators in + test "==" (fun () -> expect (1 + 2) == 3); + test ">" (fun () -> expect 4 > 3); + test ">=" (fun () -> expect 4 >= 4); + test "<" (fun () -> expect 4 < 5); + test "<=" (fun () -> expect 4 <= 4); + test "=" (fun () -> expect (1 + 2) = 3); + test "<>" (fun () -> expect (1 + 2) <> 4); + test "!=" (fun () -> expect (1 + 2) != 4)); - test "toBeDefined" (fun () -> - expect (Js.Undefined.return 3) |> toBeDefined); - test "toBeFalsy" (fun () -> - expect nan |> toBeFalsy); - test "toBeNull" (fun () -> - expect Js.null |> toBeNull); - test "toBeTruthy" (fun () -> - expect [||] |> toBeTruthy); - test "toBeUndefined" (fun () -> - expect Js.Undefined.empty |> toBeUndefined); - test "toContainProperties" (fun () -> - expect [%obj { foo = 0; bar = true }] |> toContainProperties [| "foo"; "bar" |]); - test "toMatchObject" (fun () -> - expect [%obj { a = 1; b = 2; c = 3 }] |> toMatchObject [%obj { a = 1; b = 2 }]); + describe "ExpectJs" (fun () -> + let open ExpectJs in + test "toBeDefined" (fun () -> + expect (Js.Undefined.return 3) |> toBeDefined); + test "toBeFalsy" (fun () -> expect nan |> toBeFalsy); + test "toBeNull" (fun () -> expect Js.null |> toBeNull); + test "toBeTruthy" (fun () -> expect [||] |> toBeTruthy); + test "toBeUndefined" (fun () -> + expect Js.Undefined.empty |> toBeUndefined); + test "toContainProperties" (fun () -> + expect [%obj { foo = 0; bar = true }] + |> toContainProperties [| "foo"; "bar" |]); + test "toMatchObject" (fun () -> + expect [%obj { a = 1; b = 2; c = 3 }] + |> toMatchObject [%obj { a = 1; b = 2 }]); - test "not toBeDefined" (fun () -> - expect (Js.undefined) |> not_ |> toBeDefined); - test "not toBeFalsy" (fun () -> - expect [||] |> not_ |> toBeFalsy); - test "not toBeNull" (fun () -> - expect (Js.Null.return 4) |> not_ |> toBeNull); - test "not toBeTruthy" (fun () -> - expect nan |> not_ |> toBeTruthy); - test "not toBeUndefined" (fun () -> - expect (Js.Undefined.return 4) |> not_ |> toBeUndefined); - test "not toContainProperties" (fun () -> - expect [%obj { foo = 0; bar = true }] |> not_ |> toContainProperties [| "foo"; "zoo" |]); - test "not toMatchObject" (fun () -> - expect [%obj { a = 1; b = 2; c = 3 }] |> not_ |> toMatchObject [%obj { a = 1; c = 2 }]); -); + test "not toBeDefined" (fun () -> + expect Js.undefined |> not_ |> toBeDefined); + test "not toBeFalsy" (fun () -> expect [||] |> not_ |> toBeFalsy); + test "not toBeNull" (fun () -> + expect (Js.Null.return 4) |> not_ |> toBeNull); + test "not toBeTruthy" (fun () -> expect nan |> not_ |> toBeTruthy); + test "not toBeUndefined" (fun () -> + expect (Js.Undefined.return 4) |> not_ |> toBeUndefined); + test "not toContainProperties" (fun () -> + expect [%obj { foo = 0; bar = true }] + |> not_ + |> toContainProperties [| "foo"; "zoo" |]); + test "not toMatchObject" (fun () -> + expect [%obj { a = 1; b = 2; c = 3 }] + |> not_ + |> toMatchObject [%obj { a = 1; c = 2 }])) diff --git a/jest/__tests__/globals_only_test.ml b/jest/__tests__/globals_only_test.ml index 0eada80..20b0192 100644 --- a/jest/__tests__/globals_only_test.ml +++ b/jest/__tests__/globals_only_test.ml @@ -1,24 +1,20 @@ open Jest let () = - Only.test "Only.test" (fun () -> pass); - Only.testAsync "Only.testAsync" (fun finish -> - finish pass); - Only.testAsync "testAsync - timeout ok" ~timeout:1 (fun finish -> - finish pass); + Only.testAsync "Only.testAsync" (fun finish -> finish pass); + Only.testAsync "testAsync - timeout ok" ~timeout:1 (fun finish -> finish pass); - Only.testPromise "Only.testPromise" (fun () -> - Js.Promise.resolve pass); + Only.testPromise "Only.testPromise" (fun () -> Js.Promise.resolve pass); Only.testPromise "testPromise - timeout ok" ~timeout:1 (fun () -> - Js.Promise.resolve pass); + Js.Promise.resolve pass); - Only.testAll "testAll" ["foo"; "bar"; "baz"] (fun input -> - if Js.String.length input == 3 then pass else fail ""); - Only.testAll "testAll - tuples" [("foo", 3); ("barbaz", 6); ("bananas!", 8)] (fun (input, output) -> - if Js.String.length input == output then pass else fail ""); + Only.testAll "testAll" [ "foo"; "bar"; "baz" ] (fun input -> + if Js.String.length input == 3 then pass else fail ""); + Only.testAll "testAll - tuples" + [ ("foo", 3); ("barbaz", 6); ("bananas!", 8) ] + (fun (input, output) -> + if Js.String.length input == output then pass else fail ""); - Only.describe "Only.describe" (fun () -> - test "some aspect" (fun () -> pass) - ); \ No newline at end of file + Only.describe "Only.describe" (fun () -> test "some aspect" (fun () -> pass)) diff --git a/jest/__tests__/globals_test.ml b/jest/__tests__/globals_test.ml index a7aa40e..4160f41 100644 --- a/jest/__tests__/globals_test.ml +++ b/jest/__tests__/globals_test.ml @@ -1,372 +1,339 @@ open Jest let () = - test "pass" (fun () -> - pass); + test "pass" (fun () -> pass); - Skip.test "fail" (fun () -> - fail ""); + Skip.test "fail" (fun () -> fail ""); + test "test" (fun () -> pass); - test "test" (fun () -> - pass); - - Skip.test "test - expect fail" (fun () -> - fail ""); - + Skip.test "test - expect fail" (fun () -> fail ""); + + testAsync "testAsync" (fun finish -> finish pass); - testAsync "testAsync" (fun finish -> - finish pass); - Skip.testAsync "testAsync - no done" (fun _ -> ()); - Skip.testAsync "testAsync - expect fail" (fun finish -> - finish (fail "")); + Skip.testAsync "testAsync - expect fail" (fun finish -> finish (fail "")); + + testAsync "testAsync - timeout ok" ~timeout:1 (fun finish -> finish pass); - testAsync "testAsync - timeout ok" ~timeout:1 (fun finish -> - finish pass); - Skip.testAsync "testAsync - timeout fail" ~timeout:1 (fun _ -> ()); - - testPromise "testPromise" (fun () -> - Js.Promise.resolve pass); + testPromise "testPromise" (fun () -> Js.Promise.resolve pass); Skip.testPromise "testPromise - reject" (fun () -> - Js.Promise.reject (Failure "")); + Js.Promise.reject (Failure "")); Skip.testPromise "testPromise - expect fail" (fun () -> - Js.Promise.resolve (fail "")); + Js.Promise.resolve (fail "")); testPromise "testPromise - timeout ok" ~timeout:1 (fun () -> - Js.Promise.resolve pass); - - Skip.testPromise "testPromise - timeout fail" ~timeout:1 (fun () -> - Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); - + Js.Promise.resolve pass); - testAll "testAll" ["foo"; "bar"; "baz"] (fun input -> - if Js.String.length input == 3 then pass else fail ""); - testAll "testAll - tuples" [("foo", 3); ("barbaz", 6); ("bananas!", 8)] (fun (input, output) -> - if Js.String.length input == output then pass else fail ""); - + Skip.testPromise "testPromise - timeout fail" ~timeout:1 (fun () -> + Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); - describe "describe" (fun () -> - test "some aspect" (fun () -> pass) - ); - + testAll "testAll" [ "foo"; "bar"; "baz" ] (fun input -> + if Js.String.length input == 3 then pass else fail ""); + testAll "testAll - tuples" + [ ("foo", 3); ("barbaz", 6); ("bananas!", 8) ] + (fun (input, output) -> + if Js.String.length input == output then pass else fail ""); - describe "beforeAll" (fun () -> - let x = ref 0 in - - beforeAll (fun () -> x := !x + 4); + describe "describe" (fun () -> test "some aspect" (fun () -> pass)); - test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is still 4" (fun () -> if !x == 4 then pass else fail ""); - ); - - describe "beforeAllAsync" (fun () -> - describe "without timeout" (fun () -> + describe "beforeAll" (fun () -> let x = ref 0 in - beforeAllAsync (fun (finish) -> x := !x + 4; finish ()); + beforeAll (fun () -> x := !x + 4); test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is still 4" (fun () -> if !x == 4 then pass else fail ""); - ); + test "x is still 4" (fun () -> if !x == 4 then pass else fail "")); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "beforeAllAsync" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - beforeAllAsync ~timeout:100 (fun (finish) -> x := !x + 4; finish ()); + beforeAllAsync (fun finish -> + x := !x + 4; + finish ()); - test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is still 4" (fun () -> if !x == 4 then pass else fail ""); - ); + test "x is 4" (fun () -> if !x == 4 then pass else fail ""); + test "x is still 4" (fun () -> if !x == 4 then pass else fail "")); - Skip.describe "timeout should fail suite" (fun () -> - (* This apparently runs even if the suite is skipped *) - (*beforeAllAsync ~timeout:1 (fun _ ->());*) + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> pass); (* runner will crash if there's no tests *) - ); - ); + beforeAllAsync ~timeout:100 (fun finish -> + x := !x + 4; + finish ()); - describe "beforeAllPromise" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + test "x is 4" (fun () -> if !x == 4 then pass else fail ""); + test "x is still 4" (fun () -> if !x == 4 then pass else fail "")); - beforeAllPromise (fun () -> x := !x + 4; Js.Promise.resolve ()); + Skip.describe "timeout should fail suite" (fun () -> + (* This apparently runs even if the suite is skipped *) + (*beforeAllAsync ~timeout:1 (fun _ ->());*) + test "" (fun () -> pass) + (* runner will crash if there's no tests *))); - test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is still 4" (fun () -> if !x == 4 then pass else fail ""); - ); - - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "beforeAllPromise" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - beforeAllPromise ~timeout:100 (fun () -> x := !x + 4; Js.Promise.resolve ()); + beforeAllPromise (fun () -> + x := !x + 4; + Js.Promise.resolve ()); - test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is still 4" (fun () -> if !x == 4 then pass else fail ""); - ); + test "x is 4" (fun () -> if !x == 4 then pass else fail ""); + test "x is still 4" (fun () -> if !x == 4 then pass else fail "")); - Skip.describe "timeout should fail suite" (fun () -> - (* This apparently runs even if the suite is skipped *) - (*beforeAllPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ()));*) + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> pass); (* runner will crash if there's no tests *) - ); - ); + beforeAllPromise ~timeout:100 (fun () -> + x := !x + 4; + Js.Promise.resolve ()); + test "x is 4" (fun () -> if !x == 4 then pass else fail ""); + test "x is still 4" (fun () -> if !x == 4 then pass else fail "")); - describe "beforeEach" (fun () -> - let x = ref 0 in - - beforeEach (fun () -> x := !x + 4); + Skip.describe "timeout should fail suite" (fun () -> + (* This apparently runs even if the suite is skipped *) + (*beforeAllPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ()));*) + test "" (fun () -> pass) + (* runner will crash if there's no tests *))); - test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail ""); - ); - - describe "beforeEachAsync" (fun () -> - describe "without timeout" (fun () -> + describe "beforeEach" (fun () -> let x = ref 0 in - beforeEachAsync (fun (finish) -> x := !x + 4; finish ()); + beforeEach (fun () -> x := !x + 4); test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail ""); - ); + test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail "")); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "beforeEachAsync" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - beforeEachAsync ~timeout:100 (fun (finish) -> x := !x + 4; finish ()); + beforeEachAsync (fun finish -> + x := !x + 4; + finish ()); - test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail ""); - ); + test "x is 4" (fun () -> if !x == 4 then pass else fail ""); + test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail "")); - Skip.describe "timeout should fail suite" (fun () -> - beforeEachAsync ~timeout:1 (fun _ -> ()); + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> pass); (* runner will crash if there's no tests *) - ); - ); + beforeEachAsync ~timeout:100 (fun finish -> + x := !x + 4; + finish ()); - describe "beforeEachPromise" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + test "x is 4" (fun () -> if !x == 4 then pass else fail ""); + test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail "")); - beforeEachPromise (fun () -> x := !x + 4; Js.Promise.resolve true); + Skip.describe "timeout should fail suite" (fun () -> + beforeEachAsync ~timeout:1 (fun _ -> ()); - test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail ""); - ); + test "" (fun () -> pass) (* runner will crash if there's no tests *))); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "beforeEachPromise" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - beforeEachPromise ~timeout:100 (fun () -> x := !x + 4; Js.Promise.resolve true); + beforeEachPromise (fun () -> + x := !x + 4; + Js.Promise.resolve true); - test "x is 4" (fun () -> if !x == 4 then pass else fail ""); - test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail ""); - ); + test "x is 4" (fun () -> if !x == 4 then pass else fail ""); + test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail "")); - Skip.describe "timeout should fail suite" (fun () -> - beforeEachPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> pass); (* runner will crash if there's no tests *) - ); - ); + beforeEachPromise ~timeout:100 (fun () -> + x := !x + 4; + Js.Promise.resolve true); + test "x is 4" (fun () -> if !x == 4 then pass else fail ""); + test "x is suddenly 8" (fun () -> if !x == 8 then pass else fail "")); - describe "afterAll" (fun () -> - let x = ref 0 in - - describe "phase 1" (fun () -> - afterAll (fun () -> x := !x + 4); + Skip.describe "timeout should fail suite" (fun () -> + beforeEachPromise ~timeout:1 (fun () -> + Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is still 0" (fun () -> if !x == 0 then pass else fail ""); - ); - - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); - ); - - describe "afterAllAsync" (fun () -> - describe "without timeout" (fun () -> + test "" (fun () -> pass) (* runner will crash if there's no tests *))); + + describe "afterAll" (fun () -> let x = ref 0 in describe "phase 1" (fun () -> - afterAllAsync (fun (finish) -> x := !x + 4; finish ()); + afterAll (fun () -> x := !x + 4); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is still 0" (fun () -> if !x == 0 then pass else fail ""); - ); + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is still 0" (fun () -> if !x == 0 then pass else fail "")); describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); - ); + test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""))); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "afterAllAsync" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - describe "phase 1" (fun () -> - afterAllAsync ~timeout:100 (fun (finish) -> x := !x + 4; finish ()); + describe "phase 1" (fun () -> + afterAllAsync (fun finish -> + x := !x + 4; + finish ()); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is still 0" (fun () -> if !x == 0 then pass else fail ""); - ); + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is still 0" (fun () -> if !x == 0 then pass else fail "")); - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); - ); + describe "phase 2" (fun () -> + test "x is suddenly 4" (fun () -> + if !x == 4 then pass else fail ""))); - describe "timeout should not fail suite" (fun () -> - (* In jest 27, they do. *) - (* afterAllAsync ~timeout:3 (fun _ ->()); *) + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> pass); (* runner will crash if there's no tests *) - ); - ); + describe "phase 1" (fun () -> + afterAllAsync ~timeout:100 (fun finish -> + x := !x + 4; + finish ()); - describe "afterAllPromise" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is still 0" (fun () -> if !x == 0 then pass else fail "")); - describe "phase 1" (fun () -> - afterAllPromise (fun () -> x := !x + 4; Js.Promise.resolve true); + describe "phase 2" (fun () -> + test "x is suddenly 4" (fun () -> + if !x == 4 then pass else fail ""))); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is still 0" (fun () -> if !x == 0 then pass else fail ""); - ); + describe "timeout should not fail suite" (fun () -> + (* In jest 27, they do. *) + (* afterAllAsync ~timeout:3 (fun _ ->()); *) + test "" (fun () -> pass) + (* runner will crash if there's no tests *))); - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); - ); + describe "afterAllPromise" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "phase 1" (fun () -> + afterAllPromise (fun () -> + x := !x + 4; + Js.Promise.resolve true); - describe "phase 1" (fun () -> - afterAllPromise ~timeout:100 (fun () -> x := !x + 4; Js.Promise.resolve true); + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is still 0" (fun () -> if !x == 0 then pass else fail "")); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is still 0" (fun () -> if !x == 0 then pass else fail ""); - ); + describe "phase 2" (fun () -> + test "x is suddenly 4" (fun () -> + if !x == 4 then pass else fail ""))); - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); - ); + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - describe "timeout should not fail suite" (fun () -> - (* In jest 27, they do. *) - (* afterAllPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); *) + describe "phase 1" (fun () -> + afterAllPromise ~timeout:100 (fun () -> + x := !x + 4; + Js.Promise.resolve true); - test "" (fun () -> pass); (* runner will crash if there's no tests *) - ); - ); + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is still 0" (fun () -> if !x == 0 then pass else fail "")); + describe "phase 2" (fun () -> + test "x is suddenly 4" (fun () -> + if !x == 4 then pass else fail ""))); - describe "afterEach" (fun () -> - let x = ref 0 in - - afterEach (fun () -> x := !x + 4); + describe "timeout should not fail suite" (fun () -> + (* In jest 27, they do. *) + (* afterAllPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); *) + test "" (fun () -> pass) + (* runner will crash if there's no tests *))); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); - - describe "afterEachAsync" (fun () -> - describe "without timeout" (fun () -> + describe "afterEach" (fun () -> let x = ref 0 in - afterEachAsync (fun (finish) -> x := !x + 4; finish ()); + afterEach (fun () -> x := !x + 4); test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); + test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail "")); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "afterEachAsync" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - afterEachAsync ~timeout:100 (fun (finish) -> x := !x + 4; finish ()); + afterEachAsync (fun finish -> + x := !x + 4; + finish ()); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail "")); - Skip.describe "timeout should fail suite" (fun () -> - afterEachAsync ~timeout:1 (fun _ -> ()); + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> pass); (* runner will crash if there's no tests *) - ); - ); + afterEachAsync ~timeout:100 (fun finish -> + x := !x + 4; + finish ()); - describe "afterEachPromise" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail "")); - afterEachPromise (fun () -> x := !x + 4; Js.Promise.resolve true); + Skip.describe "timeout should fail suite" (fun () -> + afterEachAsync ~timeout:1 (fun _ -> ()); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); + test "" (fun () -> pass) (* runner will crash if there's no tests *))); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "afterEachPromise" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - afterEachPromise ~timeout:100 (fun () -> x := !x + 4; Js.Promise.resolve true); + afterEachPromise (fun () -> + x := !x + 4; + Js.Promise.resolve true); - test "x is 0" (fun () -> if !x == 0 then pass else fail ""); - test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail ""); - ); + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail "")); - Skip.describe "timeout should fail suite" (fun () -> - afterEachPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> pass); (* runner will crash if there's no tests *) - ); - ); + afterEachPromise ~timeout:100 (fun () -> + x := !x + 4; + Js.Promise.resolve true); + test "x is 0" (fun () -> if !x == 0 then pass else fail ""); + test "x is suddenly 4" (fun () -> if !x == 4 then pass else fail "")); - describe "Only" (fun () -> - (* See globals_only_test.ml *) - () - ); + Skip.describe "timeout should fail suite" (fun () -> + afterEachPromise ~timeout:1 (fun () -> + Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); + + test "" (fun () -> pass) (* runner will crash if there's no tests *))); + describe "Only" (fun () -> + (* See globals_only_test.ml *) + ()); describe "Skip" (fun () -> - Skip.test "Skip.test" (fun () -> pass); + Skip.test "Skip.test" (fun () -> pass); - Skip.testAsync "Skip.testAsync" (fun finish -> - finish pass); - Skip.testAsync "Skip.testAsync - timeout" ~timeout:1 (fun _ -> ()); + Skip.testAsync "Skip.testAsync" (fun finish -> finish pass); + Skip.testAsync "Skip.testAsync - timeout" ~timeout:1 (fun _ -> ()); - Skip.testPromise "Skip.testPromise" (fun () -> - Js.Promise.resolve pass); - Skip.testPromise "testPromise - timeout" ~timeout:1 (fun () -> - Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); + Skip.testPromise "Skip.testPromise" (fun () -> Js.Promise.resolve pass); + Skip.testPromise "testPromise - timeout" ~timeout:1 (fun () -> + Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); - Skip.testAll "testAll" ["foo"; "bar"; "baz"] (fun input -> - if Js.String.length input == 3 then pass else fail ""); - Skip.testAll "testAll - tuples" [("foo", 3); ("barbaz", 6); ("bananas!", 8)] (fun (input, output) -> - if Js.String.length input == output then pass else fail ""); + Skip.testAll "testAll" [ "foo"; "bar"; "baz" ] (fun input -> + if Js.String.length input == 3 then pass else fail ""); + Skip.testAll "testAll - tuples" + [ ("foo", 3); ("barbaz", 6); ("bananas!", 8) ] + (fun (input, output) -> + if Js.String.length input == output then pass else fail ""); - Skip.describe "Skip.describe" (fun () -> - test "some aspect" (fun () -> pass); - ); + Skip.describe "Skip.describe" (fun () -> + test "some aspect" (fun () -> pass))); - ); - - describe "Todo" (fun () -> - Todo.test "Todo.test"; - ) \ No newline at end of file + describe "Todo" (fun () -> Todo.test "Todo.test") diff --git a/jest/__tests__/jest_test.ml b/jest/__tests__/jest_test.ml index 8467a62..b147229 100644 --- a/jest/__tests__/jest_test.ml +++ b/jest/__tests__/jest_test.ml @@ -6,90 +6,83 @@ external setTimeout : (unit -> unit) -> int -> unit = "setTimeout" external setImmediate : (unit -> unit) -> unit = "setImmediate" external nextTick : (unit -> unit) -> unit = "process.nextTick" -let () = - -describe "Fake Timers" (fun () -> - test "runAllTimers" (fun () -> - let flag = ref false in - Jest.useFakeTimers (); - setTimeout (fun () -> flag := true) 0; - let before = !flag in - Jest.runAllTimers (); - - expect (before, !flag) = (false, true) - ); - - test "runAllTicks" (fun () -> - let flag = ref false in - Jest.useFakeTimers (); - nextTick (fun () -> flag := true); - let before = !flag in - Jest.runAllTicks (); - - expect (before, !flag) = (false, true) - ); - - test "runAllImmediates" (fun () -> - let flag = ref false in - Jest.useFakeTimers ~config:{legacyFakeTimers=true} (); - setImmediate (fun () -> flag := true); - let before = !flag in - Jest.runAllImmediates (); - - expect (before, !flag) = (false, true) - ); - - test "runTimersToTime" (fun () -> - let flag = ref false in - Jest.useFakeTimers (); - setTimeout (fun () -> flag := true) 1500; - let before = !flag in - Jest.advanceTimersByTime 1000; - let inbetween = !flag in - Jest.advanceTimersByTime 1000; - - expect (before, inbetween, !flag) = (false, false, true) - ); - - test "advanceTimersByTime" (fun () -> - let flag = ref false in - Jest.useFakeTimers (); - setTimeout(fun () -> flag := true) 1500; - let before = !flag in - Jest.advanceTimersByTime 1000; - let inbetween = !flag in - Jest.advanceTimersByTime 1000; - - expect (before, inbetween, !flag) = (false, false, true) - ); - - test "runOnlyPendingTimers" (fun () -> - let count = ref 0 in - Jest.useFakeTimers (); - let rec recursiveTimeout () = count := !count + 1; setTimeout recursiveTimeout 1500 in - recursiveTimeout (); - let before = !count in - Jest.runOnlyPendingTimers (); - let inbetween = !count in - Jest.runOnlyPendingTimers (); - - expect (before, inbetween, !count) = (1, 2, 3) - ); - - test "clearAllTimers" (fun () -> - let flag = ref false in - Jest.useFakeTimers (); - setImmediate (fun () -> flag := true); - let before = !flag in - Jest.clearAllTimers (); - Jest.runAllTimers (); - - expect (before, !flag) = (false, false) - ); - - testAsync "clearAllTimers" (fun finish -> - Jest.useFakeTimers (); - Jest.useRealTimers (); - nextTick (fun () -> finish pass); - ); -); +let () = + describe "Fake Timers" (fun () -> + test "runAllTimers" (fun () -> + let flag = ref false in + Jest.useFakeTimers (); + setTimeout (fun () -> flag := true) 0; + let before = !flag in + Jest.runAllTimers (); + + expect (before, !flag) = (false, true)); + + test "runAllTicks" (fun () -> + let flag = ref false in + Jest.useFakeTimers (); + nextTick (fun () -> flag := true); + let before = !flag in + Jest.runAllTicks (); + + expect (before, !flag) = (false, true)); + + test "runAllImmediates" (fun () -> + let flag = ref false in + Jest.useFakeTimers ~config:{ legacyFakeTimers = true } (); + setImmediate (fun () -> flag := true); + let before = !flag in + Jest.runAllImmediates (); + + expect (before, !flag) = (false, true)); + + test "runTimersToTime" (fun () -> + let flag = ref false in + Jest.useFakeTimers (); + setTimeout (fun () -> flag := true) 1500; + let before = !flag in + Jest.advanceTimersByTime 1000; + let inbetween = !flag in + Jest.advanceTimersByTime 1000; + + expect (before, inbetween, !flag) = (false, false, true)); + + test "advanceTimersByTime" (fun () -> + let flag = ref false in + Jest.useFakeTimers (); + setTimeout (fun () -> flag := true) 1500; + let before = !flag in + Jest.advanceTimersByTime 1000; + let inbetween = !flag in + Jest.advanceTimersByTime 1000; + + expect (before, inbetween, !flag) = (false, false, true)); + + test "runOnlyPendingTimers" (fun () -> + let count = ref 0 in + Jest.useFakeTimers (); + let rec recursiveTimeout () = + count := !count + 1; + setTimeout recursiveTimeout 1500 + in + recursiveTimeout (); + let before = !count in + Jest.runOnlyPendingTimers (); + let inbetween = !count in + Jest.runOnlyPendingTimers (); + + expect (before, inbetween, !count) = (1, 2, 3)); + + test "clearAllTimers" (fun () -> + let flag = ref false in + Jest.useFakeTimers (); + setImmediate (fun () -> flag := true); + let before = !flag in + Jest.clearAllTimers (); + Jest.runAllTimers (); + + expect (before, !flag) = (false, false)); + + testAsync "clearAllTimers" (fun finish -> + Jest.useFakeTimers (); + Jest.useRealTimers (); + nextTick (fun () -> finish pass))) diff --git a/jest/__tests__/mockjs_test.ml b/jest/__tests__/mockjs_test.ml index 19d43b2..cbbde16 100644 --- a/jest/__tests__/mockjs_test.ml +++ b/jest/__tests__/mockjs_test.ml @@ -2,390 +2,374 @@ open Jest open ExpectJs (* TODO: move to BS std lib *) -external bind : ('a -> 'b) -> 'c -> 'a -> ('a -> 'b) = "bind" [@@mel.send] -external bindThis : ('a -> 'b [@u]) -> 'c -> ('a -> 'b [@u]) = "bind" [@@mel.send] -external call : ('a -> 'b [@u]) -> 'c -> 'a -> 'b = "call" [@@mel.send] +external bind : ('a -> 'b) -> 'c -> 'a -> 'a -> 'b = "bind" [@@mel.send] + +external bindThis : (('a -> 'b)[@u]) -> 'c -> ('a -> 'b[@u]) = "bind" +[@@mel.send] + +external call : (('a -> 'b)[@u]) -> 'c -> 'a -> 'b = "call" [@@mel.send] + let call self arg = call self () arg -external callThis : ('a -> 'b [@u]) -> 'c -> 'a -> 'b = "call" [@@mel.send] -external call2 : ('a -> 'b -> 'c [@u]) -> (_ [@mel.as 0]) -> 'a -> 'b -> 'c = "call" [@@mel.send] -let _ = +external callThis : (('a -> 'b)[@u]) -> 'c -> 'a -> 'b = "call" [@@mel.send] -describe "inferred_fn" (fun _ -> - test "returns undefined" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - expect (call fn ()) |> toBeUndefined - ); - - test "black hole for argument type object" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - expect (call fn [%obj { property = 42 }]) |> toBeUndefined - ); - - test "black hole for argument type string" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - expect (call fn "some string") |> toBeUndefined - ); - - test "calls - records call arguments" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - let _ = call fn "first" in - let _ = call fn "second" in - let calls = mockFn |> MockJs.calls in - - expect calls |> toEqual [| "first"; "second" |] - ); +external call2 : (('a -> 'b -> 'c)[@u]) -> (_[@mel.as 0]) -> 'a -> 'b -> 'c + = "call" +[@@mel.send] - test "instances - sanity check - is empty array" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let instances = MockJs.instances mockFn in +let _ = + describe "inferred_fn" (fun _ -> + test "returns undefined" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in - expect instances |> toEqual [||] - ); + expect (call fn ()) |> toBeUndefined); - test "instances - records created instances" (fun _ -> - let mockFn = JestJs.fn [%raw "function (n) { this.n = n; }"] in - - mockFn |> MockJs.new1 4 |> ignore; - mockFn |> MockJs.new1 7 |> ignore; + test "black hole for argument type object" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in - let instances = MockJs.instances mockFn in + expect (call fn [%obj { property = 42 }]) |> toBeUndefined); - expect instances |> toEqual [| [%obj { n = 4 }]; [%obj { n = 7 }] |] - ); - - test "mockClear - resets calls" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - let before = mockFn |> MockJs.calls in - let _ = (call fn 1, call fn 2) in - let inbetween = mockFn |> MockJs.calls in - MockJs.mockClear mockFn; - let after = mockFn |> MockJs.calls in - - expect - (before, inbetween, after) - |> toEqual - ([||], [| 1; 2 |], [||]) - ); - - test "mockReset - resets calls" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - let before = mockFn |> MockJs.calls in - let _ = (call fn 1, call fn 2) in - let inbetween = mockFn |> MockJs.calls in - MockJs.mockReset mockFn; - let after = mockFn |> MockJs.calls in - - expect - (before, inbetween, after) - |> toEqual - ([||], [| 1; 2 |], [||]) - ); - - test "mockReset - resets implementations" (fun _ -> - let mockFn = JestJs.inferred_fn () in - mockFn |> MockJs.mockReturnValue (Js.Undefined.return 128) |> ignore; - let fn = MockJs.fn mockFn in - - let before = call fn () in - MockJs.mockReset mockFn; - let after = call fn () in - - expect - (before, after) - |> toEqual - (Js.Undefined.return 128, Js.Undefined.empty) - ); - - test "mockImplementation - sets implementation to use for subsequent invocations" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - let before = call fn 10 in - mockFn |> MockJs.mockImplementation ((fun a -> Js.Undefined.return (string_of_int a)) [@u]) |> ignore; - - expect - (before, call fn 18, call fn 24) - |> toEqual - (Js.Undefined.empty, Js.Undefined.return "18", Js.Undefined.return "24") - ); - - test "mockImplementationOnce - queues implementation for one subsequent invocation" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - let before = call fn 10 in - mockFn |> MockJs.mockImplementationOnce ((fun a -> Js.Undefined.return (string_of_int a)) [@u]) |> ignore; - mockFn |> MockJs.mockImplementationOnce ((fun a -> Js.Undefined.return (string_of_int (a * 2))) [@u]) |> ignore; - - expect - (before, call fn 18, call fn 24, call fn 12) - |> toEqual - (Js.Undefined.empty, Js.Undefined.return "18", Js.Undefined.return "48", Js.Undefined.empty) - ); - - test "mockReturnThis - returns `this` on subsequent invocations" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let this = "this" in - let fn = bindThis (mockFn |> MockJs.fn) this in - - let before = call fn () in - mockFn |> MockJs.mockReturnThis |> ignore; - - expect - (before, call fn (), call fn ()) - |> toEqual - (Js.Undefined.empty, Js.Undefined.return this, Js.Undefined.return this) - ); - - test "mockReturnValue - returns given value on subsequent invocations" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - let before = call fn 10 in - mockFn |> MockJs.mockReturnValue (Js.Undefined.return 146) |> ignore; - - expect - (before, call fn 18, call fn 24) - |> toEqual - (Js.Undefined.empty, Js.Undefined.return 146, Js.Undefined.return 146) - ); - - test "mockReturnValueOnce - queues implementation for one subsequent invocation" (fun _ -> - let mockFn = JestJs.inferred_fn () in - let fn = MockJs.fn mockFn in - - let before = call fn 10 in - mockFn |> MockJs.mockReturnValueOnce (Js.Undefined.return 29) |> ignore; - mockFn |> MockJs.mockReturnValueOnce (Js.Undefined.return 41) |> ignore; - - expect - (before, call fn 18, call fn 24, call fn 12) - |> toEqual - (Js.Undefined.empty, Js.Undefined.return 29, Js.Undefined.return 41, Js.Undefined.empty) - ); - - (* + test "black hole for argument type string" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in + + expect (call fn "some string") |> toBeUndefined); + + test "calls - records call arguments" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in + + let _ = call fn "first" in + let _ = call fn "second" in + let calls = mockFn |> MockJs.calls in + + expect calls |> toEqual [| "first"; "second" |]); + + test "instances - sanity check - is empty array" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let instances = MockJs.instances mockFn in + + expect instances |> toEqual [||]); + + test "instances - records created instances" (fun _ -> + let mockFn = JestJs.fn [%raw "function (n) { this.n = n; }"] in + + mockFn |> MockJs.new1 4 |> ignore; + mockFn |> MockJs.new1 7 |> ignore; + + let instances = MockJs.instances mockFn in + + expect instances |> toEqual [| [%obj { n = 4 }]; [%obj { n = 7 }] |]); + + test "mockClear - resets calls" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in + + let before = mockFn |> MockJs.calls in + let _ = (call fn 1, call fn 2) in + let inbetween = mockFn |> MockJs.calls in + MockJs.mockClear mockFn; + let after = mockFn |> MockJs.calls in + + expect (before, inbetween, after) |> toEqual ([||], [| 1; 2 |], [||])); + + test "mockReset - resets calls" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in + + let before = mockFn |> MockJs.calls in + let _ = (call fn 1, call fn 2) in + let inbetween = mockFn |> MockJs.calls in + MockJs.mockReset mockFn; + let after = mockFn |> MockJs.calls in + + expect (before, inbetween, after) |> toEqual ([||], [| 1; 2 |], [||])); + + test "mockReset - resets implementations" (fun _ -> + let mockFn = JestJs.inferred_fn () in + mockFn |> MockJs.mockReturnValue (Js.Undefined.return 128) |> ignore; + let fn = MockJs.fn mockFn in + + let before = call fn () in + MockJs.mockReset mockFn; + let after = call fn () in + + expect (before, after) + |> toEqual (Js.Undefined.return 128, Js.Undefined.empty)); + + test + "mockImplementation - sets implementation to use for subsequent \ + invocations" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in + + let before = call fn 10 in + mockFn + |> MockJs.mockImplementation (fun [@u] a -> + Js.Undefined.return (string_of_int a)) + |> ignore; + + expect (before, call fn 18, call fn 24) + |> toEqual + ( Js.Undefined.empty, + Js.Undefined.return "18", + Js.Undefined.return "24" )); + + test + "mockImplementationOnce - queues implementation for one subsequent \ + invocation" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in + + let before = call fn 10 in + mockFn + |> MockJs.mockImplementationOnce (fun [@u] a -> + Js.Undefined.return (string_of_int a)) + |> ignore; + mockFn + |> MockJs.mockImplementationOnce (fun [@u] a -> + Js.Undefined.return (string_of_int (a * 2))) + |> ignore; + + expect (before, call fn 18, call fn 24, call fn 12) + |> toEqual + ( Js.Undefined.empty, + Js.Undefined.return "18", + Js.Undefined.return "48", + Js.Undefined.empty )); + + test "mockReturnThis - returns `this` on subsequent invocations" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let this = "this" in + let fn = bindThis (mockFn |> MockJs.fn) this in + + let before = call fn () in + mockFn |> MockJs.mockReturnThis |> ignore; + + expect (before, call fn (), call fn ()) + |> toEqual + ( Js.Undefined.empty, + Js.Undefined.return this, + Js.Undefined.return this )); + + test "mockReturnValue - returns given value on subsequent invocations" + (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in + + let before = call fn 10 in + mockFn |> MockJs.mockReturnValue (Js.Undefined.return 146) |> ignore; + + expect (before, call fn 18, call fn 24) + |> toEqual + ( Js.Undefined.empty, + Js.Undefined.return 146, + Js.Undefined.return 146 )); + + test + "mockReturnValueOnce - queues implementation for one subsequent \ + invocation" (fun _ -> + let mockFn = JestJs.inferred_fn () in + let fn = MockJs.fn mockFn in + + let before = call fn 10 in + mockFn + |> MockJs.mockReturnValueOnce (Js.Undefined.return 29) + |> ignore; + mockFn + |> MockJs.mockReturnValueOnce (Js.Undefined.return 41) + |> ignore; + + expect (before, call fn 18, call fn 24, call fn 12) + |> toEqual + ( Js.Undefined.empty, + Js.Undefined.return 29, + Js.Undefined.return 41, + Js.Undefined.empty )); + + (* Skip.test "bindThis" (fun _ -> let fn = ((fun a -> string_of_int a) [@u]) in let boundFn = bindThis fn "this" in - + expect (call boundFn () 2) |> toEqual "2" ); *) - - test "mockClear - resets instances" (fun _ -> + test "mockClear - resets instances" (fun _ -> + let mockFn = JestJs.fn [%raw "function (n) { this.n = n; }"] in - let mockFn = JestJs.fn [%raw "function (n) { this.n = n; }"] in - - let before = mockFn |> MockJs.instances in + let before = mockFn |> MockJs.instances in - mockFn |> MockJs.new1 4 |> ignore; - mockFn |> MockJs.new1 7 |> ignore; + mockFn |> MockJs.new1 4 |> ignore; + mockFn |> MockJs.new1 7 |> ignore; - let inbetween = mockFn |> MockJs.instances in + let inbetween = mockFn |> MockJs.instances in - MockJs.mockClear mockFn; + MockJs.mockClear mockFn; - let after = mockFn |> MockJs.instances in - - expect - (before, inbetween, after) - |> toEqual - ([||], [| [%obj { n = 4 }]; [%obj { n = 7 }] |], [||]) - ); -); - -describe "fn" (fun _ -> - test "calls implementation" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - let fn = MockJs.fn mockFn in - - expect (fn 18) |> toBe "18" - ); - - test "calls - records call arguments" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - - let _ = MockJs.fn mockFn 74 in - let _ = MockJs.fn mockFn 89435 in - let calls = mockFn |> MockJs.calls in - - expect calls |> toEqual [| 74; 89435 |] - ); - - Skip.test "mockClear - resets calls" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - - let before = mockFn |> MockJs.calls in - let _ = (MockJs.fn mockFn 1, MockJs.fn mockFn 2) in - let inbetween = mockFn |> MockJs.calls in - MockJs.mockClear mockFn; - let after = mockFn |> MockJs.calls in - - expect - (before, inbetween, after) - |> toEqual - ([||], [| 1; 2 |], [||]) - ); - - test "mockReset - resets calls" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - let fn = MockJs.fn mockFn in - - let before = mockFn |> MockJs.calls in - let _ = (fn 1, fn 2) in - let inbetween = mockFn |> MockJs.calls in - MockJs.mockReset mockFn; - let after = mockFn |> MockJs.calls in - - expect - (before, inbetween, after) - |> toEqual - ([||], [| 1; 2 |], [||]) - ); - - (* TODO: Actually removes the original imlementation too, causing it to return undefined, which usually won't be a valid return value for the function type it mocks *) - Skip.test "mockReset - resets implementations" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - mockFn |> MockJs.mockReturnValue "128" |> ignore; - let fn = MockJs.fn mockFn in - - let before = fn 0 in - MockJs.mockReset mockFn; - let after = fn 1 in - - expect - (before, after) - |> toEqual - ("128", "1") - ); - - test "mockImplementation - sets implementation to use for subsequent invocations" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - let fn = MockJs.fn mockFn in - - let before = fn 10 in - mockFn |> MockJs.mockImplementation (fun a -> string_of_int (a * 2)) |> ignore; - - expect - (before, fn 18, fn 24) - |> toEqual - ("10", "36", "48") - ); - - test "mockImplementationOnce - queues implementation for one subsequent invocation" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - let fn = MockJs.fn mockFn in - - let before = fn 10 in - mockFn |> MockJs.mockImplementationOnce (fun a -> string_of_int (a * 3)) |> ignore; - mockFn |> MockJs.mockImplementationOnce (fun a -> string_of_int (a * 2)) |> ignore; - - expect - (before, fn 18, fn 24, fn 12) - |> toEqual - ("10", "54", "48", "12") - ); + let after = mockFn |> MockJs.instances in + + expect (before, inbetween, after) + |> toEqual ([||], [| [%obj { n = 4 }]; [%obj { n = 7 }] |], [||]))); + + describe "fn" (fun _ -> + test "calls implementation" (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + let fn = MockJs.fn mockFn in + + expect (fn 18) |> toBe "18"); + + test "calls - records call arguments" (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + + let _ = MockJs.fn mockFn 74 in + let _ = MockJs.fn mockFn 89435 in + let calls = mockFn |> MockJs.calls in + + expect calls |> toEqual [| 74; 89435 |]); + + Skip.test "mockClear - resets calls" (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + + let before = mockFn |> MockJs.calls in + let _ = (MockJs.fn mockFn 1, MockJs.fn mockFn 2) in + let inbetween = mockFn |> MockJs.calls in + MockJs.mockClear mockFn; + let after = mockFn |> MockJs.calls in + + expect (before, inbetween, after) |> toEqual ([||], [| 1; 2 |], [||])); + + test "mockReset - resets calls" (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + let fn = MockJs.fn mockFn in + + let before = mockFn |> MockJs.calls in + let _ = (fn 1, fn 2) in + let inbetween = mockFn |> MockJs.calls in + MockJs.mockReset mockFn; + let after = mockFn |> MockJs.calls in + + expect (before, inbetween, after) |> toEqual ([||], [| 1; 2 |], [||])); - (* mockReturnThis doesn't make sense for native functions + (* TODO: Actually removes the original imlementation too, causing it to return undefined, which usually won't be a valid return value for the function type it mocks *) + Skip.test "mockReset - resets implementations" (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + mockFn |> MockJs.mockReturnValue "128" |> ignore; + let fn = MockJs.fn mockFn in + + let before = fn 0 in + MockJs.mockReset mockFn; + let after = fn 1 in + + expect (before, after) |> toEqual ("128", "1")); + + test + "mockImplementation - sets implementation to use for subsequent \ + invocations" (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + let fn = MockJs.fn mockFn in + + let before = fn 10 in + mockFn + |> MockJs.mockImplementation (fun a -> string_of_int (a * 2)) + |> ignore; + + expect (before, fn 18, fn 24) |> toEqual ("10", "36", "48")); + + test + "mockImplementationOnce - queues implementation for one subsequent \ + invocation" (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + let fn = MockJs.fn mockFn in + + let before = fn 10 in + mockFn + |> MockJs.mockImplementationOnce (fun a -> string_of_int (a * 3)) + |> ignore; + mockFn + |> MockJs.mockImplementationOnce (fun a -> string_of_int (a * 2)) + |> ignore; + + expect (before, fn 18, fn 24, fn 12) + |> toEqual ("10", "54", "48", "12")); + + (* mockReturnThis doesn't make sense for native functions test "mockReturnThis - returns `this` on subsequent invocations" (fun _ -> let mockFn = JestJs.fn (fun a -> string_of_int a) in let this = "this" in let fn = bindThis (mockFn |> MockJs.fn) this in - + let before = fn () in mockFn |> MockJs.mockReturnThis |> ignore; - + expect (before, fn (), fn ()) |> toEqual (Js.Undefined.empty, Js.Undefined.return this, Js.Undefined.return this) ); *) - - test "mockReturnValue - returns given value on subsequent invocations" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - let fn = MockJs.fn mockFn in - - let before = fn 10 in - mockFn |> MockJs.mockReturnValue "146" |> ignore; - - expect - (before, fn 18, fn 24) - |> toEqual - ("10", "146", "146") - ); - - test "mockReturnValueOnce - queues implementation for one subsequent invocation" (fun _ -> - let mockFn = JestJs.fn (fun a -> string_of_int a) in - let fn = MockJs.fn mockFn in - - let before = fn 10 in - mockFn |> MockJs.mockReturnValueOnce "29" |> ignore; - mockFn |> MockJs.mockReturnValueOnce "41" |> ignore; - - expect - (before, fn 18, fn 24, fn 12) - |> toEqual - ("10", "29", "41", "12") - ); -); + test "mockReturnValue - returns given value on subsequent invocations" + (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + let fn = MockJs.fn mockFn in -describe "fn2" (fun _ -> - test "calls implementation" (fun _ -> - let mockFn = JestJs.fn2 ((fun a b -> string_of_int (a + b)) [@u]) in - let fn = MockJs.fn mockFn in - - expect (call2 fn 18 24) |> toBe "42" - ); + let before = fn 10 in + mockFn |> MockJs.mockReturnValue "146" |> ignore; + + expect (before, fn 18, fn 24) |> toEqual ("10", "146", "146")); + + test + "mockReturnValueOnce - queues implementation for one subsequent \ + invocation" (fun _ -> + let mockFn = JestJs.fn (fun a -> string_of_int a) in + let fn = MockJs.fn mockFn in + + let before = fn 10 in + mockFn |> MockJs.mockReturnValueOnce "29" |> ignore; + mockFn |> MockJs.mockReturnValueOnce "41" |> ignore; + + expect (before, fn 18, fn 24, fn 12) + |> toEqual ("10", "29", "41", "12"))); - (* + describe "fn2" (fun _ -> + test "calls implementation" (fun _ -> + let mockFn = JestJs.fn2 (fun [@u] a b -> string_of_int (a + b)) in + let fn = MockJs.fn mockFn in + + expect (call2 fn 18 24) |> toBe "42") + (* test "calls - records call arguments" (fun _ -> let mockFn = JestJs.fn2 ((fun a b -> string_of_int (a + b)) [@u]) in - + let _ = MockJs.fn mockFn 18 24 in let calls = mockFn |> MockJs.calls in - + expect calls |> toEqual [| (18, 24) |] ); - *) -); - -describe "MockJs.new" (fun _ -> - test "MockJs.new0" (fun _ -> - let mockFn = JestJs.fn [%raw "function () { this.n = 42; }"] in - - let instance = mockFn |> MockJs.new0 in - - expect instance |> toEqual [%obj { n = 42 }] - ); + *)); - test "MockJs.new1" (fun _ -> - let mockFn = JestJs.fn [%raw "function (n) { this.n = n; }"] in - - let instance = mockFn |> MockJs.new1 4 in - - expect instance |> toEqual [%obj { n = 4 }] - ); + describe "MockJs.new" (fun _ -> + test "MockJs.new0" (fun _ -> + let mockFn = JestJs.fn [%raw "function () { this.n = 42; }"] in - test "MockJs.new2" (fun _ -> - let mockFn = JestJs.fn2 [%raw "function (a, b) { this.n = a * b; }"] in - - let instance = mockFn |> MockJs.new2 4 7 in - - expect instance |> toEqual [%obj { n = 28 }] - ); -); + let instance = mockFn |> MockJs.new0 in + + expect instance |> toEqual [%obj { n = 42 }]); + + test "MockJs.new1" (fun _ -> + let mockFn = JestJs.fn [%raw "function (n) { this.n = n; }"] in + + let instance = mockFn |> MockJs.new1 4 in + + expect instance |> toEqual [%obj { n = 4 }]); + + test "MockJs.new2" (fun _ -> + let mockFn = + JestJs.fn2 [%raw "function (a, b) { this.n = a * b; }"] + in + + let instance = mockFn |> MockJs.new2 4 7 in + + expect instance |> toEqual [%obj { n = 28 }])) diff --git a/jest/__tests__/reason_syntax_test.re b/jest/__tests__/reason_syntax_test.re index e396fe0..714a750 100644 --- a/jest/__tests__/reason_syntax_test.re +++ b/jest/__tests__/reason_syntax_test.re @@ -1,16 +1,18 @@ open Jest; let () = + describe("Reason Syntax", () => { + open Expect; -describe("Reason Syntax", () => { - open Expect; + test("toBe", () => + expect(1 + 2) |> toBe(3) + ); - test("toBe", () => - expect(1 + 2) |> toBe(3)); + test("not toBe", () => + expect(1 + 2) |> not |> toBe(4) + ); - test("not toBe", () => - expect(1 + 2) |> not |> toBe(4)); - - test("not_ toBe", () => - expect(1 + 2) |> not_ |> toBe(4)); -}); \ No newline at end of file + test("not_ toBe", () => + expect(1 + 2) |> not_ |> toBe(4) + ); + }); diff --git a/jest/__tests__/runner_only_test.ml b/jest/__tests__/runner_only_test.ml index 0e8b1fd..2230b3a 100644 --- a/jest/__tests__/runner_only_test.ml +++ b/jest/__tests__/runner_only_test.ml @@ -1,27 +1,24 @@ -include Jest.Runner(struct +include Jest.Runner (struct type _ t = bool + let affirm ok = assert ok end) let () = - Only.test "Only.test" (fun () -> true); - Only.testAsync "Only.testAsync" (fun finish -> - finish true); - Only.testAsync "testAsync - timeout ok" ~timeout:1 (fun finish -> - finish true); + Only.testAsync "Only.testAsync" (fun finish -> finish true); + Only.testAsync "testAsync - timeout ok" ~timeout:1 (fun finish -> finish true); - Only.testPromise "Only.testPromise" (fun () -> - Js.Promise.resolve true); + Only.testPromise "Only.testPromise" (fun () -> Js.Promise.resolve true); Only.testPromise "testPromise - timeout ok" ~timeout:1 (fun () -> - Js.Promise.resolve true); + Js.Promise.resolve true); - Only.testAll "testAll" ["foo"; "bar"; "baz"] (fun input -> - Js.String.length input == 3); - Only.testAll "testAll - tuples" [("foo", 3); ("barbaz", 6); ("bananas!", 8)] (fun (input, output) -> - Js.String.length input == output); + Only.testAll "testAll" [ "foo"; "bar"; "baz" ] (fun input -> + Js.String.length input == 3); + Only.testAll "testAll - tuples" + [ ("foo", 3); ("barbaz", 6); ("bananas!", 8) ] + (fun (input, output) -> Js.String.length input == output); Only.describe "Only.describe" (fun () -> - test "some aspect" (fun () -> 1 + 2 ==3) - ); \ No newline at end of file + test "some aspect" (fun () -> 1 + 2 == 3)) diff --git a/jest/__tests__/runner_test.ml b/jest/__tests__/runner_test.ml index 0a6990c..dfbf87b 100644 --- a/jest/__tests__/runner_test.ml +++ b/jest/__tests__/runner_test.ml @@ -1,359 +1,329 @@ -include Jest.Runner(struct +include Jest.Runner (struct type _ t = bool + let affirm ok = assert ok end) - let () = - test "test" (fun () -> - true); + test "test" (fun () -> true); - Skip.test "test - expect fail" (fun () -> - false); - + Skip.test "test - expect fail" (fun () -> false); - testAsync "testAsync" (fun finish -> - finish true); + testAsync "testAsync" (fun finish -> finish true); Skip.testAsync "testAsync - no done" (fun _ -> ()); - Skip.testAsync "testAsync - expect fail" (fun finish -> - finish false); + Skip.testAsync "testAsync - expect fail" (fun finish -> finish false); - testAsync "testAsync - timeout ok" ~timeout:1 (fun finish -> - finish true); - - Skip.testAsync "testAsync - timeout fail" ~timeout:1 (fun _ -> ()); + testAsync "testAsync - timeout ok" ~timeout:1 (fun finish -> finish true); + Skip.testAsync "testAsync - timeout fail" ~timeout:1 (fun _ -> ()); - testPromise "testPromise" (fun () -> - Js.Promise.resolve true); + testPromise "testPromise" (fun () -> Js.Promise.resolve true); Skip.testPromise "testPromise - reject" (fun () -> - Js.Promise.reject (Failure "")); + Js.Promise.reject (Failure "")); Skip.testPromise "testPromise - expect fail" (fun () -> - Js.Promise.resolve false); + Js.Promise.resolve false); testPromise "testPromise - timeout ok" ~timeout:1 (fun () -> - Js.Promise.resolve true); - + Js.Promise.resolve true); + Skip.testPromise "testPromise - timeout fail" ~timeout:1 (fun () -> - Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); - - - testAll "testAll" ["foo"; "bar"; "baz"] (fun input -> - Js.String.length input == 3); - testAll "testAll - tuples" [("foo", 3); ("barbaz", 6); ("bananas!", 8)] (fun (input, output) -> - Js.String.length input == output); - - - describe "describe" (fun () -> - test "some aspect" (fun () -> - true) - ); - - describe "beforeAll" (fun () -> - let x = ref 0 in - - beforeAll (fun () -> x := !x + 4); - - test "x is 4" (fun () -> !x == 4); - test "x is still 4" (fun () -> !x == 4); - ); - - describe "beforeAllAsync" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); - beforeAllAsync (fun (finish) -> x := !x + 4; finish ()); + testAll "testAll" [ "foo"; "bar"; "baz" ] (fun input -> + Js.String.length input == 3); + testAll "testAll - tuples" + [ ("foo", 3); ("barbaz", 6); ("bananas!", 8) ] + (fun (input, output) -> Js.String.length input == output); - test "x is 4" (fun () -> !x == 4); - test "x is still 4" (fun () -> !x == 4); - ); + describe "describe" (fun () -> test "some aspect" (fun () -> true)); - describe "with 100ms timeout" (fun () -> + describe "beforeAll" (fun () -> let x = ref 0 in - beforeAllAsync ~timeout:100 (fun (finish) -> x := !x + 4; finish ()); + beforeAll (fun () -> x := !x + 4); test "x is 4" (fun () -> !x == 4); - test "x is still 4" (fun () -> !x == 4); - ); + test "x is still 4" (fun () -> !x == 4)); - Skip.describe "timeout should fail suite" (fun () -> - (* This apparently runs even if the suite is skipped *) - (*beforeAllAsync ~timeout:1 (fun _ ->());*) + describe "beforeAllAsync" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> true); (* runner will crash if there's no tests *) - ); - ); + beforeAllAsync (fun finish -> + x := !x + 4; + finish ()); - describe "beforeAllPromise" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + test "x is 4" (fun () -> !x == 4); + test "x is still 4" (fun () -> !x == 4)); - beforeAllPromise (fun () -> x := !x + 4; Js.Promise.resolve ()); + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "x is 4" (fun () -> !x == 4); - test "x is still 4" (fun () -> !x == 4); - ); + beforeAllAsync ~timeout:100 (fun finish -> + x := !x + 4; + finish ()); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + test "x is 4" (fun () -> !x == 4); + test "x is still 4" (fun () -> !x == 4)); - beforeAllPromise ~timeout:100 (fun () -> x := !x + 4; Js.Promise.resolve ()); + Skip.describe "timeout should fail suite" (fun () -> + (* This apparently runs even if the suite is skipped *) + (*beforeAllAsync ~timeout:1 (fun _ ->());*) + test "" (fun () -> true) + (* runner will crash if there's no tests *))); - test "x is 4" (fun () -> !x == 4); - test "x is still 4" (fun () -> !x == 4); - ); + describe "beforeAllPromise" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - Skip.describe "timeout should fail suite" (fun () -> - (* This apparently runs even if the suite is skipped *) - (*beforeAllPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ()));*) + beforeAllPromise (fun () -> + x := !x + 4; + Js.Promise.resolve ()); - test "" (fun () -> true); (* runner will crash if there's no tests *) - ); - ); + test "x is 4" (fun () -> !x == 4); + test "x is still 4" (fun () -> !x == 4)); - describe "beforeEach" (fun () -> - let x = ref 0 in - - beforeEach (fun () -> x := !x + 4); + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "x is 4" (fun () -> !x == 4); - test "x is suddenly 8" (fun () -> !x == 8); - ); + beforeAllPromise ~timeout:100 (fun () -> + x := !x + 4; + Js.Promise.resolve ()); - describe "beforeEachAsync" (fun () -> - describe "without timeout" (fun () -> + test "x is 4" (fun () -> !x == 4); + test "x is still 4" (fun () -> !x == 4)); + + Skip.describe "timeout should fail suite" (fun () -> + (* This apparently runs even if the suite is skipped *) + (*beforeAllPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ()));*) + test "" (fun () -> true) + (* runner will crash if there's no tests *))); + + describe "beforeEach" (fun () -> let x = ref 0 in - beforeEachAsync (fun (finish) -> x := !x + 4; finish ()); + beforeEach (fun () -> x := !x + 4); test "x is 4" (fun () -> !x == 4); - test "x is suddenly 8" (fun () -> !x == 8); - ); + test "x is suddenly 8" (fun () -> !x == 8)); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "beforeEachAsync" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - beforeEachAsync ~timeout:100 (fun (finish) -> x := !x + 4; finish ()); + beforeEachAsync (fun finish -> + x := !x + 4; + finish ()); - test "x is 4" (fun () -> !x == 4); - test "x is suddenly 8" (fun () -> !x == 8); - ); + test "x is 4" (fun () -> !x == 4); + test "x is suddenly 8" (fun () -> !x == 8)); + + describe "with 100ms timeout" (fun () -> + let x = ref 0 in + + beforeEachAsync ~timeout:100 (fun finish -> + x := !x + 4; + finish ()); - Skip.describe "timeout should fail suite" (fun () -> - beforeEachAsync ~timeout:1 (fun _ -> ()); + test "x is 4" (fun () -> !x == 4); + test "x is suddenly 8" (fun () -> !x == 8)); - test "" (fun () -> true); (* runner will crash if there's no tests *) - ); - ); + Skip.describe "timeout should fail suite" (fun () -> + beforeEachAsync ~timeout:1 (fun _ -> ()); + + test "" (fun () -> true) (* runner will crash if there's no tests *))); describe "beforeEachPromise" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + describe "without timeout" (fun () -> + let x = ref 0 in - beforeEachPromise (fun () -> x := !x + 4; Js.Promise.resolve true); + beforeEachPromise (fun () -> + x := !x + 4; + Js.Promise.resolve true); - test "x is 4" (fun () -> !x == 4); - test "x is suddenly 8" (fun () -> !x == 8); - ); + test "x is 4" (fun () -> !x == 4); + test "x is suddenly 8" (fun () -> !x == 8)); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - beforeEachPromise ~timeout:100 (fun () -> x := !x + 4; Js.Promise.resolve true); + beforeEachPromise ~timeout:100 (fun () -> + x := !x + 4; + Js.Promise.resolve true); - test "x is 4" (fun () -> !x == 4); - test "x is suddenly 8" (fun () -> !x == 8); - ); - - Skip.describe "timeout should fail suite" (fun () -> - beforeEachPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); - - test "" (fun () -> true); (* runner will crash if there's no tests *) - ); - ); - - describe "afterAll" (fun () -> - let x = ref 0 in - - describe "phase 1" (fun () -> - afterAll (fun () -> x := !x + 4); + test "x is 4" (fun () -> !x == 4); + test "x is suddenly 8" (fun () -> !x == 8)); - test "x is 0" (fun () -> !x == 0); - test "x is still 0" (fun () -> !x == 0); - ); - - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> !x == 4); - ); - ); - - describe "afterAllAsync" (fun () -> - describe "without timeout" (fun () -> + Skip.describe "timeout should fail suite" (fun () -> + beforeEachPromise ~timeout:1 (fun () -> + Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); + + test "" (fun () -> true) (* runner will crash if there's no tests *))); + + describe "afterAll" (fun () -> let x = ref 0 in describe "phase 1" (fun () -> - afterAllAsync (fun (finish) -> x := !x + 4; finish ()); + afterAll (fun () -> x := !x + 4); - test "x is 0" (fun () -> !x == 0); - test "x is still 0" (fun () -> !x == 0); - ); + test "x is 0" (fun () -> !x == 0); + test "x is still 0" (fun () -> !x == 0)); - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> !x == 4); - ); - ); + describe "phase 2" (fun () -> test "x is suddenly 4" (fun () -> !x == 4))); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "afterAllAsync" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in - describe "phase 1" (fun () -> - afterAllAsync ~timeout:100 (fun (finish) -> x := !x + 4; finish ()); + describe "phase 1" (fun () -> + afterAllAsync (fun finish -> + x := !x + 4; + finish ()); + + test "x is 0" (fun () -> !x == 0); + test "x is still 0" (fun () -> !x == 0)); + + describe "phase 2" (fun () -> + test "x is suddenly 4" (fun () -> !x == 4))); + + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "x is 0" (fun () -> !x == 0); - test "x is still 0" (fun () -> !x == 0); - ); + describe "phase 1" (fun () -> + afterAllAsync ~timeout:100 (fun finish -> + x := !x + 4; + finish ()); - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> !x == 4); - ); - ); + test "x is 0" (fun () -> !x == 0); + test "x is still 0" (fun () -> !x == 0)); - describe "timeout should not fail suite" (fun () -> - (* In jest 27, they do. *) - (* afterAllAsync ~timeout:1 (fun _ ->()); *) + describe "phase 2" (fun () -> + test "x is suddenly 4" (fun () -> !x == 4))); - test "" (fun () -> true); (* runner will crash if there's no tests *) - ); - ); + describe "timeout should not fail suite" (fun () -> + (* In jest 27, they do. *) + (* afterAllAsync ~timeout:1 (fun _ ->()); *) + test "" (fun () -> true) + (* runner will crash if there's no tests *))); describe "afterAllPromise" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + describe "without timeout" (fun () -> + let x = ref 0 in - describe "phase 1" (fun () -> - afterAllPromise (fun () -> x := !x + 4; Js.Promise.resolve true); + describe "phase 1" (fun () -> + afterAllPromise (fun () -> + x := !x + 4; + Js.Promise.resolve true); - test "x is 0" (fun () -> !x == 0); - test "x is still 0" (fun () -> !x == 0); - ); + test "x is 0" (fun () -> !x == 0); + test "x is still 0" (fun () -> !x == 0)); - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> !x == 4); - ); - ); + describe "phase 2" (fun () -> + test "x is suddenly 4" (fun () -> !x == 4))); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - describe "phase 1" (fun () -> - afterAllPromise ~timeout:100 (fun () -> x := !x + 4; Js.Promise.resolve true); - - test "x is 0" (fun () -> !x == 0); - test "x is still 0" (fun () -> !x == 0); - ); - - describe "phase 2" (fun () -> - test "x is suddenly 4" (fun () -> !x == 4); - ); - ); - - describe "timeout should not fail suite" (fun () -> - (* In jest 27, they do. *) - (* afterAllPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); *) - - test "" (fun () -> true); (* runner will crash if there's no tests *) - ); - ); - - describe "afterEach" (fun () -> - let x = ref 0 in - - afterEach (fun () -> x := !x + 4); - - test "x is 0" (fun () -> !x == 0); - test "x is suddenly 4" (fun () -> !x == 4); - ); - - describe "afterEachAsync" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + describe "phase 1" (fun () -> + afterAllPromise ~timeout:100 (fun () -> + x := !x + 4; + Js.Promise.resolve true); - afterEachAsync (fun (finish) -> x := !x + 4; finish ()); + test "x is 0" (fun () -> !x == 0); + test "x is still 0" (fun () -> !x == 0)); - test "x is 0" (fun () -> !x == 0); - test "x is suddenly 4" (fun () -> !x == 4); - ); + describe "phase 2" (fun () -> + test "x is suddenly 4" (fun () -> !x == 4))); + + describe "timeout should not fail suite" (fun () -> + (* In jest 27, they do. *) + (* afterAllPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); *) + test "" (fun () -> true) + (* runner will crash if there's no tests *))); - describe "with 100ms timeout" (fun () -> + describe "afterEach" (fun () -> let x = ref 0 in - afterEachAsync ~timeout:100 (fun (finish) -> x := !x + 4; finish ()); + afterEach (fun () -> x := !x + 4); test "x is 0" (fun () -> !x == 0); - test "x is suddenly 4" (fun () -> !x == 4); - ); + test "x is suddenly 4" (fun () -> !x == 4)); - Skip.describe "timeout should fail suite" (fun () -> - afterEachAsync ~timeout:1 (fun _ -> ()); + describe "afterEachAsync" (fun () -> + describe "without timeout" (fun () -> + let x = ref 0 in + + afterEachAsync (fun finish -> + x := !x + 4; + finish ()); + + test "x is 0" (fun () -> !x == 0); + test "x is suddenly 4" (fun () -> !x == 4)); + + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - test "" (fun () -> true); (* runner will crash if there's no tests *) - ); - ); + afterEachAsync ~timeout:100 (fun finish -> + x := !x + 4; + finish ()); + + test "x is 0" (fun () -> !x == 0); + test "x is suddenly 4" (fun () -> !x == 4)); + + Skip.describe "timeout should fail suite" (fun () -> + afterEachAsync ~timeout:1 (fun _ -> ()); + + test "" (fun () -> true) (* runner will crash if there's no tests *))); describe "afterEachPromise" (fun () -> - describe "without timeout" (fun () -> - let x = ref 0 in + describe "without timeout" (fun () -> + let x = ref 0 in - afterEachPromise (fun () -> x := !x + 4; Js.Promise.resolve true); + afterEachPromise (fun () -> + x := !x + 4; + Js.Promise.resolve true); - test "x is 0" (fun () -> !x == 0); - test "x is suddenly 4" (fun () -> !x == 4); - ); + test "x is 0" (fun () -> !x == 0); + test "x is suddenly 4" (fun () -> !x == 4)); - describe "with 100ms timeout" (fun () -> - let x = ref 0 in + describe "with 100ms timeout" (fun () -> + let x = ref 0 in - afterEachPromise ~timeout:100 (fun () -> x := !x + 4; Js.Promise.resolve true); + afterEachPromise ~timeout:100 (fun () -> + x := !x + 4; + Js.Promise.resolve true); - test "x is 0" (fun () -> !x == 0); - test "x is suddenly 4" (fun () -> !x == 4); - ); + test "x is 0" (fun () -> !x == 0); + test "x is suddenly 4" (fun () -> !x == 4)); + + Skip.describe "timeout should fail suite" (fun () -> + afterEachPromise ~timeout:1 (fun () -> + Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); - Skip.describe "timeout should fail suite" (fun () -> - afterEachPromise ~timeout:1 (fun () -> Js.Promise.make (fun ~resolve:_ ~reject:_ -> ())); + test "" (fun () -> true) (* runner will crash if there's no tests *))); - test "" (fun () -> true); (* runner will crash if there's no tests *) - ); - ); - describe "Only" (fun () -> - (* See runner_only_test.ml *) - () - ); + (* See runner_only_test.ml *) + ()); describe "Skip" (fun () -> - Skip.test "Skip.test" (fun () -> false); + Skip.test "Skip.test" (fun () -> false); - Skip.testAsync "Skip.testAsync" (fun finish -> - finish false); - Skip.testAsync "Skip.testAsync - timeout" ~timeout:1 (fun _ -> ()); + Skip.testAsync "Skip.testAsync" (fun finish -> finish false); + Skip.testAsync "Skip.testAsync - timeout" ~timeout:1 (fun _ -> ()); - Skip.testPromise "Skip.testPromise" (fun () -> - Js.Promise.resolve false); - Skip.testPromise "testPromise - timeout" ~timeout:1 (fun () -> - Js.Promise.make (fun ~resolve ~reject:_ -> (resolve false)[@u])); - - Skip.testAll "testAll" ["foo"; "bar"; "baz"] (fun _ -> - false); - Skip.testAll "testAll - tuples" [("foo", 3); ("barbaz", 6); ("bananas!", 8)] (fun (_, _) -> - false); - - Skip.describe "Skip.describe" (fun () -> - test "some aspect" (fun () -> false) - ); - ); + Skip.testPromise "Skip.testPromise" (fun () -> Js.Promise.resolve false); + Skip.testPromise "testPromise - timeout" ~timeout:1 (fun () -> + Js.Promise.make (fun ~resolve ~reject:_ -> (resolve false [@u]))); + + Skip.testAll "testAll" [ "foo"; "bar"; "baz" ] (fun _ -> false); + Skip.testAll "testAll - tuples" + [ ("foo", 3); ("barbaz", 6); ("bananas!", 8) ] + (fun (_, _) -> false); + + Skip.describe "Skip.describe" (fun () -> + test "some aspect" (fun () -> false))) diff --git a/jest/jest.ml b/jest/jest.ml index fa5a405..2e7145b 100644 --- a/jest/jest.ml +++ b/jest/jest.ml @@ -1,56 +1,49 @@ -type 'a modifier = [ -| `Just of 'a -| `Not of 'a -] +type 'a modifier = [ `Just of 'a | `Not of 'a ] -let mapMod f = function -| `Just a -> `Just (f a) -| `Not a -> `Not (f a) +let mapMod f = function `Just a -> `Just (f a) | `Not a -> `Not (f a) type assertion = -| Ok : assertion -| Fail : string -> assertion - -| ArrayContains : ('a array * 'a) modifier -> assertion -| ArrayContainsEqual : ('a array * 'a) modifier -> assertion -| ArrayLength : ('a array * int) modifier -> assertion -| ArraySuperset : ('a array * 'a array) modifier -> assertion -| Be : ('a * 'a) modifier -> assertion -| Equal : ('a * 'a) modifier -> assertion -| FloatCloseTo : (float * float * int option) modifier -> assertion -| GreaterThan : ('a * 'a) modifier -> assertion -| GreaterThanOrEqual : ('a * 'a) modifier -> assertion -| LessThan : ('a * 'a) modifier -> assertion -| LessThanOrEqual : ('a * 'a) modifier -> assertion -| StringContains : (string * string) modifier -> assertion -| StringMatch : (string * Js.Re.t) modifier -> assertion - -| Throws : (unit -> _) modifier -> assertion - -| MatchInlineSnapshot : _ * string -> assertion -| MatchSnapshot : _ -> assertion -| MatchSnapshotName : _ * string -> assertion -| ThrowsMatchSnapshot : (unit -> _) -> assertion - -(* JS *) -| Defined : ('a Js.undefined) modifier -> assertion -| Falsy : 'a modifier -> assertion -| Null : _ Js.null modifier -> assertion -| Truthy : 'a modifier -> assertion -| Undefined : 'a Js.undefined modifier -> assertion -| ObjectContains : (< .. > Js.t * string array) modifier -> assertion -| ObjectMatch : (< .. > Js.t * < .. > Js.t) modifier -> assertion + | Ok : assertion + | Fail : string -> assertion + | ArrayContains : ('a array * 'a) modifier -> assertion + | ArrayContainsEqual : ('a array * 'a) modifier -> assertion + | ArrayLength : ('a array * int) modifier -> assertion + | ArraySuperset : ('a array * 'a array) modifier -> assertion + | Be : ('a * 'a) modifier -> assertion + | Equal : ('a * 'a) modifier -> assertion + | FloatCloseTo : (float * float * int option) modifier -> assertion + | GreaterThan : ('a * 'a) modifier -> assertion + | GreaterThanOrEqual : ('a * 'a) modifier -> assertion + | LessThan : ('a * 'a) modifier -> assertion + | LessThanOrEqual : ('a * 'a) modifier -> assertion + | StringContains : (string * string) modifier -> assertion + | StringMatch : (string * Js.Re.t) modifier -> assertion + | Throws : (unit -> _) modifier -> assertion + | MatchInlineSnapshot : _ * string -> assertion + | MatchSnapshot : _ -> assertion + | MatchSnapshotName : _ * string -> assertion + | ThrowsMatchSnapshot : (unit -> _) -> assertion + (* JS *) + | Defined : 'a Js.undefined modifier -> assertion + | Falsy : 'a modifier -> assertion + | Null : _ Js.null modifier -> assertion + | Truthy : 'a modifier -> assertion + | Undefined : 'a Js.undefined modifier -> assertion + | ObjectContains : (< .. > Js.t * string array) modifier -> assertion + | ObjectMatch : (< .. > Js.t * < .. > Js.t) modifier -> assertion module type Asserter = sig type 'a t + val affirm : 'a t -> unit end -external inputAsString: 'a -> string = "%identity" +external inputAsString : 'a -> string = "%identity" (* internal *) module LLExpect : sig type 'a t = assertion + val affirm : 'a t -> unit end = struct type 'a t = assertion @@ -60,7 +53,10 @@ end = struct external fail : string -> unit = "fail" external arrayContaining : 'a array -> specialMatch = "expect.arrayContaining" external stringContaining : string -> specialMatch = "expect.stringContaining" - let objectContaining : string array -> < .. > Js.t = [%raw {| + + let objectContaining : string array -> < .. > Js.t = + [%raw + {| function (properties) { var spec = {}; properties.forEach(function (property) { @@ -71,202 +67,301 @@ end = struct |}] let affirm = function - | Ok -> () - | Fail message -> fail message - - | ArrayContains `Just (a, b) -> (expect a) ## toContain b - | ArrayContains `Not (a, b) -> (expect a) ## not ## toContain b - | ArrayContainsEqual `Just (a, b) -> (expect a) ## toContainEqual b - | ArrayContainsEqual `Not (a, b) -> (expect a) ## not ## toContainEqual b - | ArrayLength `Just (a, l) -> (expect a) ## toHaveLength l - | ArrayLength `Not (a, l) -> (expect a) ## not ## toHaveLength l - | ArraySuperset `Just (a, b) -> (expect a) ## toEqual (arrayContaining b) - | ArraySuperset `Not (a, b) -> (expect a) ## not ## toEqual (arrayContaining b) - | Be `Just (a, b) -> (expect a) ## toBe b - | Be `Not (a, b) -> (expect a) ## not ## toBe b - | Equal `Just (a, b) -> (expect a) ## toEqual b - | Equal `Not (a, b) -> (expect a) ## not ## toEqual b - | FloatCloseTo `Just (a, b, p) -> (expect a) ## toBeCloseTo b (Js.Undefined.fromOption p) - | FloatCloseTo `Not (a, b, p) -> (expect a) ## not ## toBeCloseTo b (Js.Undefined.fromOption p) - | GreaterThan `Just (a, b) -> (expect a) ## toBeGreaterThan b - | GreaterThan `Not (a, b) -> (expect a) ## not ## toBeGreaterThan b - | GreaterThanOrEqual `Just (a, b) -> (expect a) ## toBeGreaterThanOrEqual b - | GreaterThanOrEqual `Not (a, b) -> (expect a) ## not ## toBeGreaterThanOrEqual b - | LessThan `Just (a, b) -> (expect a) ## toBeLessThan b - | LessThan `Not (a, b) -> (expect a) ## not ## toBeLessThan b - | LessThanOrEqual `Just (a, b) -> (expect a) ## toBeLessThanOrEqual b - | LessThanOrEqual `Not (a, b) -> (expect a) ## not ## toBeLessThanOrEqual b - | StringMatch `Just (s, re) -> (expect s) ## toMatch re - | StringMatch `Not (s, re) -> (expect s) ## not ## toMatch re - | StringContains `Just (a, b) -> (expect a) ## toEqual (stringContaining b) - | StringContains `Not (a, b) -> (expect a) ## not ## toEqual (stringContaining b) - - | Throws `Just f -> (expect f) ## toThrow () - | Throws `Not f -> (expect f) ## not ## toThrow () - - | MatchInlineSnapshot (a, inlineSnapshot) -> (expect a) ## toMatchInlineSnapshot inlineSnapshot - | MatchSnapshot a -> (expect a) ## toMatchSnapshot () - | MatchSnapshotName (a, name) -> (expect a) ## toMatchSnapshot name - | ThrowsMatchSnapshot f -> (expect f) ## toThrowErrorMatchingSnapshot () - - (* JS *) - | Defined `Just a -> (expect a) ## toBeDefined () - | Defined `Not a -> (expect a) ## not ## toBeDefined () - | Falsy `Just a -> (expect a) ## toBeFalsy () - | Falsy `Not a -> (expect a) ## not ## toBeFalsy () - | Null `Just a -> (expect a) ## toBeNull () - | Null `Not a -> (expect a) ## not ## toBeNull () - | Truthy `Just a -> (expect a) ## toBeTruthy () - | Truthy `Not a -> (expect a) ## not ## toBeTruthy () - | Undefined `Just a -> (expect a) ## toBeUndefined () - | Undefined `Not a -> (expect a) ## not ## toBeUndefined () - | ObjectContains `Just (a, props) -> (expect a) ## toEqual (objectContaining props) - | ObjectContains `Not (a, props) -> (expect a) ## not ## toEqual (objectContaining props) - | ObjectMatch `Just (a, b) -> (expect a) ## toMatchObject b - | ObjectMatch `Not (a, b) -> (expect a) ## not ## toMatchObject b + | Ok -> () + | Fail message -> fail message + | ArrayContains (`Just (a, b)) -> (expect a)##toContain b + | ArrayContains (`Not (a, b)) -> (expect a)##not##toContain b + | ArrayContainsEqual (`Just (a, b)) -> (expect a)##toContainEqual b + | ArrayContainsEqual (`Not (a, b)) -> (expect a)##not##toContainEqual b + | ArrayLength (`Just (a, l)) -> (expect a)##toHaveLength l + | ArrayLength (`Not (a, l)) -> (expect a)##not##toHaveLength l + | ArraySuperset (`Just (a, b)) -> (expect a)##toEqual (arrayContaining b) + | ArraySuperset (`Not (a, b)) -> + (expect a)##not##toEqual (arrayContaining b) + | Be (`Just (a, b)) -> (expect a)##toBe b + | Be (`Not (a, b)) -> (expect a)##not##toBe b + | Equal (`Just (a, b)) -> (expect a)##toEqual b + | Equal (`Not (a, b)) -> (expect a)##not##toEqual b + | FloatCloseTo (`Just (a, b, p)) -> + (expect a)##toBeCloseTo b (Js.Undefined.fromOption p) + | FloatCloseTo (`Not (a, b, p)) -> + (expect a)##not##toBeCloseTo b (Js.Undefined.fromOption p) + | GreaterThan (`Just (a, b)) -> (expect a)##toBeGreaterThan b + | GreaterThan (`Not (a, b)) -> (expect a)##not##toBeGreaterThan b + | GreaterThanOrEqual (`Just (a, b)) -> (expect a)##toBeGreaterThanOrEqual b + | GreaterThanOrEqual (`Not (a, b)) -> + (expect a)##not##toBeGreaterThanOrEqual b + | LessThan (`Just (a, b)) -> (expect a)##toBeLessThan b + | LessThan (`Not (a, b)) -> (expect a)##not##toBeLessThan b + | LessThanOrEqual (`Just (a, b)) -> (expect a)##toBeLessThanOrEqual b + | LessThanOrEqual (`Not (a, b)) -> (expect a)##not##toBeLessThanOrEqual b + | StringMatch (`Just (s, re)) -> (expect s)##toMatch re + | StringMatch (`Not (s, re)) -> (expect s)##not##toMatch re + | StringContains (`Just (a, b)) -> (expect a)##toEqual (stringContaining b) + | StringContains (`Not (a, b)) -> + (expect a)##not##toEqual (stringContaining b) + | Throws (`Just f) -> (expect f)##toThrow () + | Throws (`Not f) -> (expect f)##not##toThrow () + | MatchInlineSnapshot (a, inlineSnapshot) -> + (expect a)##toMatchInlineSnapshot inlineSnapshot + | MatchSnapshot a -> (expect a)##toMatchSnapshot () + | MatchSnapshotName (a, name) -> (expect a)##toMatchSnapshot name + | ThrowsMatchSnapshot f -> (expect f)##toThrowErrorMatchingSnapshot () + (* JS *) + | Defined (`Just a) -> (expect a)##toBeDefined () + | Defined (`Not a) -> (expect a)##not##toBeDefined () + | Falsy (`Just a) -> (expect a)##toBeFalsy () + | Falsy (`Not a) -> (expect a)##not##toBeFalsy () + | Null (`Just a) -> (expect a)##toBeNull () + | Null (`Not a) -> (expect a)##not##toBeNull () + | Truthy (`Just a) -> (expect a)##toBeTruthy () + | Truthy (`Not a) -> (expect a)##not##toBeTruthy () + | Undefined (`Just a) -> (expect a)##toBeUndefined () + | Undefined (`Not a) -> (expect a)##not##toBeUndefined () + | ObjectContains (`Just (a, props)) -> + (expect a)##toEqual (objectContaining props) + | ObjectContains (`Not (a, props)) -> + (expect a)##not##toEqual (objectContaining props) + | ObjectMatch (`Just (a, b)) -> (expect a)##toMatchObject b + | ObjectMatch (`Not (a, b)) -> (expect a)##not##toMatchObject b end module Runner (A : Asserter) = struct let affirm = A.affirm - external _test : string -> (unit -> unit Js.undefined [@mel.uncurry]) -> unit = "test" - external _testAsync : string -> ((unit -> unit [@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit = "test" - external _testPromise : string -> (unit -> 'a Js.Promise.t [@mel.uncurry]) -> int Js.Undefined.t -> unit = "test" + + external _test : string -> ((unit -> unit Js.undefined)[@mel.uncurry]) -> unit + = "test" + + external _testAsync : + string -> + (((unit -> unit)[@u]) -> unit Js.undefined) -> + int Js.Undefined.t -> + unit = "test" + + external _testPromise : + string -> + ((unit -> 'a Js.Promise.t)[@mel.uncurry]) -> + int Js.Undefined.t -> + unit = "test" let test name callback = _test name (fun () -> - affirm @@ callback (); - Js.undefined) + affirm @@ callback (); + Js.undefined) let testAsync name ?timeout callback = - _testAsync name (fun finish -> - callback (fun case -> - affirm case; - finish () [@u]); - Js.undefined) + _testAsync name + (fun finish -> + callback (fun case -> + affirm case; + finish () [@u]); + Js.undefined) (Js.Undefined.fromOption timeout) let testPromise name ?timeout callback = - _testPromise name (fun () -> - callback () |> Js.Promise.then_ (fun a -> a |> A.affirm |> Js.Promise.resolve)) + _testPromise name + (fun () -> + callback () + |> Js.Promise.then_ (fun a -> a |> A.affirm |> Js.Promise.resolve)) (Js.Undefined.fromOption timeout) let testAll name inputs callback = - inputs |> List.iter (fun input -> - let name = - let input = inputAsString input in - {j|$name - $input|j} in - _test name (fun () -> - affirm @@ callback input; - Js.undefined)) + inputs + |> List.iter (fun input -> + let name = + let input = inputAsString input in + {j|$name - $input|j} + in + _test name (fun () -> + affirm @@ callback input; + Js.undefined)) + + external describe : + string -> ((unit -> unit Js.undefined)[@mel.uncurry]) -> unit = "describe" - external describe : string -> (unit -> unit Js.undefined [@mel.uncurry]) -> unit = "describe" let describe label f = - describe label (fun () -> f (); Js.undefined) + describe label (fun () -> + f (); + Js.undefined) - external beforeAll : (unit -> unit [@mel.uncurry]) -> unit = "beforeAll" - external beforeAllAsync : ((unit -> unit [@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit = "beforeAll" - let beforeAllAsync ?timeout callback = + external beforeAll : ((unit -> unit)[@mel.uncurry]) -> unit = "beforeAll" + + external beforeAllAsync : + (((unit -> unit)[@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit + = "beforeAll" + + let beforeAllAsync ?timeout callback = beforeAllAsync - (fun finish -> callback (fun () -> finish () [@u]); Js.undefined) + (fun finish -> + callback (fun () -> (finish () [@u])); + Js.undefined) (Js.Undefined.fromOption timeout) - external beforeAllPromise : (unit -> 'a Js.Promise.t [@mel.uncurry]) -> int Js.Undefined.t -> unit = "beforeAll" + + external beforeAllPromise : + ((unit -> 'a Js.Promise.t)[@mel.uncurry]) -> int Js.Undefined.t -> unit + = "beforeAll" + let beforeAllPromise ?timeout callback = beforeAllPromise (fun () -> callback () |> Js.Promise.resolve) (Js.Undefined.fromOption timeout) - external beforeEach : (unit -> unit [@mel.uncurry]) -> unit = "beforeEach" - external beforeEachAsync : ((unit -> unit [@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit = "beforeEach" - let beforeEachAsync ?timeout callback = + external beforeEach : ((unit -> unit)[@mel.uncurry]) -> unit = "beforeEach" + + external beforeEachAsync : + (((unit -> unit)[@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit + = "beforeEach" + + let beforeEachAsync ?timeout callback = beforeEachAsync - (fun finish -> callback (fun () -> finish () [@u]); Js.undefined) + (fun finish -> + callback (fun () -> (finish () [@u])); + Js.undefined) (Js.Undefined.fromOption timeout) - external beforeEachPromise : (unit -> 'a Js.Promise.t [@mel.uncurry]) -> int Js.Undefined.t -> unit = "beforeEach" + + external beforeEachPromise : + ((unit -> 'a Js.Promise.t)[@mel.uncurry]) -> int Js.Undefined.t -> unit + = "beforeEach" + let beforeEachPromise ?timeout callback = beforeEachPromise (fun () -> callback () |> Js.Promise.resolve) (Js.Undefined.fromOption timeout) - external afterAll : (unit -> unit [@mel.uncurry]) -> unit = "afterAll" - external afterAllAsync : ((unit -> unit [@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit = "afterAll" + external afterAll : ((unit -> unit)[@mel.uncurry]) -> unit = "afterAll" + + external afterAllAsync : + (((unit -> unit)[@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit + = "afterAll" + let afterAllAsync ?timeout callback = afterAllAsync - (fun finish -> callback (fun () -> finish () [@u]); Js.undefined) + (fun finish -> + callback (fun () -> (finish () [@u])); + Js.undefined) (Js.Undefined.fromOption timeout) - external afterAllPromise : (unit -> 'a Js.Promise.t [@mel.uncurry]) -> int Js.Undefined.t -> unit = "afterAll" + + external afterAllPromise : + ((unit -> 'a Js.Promise.t)[@mel.uncurry]) -> int Js.Undefined.t -> unit + = "afterAll" + let afterAllPromise ?timeout callback = afterAllPromise (fun () -> callback () |> Js.Promise.resolve) (Js.Undefined.fromOption timeout) - external afterEach : (unit -> unit [@mel.uncurry]) -> unit = "afterEach" - external afterEachAsync : ((unit -> unit [@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit = "afterEach" + external afterEach : ((unit -> unit)[@mel.uncurry]) -> unit = "afterEach" + + external afterEachAsync : + (((unit -> unit)[@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit + = "afterEach" + let afterEachAsync ?timeout callback = afterEachAsync - (fun finish -> callback (fun () -> finish () [@u]); Js.undefined) + (fun finish -> + callback (fun () -> (finish () [@u])); + Js.undefined) (Js.Undefined.fromOption timeout) - external afterEachPromise : (unit -> 'a Js.Promise.t [@mel.uncurry]) -> int Js.Undefined.t -> unit = "afterEach" + + external afterEachPromise : + ((unit -> 'a Js.Promise.t)[@mel.uncurry]) -> int Js.Undefined.t -> unit + = "afterEach" + let afterEachPromise ?timeout callback = afterEachPromise (fun () -> callback () |> Js.Promise.resolve) (Js.Undefined.fromOption timeout) module Only = struct - external _test : string -> (unit -> unit Js.undefined [@mel.uncurry]) -> unit = "it.only" - external _testAsync : string -> ((unit -> unit [@u]) -> unit Js.undefined) -> int Js.Undefined.t -> unit = "it.only" - external _testPromise : string -> (unit -> 'a Js.Promise.t [@mel.uncurry]) -> int Js.Undefined.t -> unit = "it.only" + external _test : + string -> ((unit -> unit Js.undefined)[@mel.uncurry]) -> unit = "it.only" + + external _testAsync : + string -> + (((unit -> unit)[@u]) -> unit Js.undefined) -> + int Js.Undefined.t -> + unit = "it.only" + + external _testPromise : + string -> + ((unit -> 'a Js.Promise.t)[@mel.uncurry]) -> + int Js.Undefined.t -> + unit = "it.only" let test name callback = _test name (fun () -> - affirm @@ callback (); - Js.undefined) + affirm @@ callback (); + Js.undefined) let testAsync name ?timeout callback = - _testAsync name (fun finish -> - callback (fun assertion -> - affirm assertion; - finish () [@u]); - Js.undefined) + _testAsync name + (fun finish -> + callback (fun assertion -> + affirm assertion; + finish () [@u]); + Js.undefined) (Js.Undefined.fromOption timeout) let testPromise name ?timeout callback = - _testPromise name (fun () -> - callback () |> Js.Promise.then_ (fun a -> a |> affirm |> Js.Promise.resolve)) + _testPromise name + (fun () -> + callback () + |> Js.Promise.then_ (fun a -> a |> affirm |> Js.Promise.resolve)) (Js.Undefined.fromOption timeout) let testAll name inputs callback = - inputs |> List.iter (fun input -> - let name = - let input: string = inputAsString input in - {j|$name - $input|j} - in - _test name (fun () -> - affirm @@ callback input; - Js.undefined)) - - external describe : string -> (unit -> unit Js.undefined [@mel.uncurry]) -> unit = "describe.only" + inputs + |> List.iter (fun input -> + let name = + let input : string = inputAsString input in + {j|$name - $input|j} + in + _test name (fun () -> + affirm @@ callback input; + Js.undefined)) + + external describe : + string -> ((unit -> unit Js.undefined)[@mel.uncurry]) -> unit + = "describe.only" + let describe label f = - describe label (fun () -> f (); Js.undefined) + describe label (fun () -> + f (); + Js.undefined) end module Skip = struct - external test : string -> (unit -> 'a A.t [@mel.uncurry]) -> unit = "it.skip" - external testAsync : string -> (('a A.t -> unit) -> unit) -> unit = "it.skip" - let testAsync name ?timeout:_ callback = - testAsync name callback - external testPromise : string -> (unit -> 'a A.t Js.Promise.t [@mel.uncurry]) -> unit = "it.skip" - let testPromise name ?timeout:_ callback = - testPromise name callback + external test : string -> ((unit -> 'a A.t)[@mel.uncurry]) -> unit + = "it.skip" + + external testAsync : string -> (('a A.t -> unit) -> unit) -> unit + = "it.skip" + + let testAsync name ?timeout:_ callback = testAsync name callback + + external testPromise : + string -> ((unit -> 'a A.t Js.Promise.t)[@mel.uncurry]) -> unit + = "it.skip" + + let testPromise name ?timeout:_ callback = testPromise name callback + let testAll name inputs callback = - inputs |> List.iter (fun input -> - let name = - let input: string = inputAsString input in - {j|$name - $input|j} - in - test name (fun () -> callback input)) - external describe : string -> (unit -> unit Js.undefined [@mel.uncurry]) -> unit = "describe.skip" + inputs + |> List.iter (fun input -> + let name = + let input : string = inputAsString input in + {j|$name - $input|j} + in + test name (fun () -> callback input)) + + external describe : + string -> ((unit -> unit Js.undefined)[@mel.uncurry]) -> unit + = "describe.skip" + let describe label f = - describe label (fun () -> f (); Js.undefined) + describe label (fun () -> + f (); + Js.undefined) end module Todo = struct @@ -274,7 +369,7 @@ module Runner (A : Asserter) = struct end end -include Runner(LLExpect) +include Runner (LLExpect) let pass = Ok let fail message = Fail message @@ -290,94 +385,66 @@ let fail message = Fail message *) module Expect = struct - type 'a plainPartial = [`Just of 'a] - type 'a invertedPartial = [`Not of 'a] + type 'a plainPartial = [ `Just of 'a ] + type 'a invertedPartial = [ `Not of 'a ] type 'a partial = 'a modifier - let expect a = - `Just a - - let expectFn f a = - `Just (fun () -> f a) - - let toBe b p = - Be (mapMod (fun a -> (a, b)) p) + let expect a = `Just a + let expectFn f a = `Just (fun () -> f a) + let toBe b p = Be (mapMod (fun a -> (a, b)) p) (* toHaveBeenCalled* *) - let toBeCloseTo b p = - FloatCloseTo (mapMod (fun a -> (a, b, None)) p) + let toBeCloseTo b p = FloatCloseTo (mapMod (fun a -> (a, b, None)) p) let toBeSoCloseTo b ~digits p = FloatCloseTo (mapMod (fun a -> (a, b, Some digits)) p) - let toBeGreaterThan b p = - GreaterThan (mapMod (fun a -> (a, b)) p) + let toBeGreaterThan b p = GreaterThan (mapMod (fun a -> (a, b)) p) let toBeGreaterThanOrEqual b p = GreaterThanOrEqual (mapMod (fun a -> (a, b)) p) - let toBeLessThan b p = - LessThan (mapMod (fun a -> (a, b)) p) - - let toBeLessThanOrEqual b p = - LessThanOrEqual (mapMod (fun a -> (a, b)) p) + let toBeLessThan b p = LessThan (mapMod (fun a -> (a, b)) p) + let toBeLessThanOrEqual b p = LessThanOrEqual (mapMod (fun a -> (a, b)) p) (** replaces expect.arrayContaining *) - let toBeSupersetOf b p = - ArraySuperset (mapMod (fun a -> (a, b)) p) - - let toContain b p = - ArrayContains (mapMod (fun a -> (a, b)) p) + let toBeSupersetOf b p = ArraySuperset (mapMod (fun a -> (a, b)) p) - let toContainEqual b p = - ArrayContainsEqual (mapMod (fun a -> (a, b)) p) + let toContain b p = ArrayContains (mapMod (fun a -> (a, b)) p) + let toContainEqual b p = ArrayContainsEqual (mapMod (fun a -> (a, b)) p) (** replaces expect.stringContaining *) - let toContainString b p = - StringContains (mapMod (fun a -> (a, b)) p) + let toContainString b p = StringContains (mapMod (fun a -> (a, b)) p) - let toEqual b p = - Equal (mapMod (fun a -> (a, b)) p) - - let toHaveLength l p = - ArrayLength (mapMod (fun a -> (a, l)) p) - - let toMatch s p = - StringMatch (mapMod (fun a -> (a, Js.Re.fromString s)) p) + let toEqual b p = Equal (mapMod (fun a -> (a, b)) p) + let toHaveLength l p = ArrayLength (mapMod (fun a -> (a, l)) p) + let toMatch s p = StringMatch (mapMod (fun a -> (a, Js.Re.fromString s)) p) let toMatchInlineSnapshot inlineSnapshot (`Just a) = MatchInlineSnapshot (a, inlineSnapshot) - let toMatchRe re p = - StringMatch (mapMod (fun a -> (a, re)) p) - - let toMatchSnapshot (`Just a) = - MatchSnapshot a - - let toMatchSnapshotWithName name (`Just a) = - MatchSnapshotName (a, name) - - let toThrow f = - Throws (f :> _ modifier) - - let toThrowErrorMatchingSnapshot (`Just f) = - ThrowsMatchSnapshot f - + let toMatchRe re p = StringMatch (mapMod (fun a -> (a, re)) p) + let toMatchSnapshot (`Just a) = MatchSnapshot a + let toMatchSnapshotWithName name (`Just a) = MatchSnapshotName (a, name) + let toThrow f = Throws (f :> _ modifier) + let toThrowErrorMatchingSnapshot (`Just f) = ThrowsMatchSnapshot f let not_ (`Just a) = `Not a - let not__ = not_ (* For Reason syntax compatibility. TODO: deprecate and remove *) + + let not__ = + not_ (* For Reason syntax compatibility. TODO: deprecate and remove *) module Operators = struct (** experimental *) - let (==) = fun a b -> toBe b a - let (>) = fun a b -> toBeGreaterThan b a - let (>=) = fun a b -> toBeGreaterThanOrEqual b a - let (<) = fun a b -> toBeLessThan b a - let (<=) = fun a b -> toBeLessThanOrEqual b a - let (=) = fun a b -> toEqual b a - let (<>) = fun a b -> a |> not_ |> toEqual b - let (!=) = fun a b -> a |> not_ |> toBe b + let ( == ) = fun a b -> toBe b a + let ( > ) = fun a b -> toBeGreaterThan b a + let ( >= ) = fun a b -> toBeGreaterThanOrEqual b a + let ( < ) = fun a b -> toBeLessThan b a + let ( <= ) = fun a b -> toBeLessThanOrEqual b a + let ( = ) = fun a b -> toEqual b a + let ( <> ) = fun a b -> a |> not_ |> toEqual b + let ( != ) = fun a b -> a |> not_ |> toBe b end end @@ -386,6 +453,7 @@ module ExpectJs = struct let toBeDefined a = Defined (a :> _ modifier) let toBeFalsy a = Falsy (a :> _ modifier) + (* toBeInstanceOf *) let toBeNull a = Null (a :> _ modifier) let toBeTruthy a = Truthy (a :> _ modifier) @@ -395,8 +463,7 @@ module ExpectJs = struct let toContainProperties props p = ObjectContains (mapMod (fun a -> (a, props)) p) - let toMatchObject b p = - ObjectMatch (mapMod (fun a -> (a, b)) p) + let toMatchObject b p = ObjectMatch (mapMod (fun a -> (a, b)) p) end module MockJs = struct @@ -404,36 +471,80 @@ module MockJs = struct type ('fn, 'args, 'ret) fn - [%%mel.raw {| + [%%mel.raw + {| function makeNewMock(self) { return new (Function.prototype.bind.apply(self, arguments)); } |}] external new0 : (unit -> 'ret, unit, 'ret) fn -> 'ret = "makeNewMock" + let new0 = new0 + external new1 : ('a -> 'ret, 'a, 'ret) fn -> 'a -> 'ret = "makeNewMock" + let new1 a self = new1 self a - external new2 : (('a -> 'b -> 'ret) [@u], ('a * 'b), 'ret) fn -> 'a -> 'b -> 'ret = "makeNewMock" + + external new2 : (('a -> 'b -> 'ret[@u]), 'a * 'b, 'ret) fn -> 'a -> 'b -> 'ret + = "makeNewMock" + let new2 a b self = new2 self a b external fn : ('fn, _, _) fn -> 'fn = "%identity" - external calls : (_, 'args, _) fn -> 'args array = "calls" [@@mel.get] [@@mel.scope "mock"] - let calls self = Js.Array.copy (calls self) (* Awesome, the bloody things are mutated so we need to copy *) - let calls self = calls self |> Array.map [%mel.raw {| + + external calls : (_, 'args, _) fn -> 'args array = "calls" + [@@mel.get] [@@mel.scope "mock"] + + let calls self = Js.Array.copy (calls self) + (* Awesome, the bloody things are mutated so we need to copy *) + + let calls self = + calls self + |> Array.map + [%mel.raw + {| function (args) { return args.length === 1 ? args[0] : args } - |}] (* there's no such thing as aa 1-ary tuple, so we need to unbox single-element arrays *) - external instances : (_, _, 'ret) fn -> 'ret array = "instances" [@@mel.get] [@@mel.scope "mock"] (* TODO: semms this only records "instances" created by `new` *) - let instances self = Js.Array.copy (instances self) (* Awesome, the bloody things are mutated so we need to copy *) - - (** Beware: this actually replaces `mock`, not just `mock.instances` and `mock.calls` *) - external mockClear : unit = "mockClear" [@@mel.send.pipe: _ fn] - external mockReset : unit = "mockReset" [@@mel.send.pipe: _ fn] - external mockImplementation : 'fn -> 'self = "mockImplementation" [@@mel.send.pipe: ('fn, _, _) fn as 'self] - external mockImplementationOnce : 'fn -> 'self = "mockImplementationOnce" [@@mel.send.pipe: ('fn, _, _) fn as 'self] - external mockReturnThis : unit = "mockReturnThis" [@@mel.send.pipe: (_, _, 'ret) fn] (* not type safe, we don't know what `this` actually is *) - external mockReturnValue : 'ret -> 'self = "mockReturnValue" [@@mel.send.pipe: (_, _, 'ret) fn as 'self] - external mockReturnValueOnce : 'ret -> 'self = "mockReturnValueOnce" [@@mel.send.pipe: (_, _, 'ret) fn as 'self] + |}] + (* there's no such thing as aa 1-ary tuple, so we need to unbox single-element arrays *) + + external instances : (_, _, 'ret) fn -> 'ret array = "instances" + [@@mel.get] [@@mel.scope "mock"] + (* TODO: semms this only records "instances" created by `new` *) + + let instances self = Js.Array.copy (instances self) + (* Awesome, the bloody things are mutated so we need to copy *) + + external mockClear : (_ fn[@mel.this]) -> unit = "mockClear" + [@@mel.send] + (** Beware: this actually replaces `mock`, not just `mock.instances` and + `mock.calls` *) + + external mockReset : (_ fn[@mel.this]) -> unit = "mockReset" [@@mel.send] + + external mockImplementation : + 'fn -> ((('fn, _, _) fn as 'self)[@mel.this]) -> 'self + = "mockImplementation" + [@@mel.send] + + external mockImplementationOnce : + 'fn -> ((('fn, _, _) fn as 'self)[@mel.this]) -> 'self + = "mockImplementationOnce" + [@@mel.send] + + external mockReturnThis : ((_, _, 'ret) fn[@mel.this]) -> unit + = "mockReturnThis" + [@@mel.send] + (* not type safe, we don't know what `this` actually is *) + + external mockReturnValue : + 'ret -> (((_, _, 'ret) fn as 'self)[@mel.this]) -> 'self = "mockReturnValue" + [@@mel.send] + + external mockReturnValueOnce : + 'ret -> (((_, _, 'ret) fn as 'self)[@mel.this]) -> 'self + = "mockReturnValueOnce" + [@@mel.send] end module Jest = struct @@ -443,10 +554,12 @@ module Jest = struct external runAllImmediates : unit -> unit = "jest.runAllImmediates" external advanceTimersByTime : int -> unit = "jest.advanceTimersByTime" external runOnlyPendingTimers : unit -> unit = "jest.runOnlyPendingTimers" - type fakeTimersConfig = { - legacyFakeTimers: bool - } - external useFakeTimers : ?config:fakeTimersConfig -> unit -> unit = "jest.useFakeTimers" + + type fakeTimersConfig = { legacyFakeTimers : bool } + + external useFakeTimers : ?config:fakeTimersConfig -> unit -> unit + = "jest.useFakeTimers" + external useRealTimers : unit -> unit = "jest.useRealTimers" end @@ -455,21 +568,34 @@ module JestJs = struct external disableAutomock : unit -> unit = "jest.disableAutomock" external enableAutomock : unit -> unit = "jest.enableAutomock" + (* genMockFromModule *) external resetModules : unit -> unit = "jest.resetModules" - external inferred_fn : unit -> ('a -> 'b Js.undefined [@u], 'a, 'b Js.undefined) MockJs.fn = "jest.fn" (* not sure how useful this really is *) + + external inferred_fn : + unit -> (('a -> 'b Js.undefined[@u]), 'a, 'b Js.undefined) MockJs.fn + = "jest.fn" (* not sure how useful this really is *) + external fn : ('a -> 'b) -> ('a -> 'b, 'a, 'b) MockJs.fn = "jest.fn" - external fn2 : ('a -> 'b -> 'c [@u]) -> (('a -> 'b -> 'c [@u]), 'a * 'b, 'c) MockJs.fn = "jest.fn" + + external fn2 : + (('a -> 'b -> 'c)[@u]) -> (('a -> 'b -> 'c[@u]), 'a * 'b, 'c) MockJs.fn + = "jest.fn" + (* TODO external fn3 : ('a -> 'b -> 'c -> 'd) -> ('a * 'b * 'c) MockJs.fn = "jest.fn" external fn4 : ('a -> 'b -> 'c -> 'd -> 'e) -> ('a * 'b * 'c * 'd) MockJs.fn = "jest.fn" external fn5 : ('a -> 'b -> 'c -> 'd -> 'e -> 'f) -> ('a * 'a * 'c * 'd * 'e) MockJs.fn = "jest.fn" external fn6 : ('a -> 'b -> 'c -> 'd -> 'e -> 'f -> 'g) -> ('a * 'b * 'c * 'd * 'e * 'f) MockJs.fn = "jest.fn" *) - (* external isMockFunction : MockJs.fn -> Js.boolean = "jest.isMockFunction" *) (* pointless with types? *) + (* external isMockFunction : MockJs.fn -> Js.boolean = "jest.isMockFunction" *) + (* pointless with types? *) external mock : string -> unit = "jest.mock" - external mockWithFactory : string -> (unit -> 'a) ->unit = "jest.mock" - external mockVirtual : string -> (unit -> 'a) -> < .. > Js.t -> unit = "jest.mock" + external mockWithFactory : string -> (unit -> 'a) -> unit = "jest.mock" + + external mockVirtual : string -> (unit -> 'a) -> < .. > Js.t -> unit + = "jest.mock" + (* TODO If this is merely defined, babel-plugin-jest-hoist fails with "The second argument of `jest.mock` must be a function." Silly thing. let mockVirtual : string -> (unit -> 'a) -> unit = fun moduleName factory -> mockVirtual moduleName factory [%mel.obj { _virtual = Js.true_ }] @@ -478,5 +604,8 @@ module JestJs = struct external resetAllMocks : unit -> unit = "jest.resetAllMocks" external setMock : string -> < .. > Js.t -> unit = "jest.setMock" external unmock : string -> unit = "jest.unmock" - external spyOn : (< .. > Js.t as 'this) -> string -> (unit, unit, 'this) MockJs.fn = "jest.spyOn" (* this is a bit too dynamic *) + + external spyOn : + (< .. > Js.t as 'this) -> string -> (unit, unit, 'this) MockJs.fn + = "jest.spyOn" (* this is a bit too dynamic *) end diff --git a/jest/jest.mli b/jest/jest.mli index c02ee93..aa48402 100644 --- a/jest/jest.mli +++ b/jest/jest.mli @@ -2,34 +2,39 @@ type assertion module type Asserter = sig type 'a t + val affirm : 'a t -> unit end module Runner (A : Asserter) : sig val test : string -> (unit -> _ A.t) -> unit val testAsync : string -> ?timeout:int -> ((_ A.t -> unit) -> unit) -> unit - val testPromise : string -> ?timeout:int -> (unit -> _ A.t Js.Promise.t) -> unit - val testAll : string -> 'a list -> ('a -> _ A.t) -> unit - val describe : string -> (unit -> unit) -> unit + val testPromise : + string -> ?timeout:int -> (unit -> _ A.t Js.Promise.t) -> unit - external beforeAll : (unit -> unit [@mel.uncurry]) -> unit = "beforeAll" + val testAll : string -> 'a list -> ('a -> _ A.t) -> unit + val describe : string -> (unit -> unit) -> unit + external beforeAll : ((unit -> unit)[@mel.uncurry]) -> unit = "beforeAll" val beforeAllAsync : ?timeout:int -> ((unit -> unit) -> unit) -> unit val beforeAllPromise : ?timeout:int -> (unit -> 'a Js.Promise.t) -> unit - external beforeEach : (unit -> unit [@mel.uncurry]) -> unit = "beforeEach" + external beforeEach : ((unit -> unit)[@mel.uncurry]) -> unit = "beforeEach" val beforeEachAsync : ?timeout:int -> ((unit -> unit) -> unit) -> unit val beforeEachPromise : ?timeout:int -> (unit -> 'a Js.Promise.t) -> unit - external afterAll : (unit -> unit [@mel.uncurry]) -> unit = "afterAll" + external afterAll : ((unit -> unit)[@mel.uncurry]) -> unit = "afterAll" val afterAllAsync : ?timeout:int -> ((unit -> unit) -> unit) -> unit val afterAllPromise : ?timeout:int -> (unit -> 'a Js.Promise.t) -> unit - external afterEach : (unit -> unit [@mel.uncurry]) -> unit = "afterEach" + external afterEach : ((unit -> unit)[@mel.uncurry]) -> unit = "afterEach" val afterEachAsync : ?timeout:int -> ((unit -> unit) -> unit) -> unit val afterEachPromise : ?timeout:int -> (unit -> 'a Js.Promise.t) -> unit module Only : sig val test : string -> (unit -> _ A.t) -> unit val testAsync : string -> ?timeout:int -> ((_ A.t -> unit) -> unit) -> unit - val testPromise : string -> ?timeout:int -> (unit -> _ A.t Js.Promise.t) -> unit + + val testPromise : + string -> ?timeout:int -> (unit -> _ A.t Js.Promise.t) -> unit + val testAll : string -> 'a list -> ('a -> _ A.t) -> unit val describe : string -> (unit -> unit) -> unit end @@ -37,7 +42,10 @@ module Runner (A : Asserter) : sig module Skip : sig val test : string -> (unit -> _ A.t) -> unit val testAsync : string -> ?timeout:int -> ((_ A.t -> unit) -> unit) -> unit - val testPromise : string -> ?timeout:int -> (unit -> _ A.t Js.Promise.t) -> unit + + val testPromise : + string -> ?timeout:int -> (unit -> _ A.t Js.Promise.t) -> unit + val testAll : string -> 'a list -> ('a -> _ A.t) -> unit val describe : string -> (unit -> unit) -> unit end @@ -45,36 +53,47 @@ end val test : string -> (unit -> assertion) -> unit val testAsync : string -> ?timeout:int -> ((assertion -> unit) -> unit) -> unit -val testPromise : string -> ?timeout:int -> (unit -> assertion Js.Promise.t) -> unit -val testAll : string -> 'a list -> ('a -> assertion) -> unit -val describe : string -> (unit -> unit) -> unit +val testPromise : + string -> ?timeout:int -> (unit -> assertion Js.Promise.t) -> unit -external beforeAll : (unit -> unit [@mel.uncurry]) -> unit = "beforeAll" +val testAll : string -> 'a list -> ('a -> assertion) -> unit +val describe : string -> (unit -> unit) -> unit +external beforeAll : ((unit -> unit)[@mel.uncurry]) -> unit = "beforeAll" val beforeAllAsync : ?timeout:int -> ((unit -> unit) -> unit) -> unit val beforeAllPromise : ?timeout:int -> (unit -> 'a Js.Promise.t) -> unit -external beforeEach : (unit -> unit [@mel.uncurry]) -> unit = "beforeEach" +external beforeEach : ((unit -> unit)[@mel.uncurry]) -> unit = "beforeEach" val beforeEachAsync : ?timeout:int -> ((unit -> unit) -> unit) -> unit val beforeEachPromise : ?timeout:int -> (unit -> 'a Js.Promise.t) -> unit -external afterAll : (unit -> unit [@mel.uncurry]) -> unit = "afterAll" +external afterAll : ((unit -> unit)[@mel.uncurry]) -> unit = "afterAll" val afterAllAsync : ?timeout:int -> ((unit -> unit) -> unit) -> unit val afterAllPromise : ?timeout:int -> (unit -> 'a Js.Promise.t) -> unit -external afterEach : (unit -> unit [@mel.uncurry]) -> unit = "afterEach" +external afterEach : ((unit -> unit)[@mel.uncurry]) -> unit = "afterEach" val afterEachAsync : ?timeout:int -> ((unit -> unit) -> unit) -> unit val afterEachPromise : ?timeout:int -> (unit -> 'a Js.Promise.t) -> unit module Only : sig val test : string -> (unit -> assertion) -> unit - val testAsync : string -> ?timeout:int -> ((assertion -> unit) -> unit) -> unit - val testPromise : string -> ?timeout:int -> (unit -> assertion Js.Promise.t) -> unit + + val testAsync : + string -> ?timeout:int -> ((assertion -> unit) -> unit) -> unit + + val testPromise : + string -> ?timeout:int -> (unit -> assertion Js.Promise.t) -> unit + val testAll : string -> 'a list -> ('a -> assertion) -> unit val describe : string -> (unit -> unit) -> unit end module Skip : sig val test : string -> (unit -> assertion) -> unit - val testAsync : string -> ?timeout:int -> ((assertion -> unit) -> unit) -> unit - val testPromise : string -> ?timeout:int -> (unit -> assertion Js.Promise.t) -> unit + + val testAsync : + string -> ?timeout:int -> ((assertion -> unit) -> unit) -> unit + + val testPromise : + string -> ?timeout:int -> (unit -> assertion Js.Promise.t) -> unit + val testAll : string -> 'a list -> ('a -> assertion) -> unit val describe : string -> (unit -> unit) -> unit end @@ -87,64 +106,65 @@ val pass : assertion val fail : string -> assertion module Expect : sig - type 'a plainPartial = [`Just of 'a] - type 'a invertedPartial = [`Not of 'a] - type 'a partial = [ - | 'a plainPartial - | 'a invertedPartial - ] + type 'a plainPartial = [ `Just of 'a ] + type 'a invertedPartial = [ `Not of 'a ] + type 'a partial = [ 'a plainPartial | 'a invertedPartial ] val expect : 'a -> 'a plainPartial - val expectFn : ('a -> 'b) -> 'a -> (unit -> 'b) plainPartial (* EXPERIMENTAL *) - - val toBe : 'a -> [< 'a partial] -> assertion - val toBeCloseTo : float -> [< float partial] -> assertion - val toBeSoCloseTo : float -> digits:int -> [< float partial] -> assertion - val toBeGreaterThan : 'a -> [< 'a partial] -> assertion - val toBeGreaterThanOrEqual : 'a -> [< 'a partial] -> assertion - val toBeLessThan : 'a -> [< 'a partial] -> assertion - val toBeLessThanOrEqual : 'a -> [< 'a partial] -> assertion - val toBeSupersetOf : 'a array -> [< 'a array partial] -> assertion - val toContain : 'a -> [< 'a array partial] -> assertion - val toContainEqual : 'a -> [< 'a array partial] -> assertion - val toContainString : string -> [< string partial] -> assertion - val toEqual : 'a -> [< 'a partial] -> assertion - val toHaveLength : int -> [< 'a array partial] -> assertion - val toMatch : string -> [< string partial] -> assertion + + val expectFn : + ('a -> 'b) -> 'a -> (unit -> 'b) plainPartial (* EXPERIMENTAL *) + + val toBe : 'a -> [< 'a partial ] -> assertion + val toBeCloseTo : float -> [< float partial ] -> assertion + val toBeSoCloseTo : float -> digits:int -> [< float partial ] -> assertion + val toBeGreaterThan : 'a -> [< 'a partial ] -> assertion + val toBeGreaterThanOrEqual : 'a -> [< 'a partial ] -> assertion + val toBeLessThan : 'a -> [< 'a partial ] -> assertion + val toBeLessThanOrEqual : 'a -> [< 'a partial ] -> assertion + val toBeSupersetOf : 'a array -> [< 'a array partial ] -> assertion + val toContain : 'a -> [< 'a array partial ] -> assertion + val toContainEqual : 'a -> [< 'a array partial ] -> assertion + val toContainString : string -> [< string partial ] -> assertion + val toEqual : 'a -> [< 'a partial ] -> assertion + val toHaveLength : int -> [< 'a array partial ] -> assertion + val toMatch : string -> [< string partial ] -> assertion val toMatchInlineSnapshot : string -> _ plainPartial -> assertion - val toMatchRe : Js.Re.t -> [< string partial] -> assertion + val toMatchRe : Js.Re.t -> [< string partial ] -> assertion val toMatchSnapshot : _ plainPartial -> assertion val toMatchSnapshotWithName : string -> _ plainPartial -> assertion - val toThrow : [< (unit -> _) partial] -> assertion + val toThrow : [< (unit -> _) partial ] -> assertion val toThrowErrorMatchingSnapshot : (unit -> _) plainPartial -> assertion - val not_ : 'a plainPartial -> 'a invertedPartial val not__ : 'a plainPartial -> 'a invertedPartial module Operators : sig (** experimental *) - val (==) : [< 'a partial] -> 'a -> assertion - val (>) : [< 'a partial] -> 'a -> assertion - val (>=) : [< 'a partial] -> 'a -> assertion - val (<) : [< 'a partial] -> 'a -> assertion - val (<=) : [< 'a partial] -> 'a -> assertion - val (=) : [< 'a partial] -> 'a -> assertion - val (<>) : 'a plainPartial -> 'a -> assertion - val (!=) : 'a plainPartial -> 'a -> assertion + val ( == ) : [< 'a partial ] -> 'a -> assertion + val ( > ) : [< 'a partial ] -> 'a -> assertion + val ( >= ) : [< 'a partial ] -> 'a -> assertion + val ( < ) : [< 'a partial ] -> 'a -> assertion + val ( <= ) : [< 'a partial ] -> 'a -> assertion + val ( = ) : [< 'a partial ] -> 'a -> assertion + val ( <> ) : 'a plainPartial -> 'a -> assertion + val ( != ) : 'a plainPartial -> 'a -> assertion end end module ExpectJs : sig include module type of Expect - val toBeDefined : [< _ Js.undefined partial] -> assertion - val toBeFalsy : [< _ partial] -> assertion - val toBeNull : [< _ Js.null partial] -> assertion - val toBeTruthy : [< _ partial] -> assertion - val toBeUndefined : [< _ Js.undefined partial] -> assertion - val toContainProperties : string array -> [< < .. > Js.t partial] -> assertion - val toMatchObject : < .. > Js.t -> [< < .. > Js.t partial] -> assertion + val toBeDefined : [< _ Js.undefined partial ] -> assertion + val toBeFalsy : [< _ partial ] -> assertion + val toBeNull : [< _ Js.null partial ] -> assertion + val toBeTruthy : [< _ partial ] -> assertion + val toBeUndefined : [< _ Js.undefined partial ] -> assertion + + val toContainProperties : + string array -> [< < .. > Js.t partial ] -> assertion + + val toMatchObject : < .. > Js.t -> [< < .. > Js.t partial ] -> assertion end module MockJs : sig @@ -154,20 +174,41 @@ module MockJs : sig val new0 : (unit -> 'ret, unit, 'ret) fn -> 'ret val new1 : 'a -> ('a -> 'ret, 'a, 'ret) fn -> 'ret - val new2 : 'a -> 'b -> (('a -> 'b -> 'ret) [@u], 'a * 'b, 'ret) fn -> 'ret - + val new2 : 'a -> 'b -> (('a -> 'b -> 'ret[@u]), 'a * 'b, 'ret) fn -> 'ret external fn : ('fn, _, _) fn -> 'fn = "%identity" val calls : (_, 'args, _) fn -> 'args array val instances : (_, _, 'ret) fn -> 'ret array - (** Beware: this actually replaces `mock`, not just `mock.instances` and `mock.calls` *) - external mockClear : unit = "mockClear" [@@mel.send.pipe: _ fn] - external mockReset : unit = "mockReset" [@@mel.send.pipe: _ fn] - external mockImplementation : 'fn -> 'self = "mockImplementation" [@@mel.send.pipe: ('fn, _, _) fn as 'self] - external mockImplementationOnce : 'fn -> 'self = "mockImplementationOnce" [@@mel.send.pipe: ('fn, _, _) fn as 'self] - external mockReturnThis : unit = "mockReturnThis" [@@mel.send.pipe: (_, _, 'ret) fn] (* not type safe, we don't know what `this` actually is *) - external mockReturnValue : 'ret -> 'self = "mockReturnValue" [@@mel.send.pipe: (_, _, 'ret) fn as 'self] - external mockReturnValueOnce : 'ret -> 'self = "mockReturnValueOnce" [@@mel.send.pipe: (_, _, 'ret) fn as 'self] + external mockClear : (_ fn[@mel.this]) -> unit = "mockClear" + [@@mel.send] + (** Beware: this actually replaces `mock`, not just `mock.instances` and + `mock.calls` *) + + external mockReset : (_ fn[@mel.this]) -> unit = "mockReset" [@@mel.send] + + external mockImplementation : + 'fn -> ((('fn, _, _) fn as 'self)[@mel.this]) -> 'self + = "mockImplementation" + [@@mel.send] + + external mockImplementationOnce : + 'fn -> ((('fn, _, _) fn as 'self)[@mel.this]) -> 'self + = "mockImplementationOnce" + [@@mel.send] + + external mockReturnThis : ((_, _, 'ret) fn[@mel.this]) -> unit + = "mockReturnThis" + [@@mel.send] + (* not type safe, we don't know what `this` actually is *) + + external mockReturnValue : + 'ret -> (((_, _, 'ret) fn as 'self)[@mel.this]) -> 'self = "mockReturnValue" + [@@mel.send] + + external mockReturnValueOnce : + 'ret -> (((_, _, 'ret) fn as 'self)[@mel.this]) -> 'self + = "mockReturnValueOnce" + [@@mel.send] end module Jest : sig @@ -177,10 +218,12 @@ module Jest : sig external runAllImmediates : unit -> unit = "jest.runAllImmediates" external advanceTimersByTime : int -> unit = "jest.advanceTimersByTime" external runOnlyPendingTimers : unit -> unit = "jest.runOnlyPendingTimers" - type fakeTimersConfig = { - legacyFakeTimers: bool - } - external useFakeTimers : ?config:fakeTimersConfig -> unit -> unit = "jest.useFakeTimers" + + type fakeTimersConfig = { legacyFakeTimers : bool } + + external useFakeTimers : ?config:fakeTimersConfig -> unit -> unit + = "jest.useFakeTimers" + external useRealTimers : unit -> unit = "jest.useRealTimers" end @@ -190,15 +233,29 @@ module JestJs : sig external disableAutomock : unit -> unit = "jest.disableAutomock" external enableAutomock : unit -> unit = "jest.enableAutomock" external resetModules : unit -> unit = "jest.resetModules" - external inferred_fn : unit -> ('a -> 'b Js.undefined [@u], 'a, 'b Js.undefined) MockJs.fn = "jest.fn" + + external inferred_fn : + unit -> (('a -> 'b Js.undefined[@u]), 'a, 'b Js.undefined) MockJs.fn + = "jest.fn" + external fn : ('a -> 'b) -> ('a -> 'b, 'a, 'b) MockJs.fn = "jest.fn" - external fn2 : ('a -> 'b -> 'c [@u]) -> (('a -> 'b -> 'c [@u]), 'a * 'b, 'c) MockJs.fn = "jest.fn" + + external fn2 : + (('a -> 'b -> 'c)[@u]) -> (('a -> 'b -> 'c[@u]), 'a * 'b, 'c) MockJs.fn + = "jest.fn" + external mock : string -> unit = "jest.mock" - external mockWithFactory : string -> (unit -> 'a) ->unit = "jest.mock" - external mockVirtual : string -> (unit -> 'a) -> < .. > Js.t -> unit = "jest.mock" + external mockWithFactory : string -> (unit -> 'a) -> unit = "jest.mock" + + external mockVirtual : string -> (unit -> 'a) -> < .. > Js.t -> unit + = "jest.mock" + external clearAllMocks : unit -> unit = "jest.clearAllMocks" external resetAllMocks : unit -> unit = "jest.resetAllMocks" external setMock : string -> < .. > Js.t -> unit = "jest.setMock" external unmock : string -> unit = "jest.unmock" - external spyOn : (< .. > Js.t as 'this) -> string -> (unit, unit, 'this) MockJs.fn = "jest.spyOn" + + external spyOn : + (< .. > Js.t as 'this) -> string -> (unit, unit, 'this) MockJs.fn + = "jest.spyOn" end diff --git a/melange-jest.opam b/melange-jest.opam index 95073f3..0082911 100644 --- a/melange-jest.opam +++ b/melange-jest.opam @@ -12,7 +12,7 @@ homepage: "https://github.com/melange-community/melange-jest" bug-reports: "https://github.com/melange-community/melange-jest/issues" depends: [ "dune" {>= "3.9"} - "melange" {>= "2.0.0"} + "melange" {>= "5.0.0"} "reason" {with-test} "melange-webapi" {with-test} "odoc" {with-doc}