| 1 | /** |
| 2 | * @title Writing Effect tests with @effect/vitest |
| 3 | * |
| 4 | * Using `it.effect` for Effect-based tests. |
| 5 | */ |
| 6 | import { assert, describe, it } from "@effect/vitest" |
| 7 | import { Effect, Fiber, Schema } from "effect" |
| 8 | import { TestClock } from "effect/testing" |
| 9 | |
| 10 | describe("@effect/vitest basics", () => { |
| 11 | it.effect("runs Effect code with assert helpers", () => |
| 12 | Effect.gen(function*() { |
| 13 | const upper = ["ada", "lin"].map((name) => name.toUpperCase()) |
| 14 | assert.deepStrictEqual(upper, ["ADA", "LIN"]) |
| 15 | assert.strictEqual(upper.length, 2) |
| 16 | assert.isTrue(upper.includes("ADA")) |
| 17 | })) |
| 18 | |
| 19 | it.effect.each([ |
| 20 | { input: " Ada ", expected: "ada" }, |
| 21 | { input: " Lin ", expected: "lin" }, |
| 22 | { input: " Nia ", expected: "nia" } |
| 23 | ])("parameterized normalization %#", ({ input, expected }) => |
| 24 | Effect.gen(function*() { |
| 25 | assert.strictEqual(input.trim().toLowerCase(), expected) |
| 26 | })) |
| 27 | |
| 28 | it.effect("controls time with TestClock", () => |
| 29 | Effect.gen(function*() { |
| 30 | const fiber = yield* Effect.forkChild( |
| 31 | Effect.sleep(60_000).pipe(Effect.as("done" as const)) |
| 32 | ) |
| 33 | |
| 34 | // Move virtual time forward to complete sleeping fibers immediately. |
| 35 | yield* TestClock.adjust(60_000) |
| 36 | |
| 37 | const value = yield* Fiber.join(fiber) |
| 38 | assert.strictEqual(value, "done") |
| 39 | })) |
| 40 | |
| 41 | it.live("uses real runtime services", () => |
| 42 | Effect.gen(function*() { |
| 43 | const startedAt = Date.now() |
| 44 | yield* Effect.sleep(1) |
| 45 | assert.isTrue(Date.now() >= startedAt) |
| 46 | })) |
| 47 | |
| 48 | // For property-based testing, use `it.effect.prop` with Schema-based |
| 49 | // arbitraries |
| 50 | it.effect.prop("reversing twice is identity", [Schema.String], ([value]) => |
| 51 | Effect.gen(function*() { |
| 52 | const reversedTwice = value.split("").reverse().reverse().join("") |
| 53 | assert.strictEqual(reversedTwice, value) |
| 54 | })) |
| 55 | }) |