diff --git a/README.md b/README.md index d937038..499410d 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ If you have any async tests, you will need to change the `testAsync`, `beforeAll Some new features have been added that are not present in `rescript-jest`. - `affirm` function to allow multiple assertions in a single test +- `describePromise` function to allow async setup in describe blocks ### `affirm` example @@ -46,6 +47,47 @@ test("multiple assertions", () => { ``` +### `describePromise` example + +`describePromise` is supported by vitest, so we add it here. + +`describePromise` enables asynchronous setup directly inside `describe` blocks. +This is useful when you need to prepare something before tests run—for example, +fetching data from a server or reading files. + +Functionally, it works the same as using a `beforeAllPromise` block, but putting +the setup inside the `describe` is often more convenient since you don’t need to +use mutable variables (refs) to share data between the setup and the tests. + +**Note:** `describe` blocks always execute before any setup handlers. + +```rescript +describePromise("async setup", async () => { + let data = await fetchData() + "some test"->test(() => expect(data)->toEqual(expectedData)) +}) +``` + +Similarly to `testPromise`, `describePromise` can take an optional `timeout` argument. + +```rescript +describePromise("async setup with timeout", ~timeout=5000, async () => { + let data = await fetchData() + "some test"->test(() => expect(data)->toEqual(expectedData)) +}) +``` + +## Installation + +This package is tested with Rescript 11 and 12. + +```rescript +describePromise("async setup with timeout", ~timeout=5000, async () => { + let data = await fetchData() + expect(data)->toEqual(expectedData) +}) +``` + ## Installation This package is tested with Rescript 11 and 12. diff --git a/__tests__/globals_only_vitest.res b/__tests__/globals_only_vitest.res index 19011dd..9ec84d1 100644 --- a/__tests__/globals_only_vitest.res +++ b/__tests__/globals_only_vitest.res @@ -47,4 +47,6 @@ let () = { ) Only.describe("Only.describe", () => test("some aspect", () => pass)) + + Only.describePromise("Only.describePromise", async () => test("some aspect", () => pass)) } diff --git a/__tests__/globals_vitest.res b/__tests__/globals_vitest.res index 2c9a54a..0fff1d4 100644 --- a/__tests__/globals_vitest.res +++ b/__tests__/globals_vitest.res @@ -61,6 +61,8 @@ let () = { describe("describe", () => test("some aspect", () => pass)) + describePromise("describePromise", async () => test("some aspect", () => pass)) + describe("beforeAll", () => { let x = ref(0) diff --git a/__tests__/runner_only_vitest.res b/__tests__/runner_only_vitest.res index 2bc1f4a..459bc6b 100644 --- a/__tests__/runner_only_vitest.res +++ b/__tests__/runner_only_vitest.res @@ -27,4 +27,6 @@ let () = { ) Only.describe("Only.describe", () => test("some aspect", () => 1 + 2 === 3)) + + Only.describePromise("Only.describePromise", async () => test("some aspect", () => 1 + 2 === 3)) } diff --git a/src/Vitest.res b/src/Vitest.res index bc3f1cc..ae80502 100644 --- a/src/Vitest.res +++ b/src/Vitest.res @@ -183,6 +183,18 @@ module Runner = (A: Asserter) => { Js.undefined }) + @module("vitest") + external describePromise: ( + string, + ~timeout: int=?, + @uncurry unit => promise>, + ) => unit = "describe" + let describePromise = (label, ~timeout=?, f) => + describePromise(label, ~timeout?, async () => { + await f() + Js.undefined + }) + @module("vitest") external beforeAll: (unit => unit) => unit = "beforeAll" @module("vitest") external beforeAllPromise: (@uncurry unit => promise<'a>, Js.Undefined.t) => unit = @@ -257,6 +269,18 @@ module Runner = (A: Asserter) => { f() Js.undefined }) + + @module("vitest") @scope("describe") + external describePromise: ( + string, + ~timeout: int=?, + @uncurry unit => promise>, + ) => unit = "only" + let describePromise = (label, ~timeout=?, f) => + describePromise(label, ~timeout?, async () => { + await f() + Js.undefined + }) } module Skip = { @@ -282,6 +306,18 @@ module Runner = (A: Asserter) => { f() Js.undefined }) + + @module("vitest") @scope("describe") + external describePromise: ( + string, + ~timeout: int=?, + @uncurry unit => promise>, + ) => unit = "skip" + let describePromise = (label, ~timeout=?, f) => + describePromise(label, ~timeout?, async () => { + await f() + Js.undefined + }) } module Todo = { diff --git a/src/Vitest.resi b/src/Vitest.resi index 1e262cc..a7c2662 100644 --- a/src/Vitest.resi +++ b/src/Vitest.resi @@ -14,6 +14,7 @@ module Runner: (A: Asserter) => let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise>) => unit let describe: (string, unit => unit) => unit + let describePromise: (string, ~timeout: int=?, unit => promise) => unit @module("vitest") external beforeAll: (unit => unit) => unit = "beforeAll" // let beforeAllAsync: (~timeout: int=?, (unit => unit) => unit) => unit @@ -35,6 +36,7 @@ module Runner: (A: Asserter) => let testAll: (string, list<'a>, 'a => A.t<_>) => unit let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise>) => unit let describe: (string, unit => unit) => unit + let describePromise: (string, ~timeout: int=?, unit => promise) => unit } module Skip: { @@ -44,6 +46,7 @@ module Runner: (A: Asserter) => let testAll: (string, list<'a>, 'a => A.t<_>) => unit let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise>) => unit let describe: (string, unit => unit) => unit + let describePromise: (string, ~timeout: int=?, unit => promise) => unit } } @@ -56,6 +59,7 @@ let testAll: (string, list<'a>, 'a => assertion) => unit let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise) => unit let describe: (string, unit => unit) => unit +let describePromise: (string, ~timeout: int=?, unit => promise) => unit @module("vitest") external beforeAll: (unit => unit) => unit = "beforeAll" let beforeAllPromise: (~timeout: int=?, unit => promise<'a>) => unit @@ -72,6 +76,7 @@ module Only: { let testAll: (string, list<'a>, 'a => assertion) => unit let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise) => unit let describe: (string, unit => unit) => unit + let describePromise: (string, ~timeout: int=?, unit => promise) => unit } module Skip: { @@ -80,6 +85,7 @@ module Skip: { let testAll: (string, list<'a>, 'a => assertion) => unit let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise) => unit let describe: (string, unit => unit) => unit + let describePromise: (string, ~timeout: int=?, unit => promise) => unit } module Todo: {