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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions __tests__/globals_only_vitest.res
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ let () = {
)

Only.describe("Only.describe", () => test("some aspect", () => pass))

Only.describePromise("Only.describePromise", async () => test("some aspect", () => pass))
}
2 changes: 2 additions & 0 deletions __tests__/globals_vitest.res
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ let () = {

describe("describe", () => test("some aspect", () => pass))

describePromise("describePromise", async () => test("some aspect", () => pass))

describe("beforeAll", () => {
let x = ref(0)

Expand Down
2 changes: 2 additions & 0 deletions __tests__/runner_only_vitest.res
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
36 changes: 36 additions & 0 deletions src/Vitest.res
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,18 @@ module Runner = (A: Asserter) => {
Js.undefined
})

@module("vitest")
external describePromise: (
string,
~timeout: int=?,
@uncurry unit => promise<Js.undefined<unit>>,
) => 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<int>) => unit =
Expand Down Expand Up @@ -257,6 +269,18 @@ module Runner = (A: Asserter) => {
f()
Js.undefined
})

@module("vitest") @scope("describe")
external describePromise: (
string,
~timeout: int=?,
@uncurry unit => promise<Js.undefined<unit>>,
) => unit = "only"
let describePromise = (label, ~timeout=?, f) =>
describePromise(label, ~timeout?, async () => {
await f()
Js.undefined
})
}

module Skip = {
Expand All @@ -282,6 +306,18 @@ module Runner = (A: Asserter) => {
f()
Js.undefined
})

@module("vitest") @scope("describe")
external describePromise: (
string,
~timeout: int=?,
@uncurry unit => promise<Js.undefined<unit>>,
) => unit = "skip"
let describePromise = (label, ~timeout=?, f) =>
describePromise(label, ~timeout?, async () => {
await f()
Js.undefined
})
}

module Todo = {
Expand Down
6 changes: 6 additions & 0 deletions src/Vitest.resi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Runner: (A: Asserter) =>
let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise<A.t<_>>) => unit

let describe: (string, unit => unit) => unit
let describePromise: (string, ~timeout: int=?, unit => promise<unit>) => unit

@module("vitest") external beforeAll: (unit => unit) => unit = "beforeAll"
// let beforeAllAsync: (~timeout: int=?, (unit => unit) => unit) => unit
Expand All @@ -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<A.t<_>>) => unit
let describe: (string, unit => unit) => unit
let describePromise: (string, ~timeout: int=?, unit => promise<unit>) => unit
}

module Skip: {
Expand All @@ -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<A.t<_>>) => unit
let describe: (string, unit => unit) => unit
let describePromise: (string, ~timeout: int=?, unit => promise<unit>) => unit
}
}

Expand All @@ -56,6 +59,7 @@ let testAll: (string, list<'a>, 'a => assertion) => unit
let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise<assertion>) => unit

let describe: (string, unit => unit) => unit
let describePromise: (string, ~timeout: int=?, unit => promise<unit>) => unit

@module("vitest") external beforeAll: (unit => unit) => unit = "beforeAll"
let beforeAllPromise: (~timeout: int=?, unit => promise<'a>) => unit
Expand All @@ -72,6 +76,7 @@ module Only: {
let testAll: (string, list<'a>, 'a => assertion) => unit
let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise<assertion>) => unit
let describe: (string, unit => unit) => unit
let describePromise: (string, ~timeout: int=?, unit => promise<unit>) => unit
}

module Skip: {
Expand All @@ -80,6 +85,7 @@ module Skip: {
let testAll: (string, list<'a>, 'a => assertion) => unit
let testAllPromise: (string, list<'a>, ~timeout: int=?, 'a => promise<assertion>) => unit
let describe: (string, unit => unit) => unit
let describePromise: (string, ~timeout: int=?, unit => promise<unit>) => unit
}

module Todo: {
Expand Down