The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { afterAll, assert, beforeAll, beforeEach, describe, describeWrapped, expect, it, layer } from "@effect/vitest" |
| 2 | import * as testAssert from "@effect/vitest/utils" |
| 3 | import { Clock, Context, Duration, Effect, Fiber, Layer, Schema } from "effect" |
| 4 | import * as Arbitrary from "effect/Arbitrary" |
| 5 | import { TestClock } from "effect/testing" |
| 6 | |
| 7 | it.effect( |
| 8 | "effect", |
| 9 | () => Effect.acquireRelease(Effect.sync(() => expect(1).toEqual(1)), () => Effect.void) |
| 10 | ) |
| 11 | it.live( |
| 12 | "live", |
| 13 | () => Effect.acquireRelease(Effect.sync(() => expect(1).toEqual(1)), () => Effect.void) |
| 14 | ) |
| 15 | |
| 16 | it("throws fails when the thunk does not throw", () => { |
| 17 | expect(() => testAssert.throws(() => {})).toThrow() |
| 18 | }) |
| 19 | |
| 20 | it("throwsAsync fails when the promise resolves", async () => { |
| 21 | await expect(testAssert.throwsAsync(() => Promise.resolve())).rejects.toThrow() |
| 22 | }) |
| 23 | |
| 24 | it("deepStrictEqual fails with the assertion diff when no message is given", () => { |
| 25 | expect(() => testAssert.deepStrictEqual({ a: 1 }, { a: 2 })).toThrow("Expected values to be strictly deep-equal") |
| 26 | }) |
| 27 | |
| 28 | it("notDeepStrictEqual fails with the assertion message when no message is given", () => { |
| 29 | expect(() => testAssert.notDeepStrictEqual({ a: 1 }, { a: 1 })).toThrow("not to be strictly deep-equal") |
| 30 | }) |
| 31 | |
| 32 | // each |
| 33 | |
| 34 | it.effect.each([1, 2, 3])( |
| 35 | "effect each %s", |
| 36 | (n) => Effect.acquireRelease(Effect.sync(() => expect(n).toEqual(n)), () => Effect.void) |
| 37 | ) |
| 38 | it.live.each([1, 2, 3])( |
| 39 | "live each %s", |
| 40 | (n) => Effect.acquireRelease(Effect.sync(() => expect(n).toEqual(n)), () => Effect.void) |
| 41 | ) |
| 42 | |
| 43 | // skip |
| 44 | |
| 45 | it.live.skip( |
| 46 | "live skipped", |
| 47 | () => Effect.die("skipped anyway") |
| 48 | ) |
| 49 | it.effect.skip( |
| 50 | "effect skipped", |
| 51 | () => Effect.die("skipped anyway") |
| 52 | ) |
| 53 | |
| 54 | // skipIf |
| 55 | |
| 56 | it.effect.skipIf(true)("effect skipIf (true)", () => Effect.die("skipped anyway")) |
| 57 | it.effect.skipIf(false)("effect skipIf (false)", () => Effect.sync(() => expect(1).toEqual(1))) |
| 58 | |
| 59 | // runIf |
| 60 | |
| 61 | it.effect.runIf(true)("effect runIf (true)", () => Effect.sync(() => expect(1).toEqual(1))) |
| 62 | it.effect.runIf(false)("effect runIf (false)", () => Effect.die("not run anyway")) |
| 63 | |
| 64 | // chained helpers |
| 65 | |
| 66 | it.describe.each(["foo", "bar"] as const)("describe.each %s", (text) => { |
| 67 | it.effect("runs an Effect test", () => |
| 68 | Effect.sync(() => { |
| 69 | assert.include(["foo", "bar"], text) |
| 70 | })) |
| 71 | }) |
| 72 | |
| 73 | it.skip.each([1])("skip.each %s", () => assert.fail("skipped anyway")) |
| 74 | |
| 75 | // The following test is expected to fail because it simulates a test timeout. |
| 76 | // Be aware that eventual "failure" of the test is only logged out. |
| 77 | it.live.fails("interrupts on timeout", (ctx) => |
| 78 | Effect.gen(function*() { |
| 79 | let acquired = false |
| 80 | |
| 81 | ctx.onTestFailed(() => { |
| 82 | if (acquired) { |
| 83 | // oxlint-disable-next-line no-console |
| 84 | console.error("'effect is interrupted on timeout' @effect/vitest test failed") |
| 85 | } |
| 86 | }) |
| 87 | |
| 88 | yield* Effect.acquireRelease( |
| 89 | Effect.sync(() => acquired = true), |
| 90 | () => Effect.sync(() => acquired = false) |
| 91 | ) |
| 92 | yield* Effect.sleep(1000) |
| 93 | }), 1) |
| 94 | |
| 95 | class Foo extends Context.Service<Foo, "foo">()("Foo") { |
| 96 | static layer = Layer.succeed(Foo)("foo") |
| 97 | } |
| 98 | |
| 99 | class Bar extends Context.Service<Bar, "bar">()("Bar") { |
| 100 | static layer = Layer.effect(Bar)(Effect.map(Foo, () => "bar" as const)) |
| 101 | } |
| 102 | |
| 103 | class Sleeper extends Context.Service<Sleeper, { |
| 104 | readonly sleep: (ms: number) => Effect.Effect<void> |
| 105 | }>()("Sleeper") { |
| 106 | static readonly layer = Layer.effect(Sleeper)( |
| 107 | Effect.gen(function*() { |
| 108 | const clock = yield* Clock.Clock |
| 109 | |
| 110 | return { |
| 111 | sleep: (ms: number) => clock.sleep(Duration.millis(ms)) |
| 112 | } |
| 113 | }) |
| 114 | ) |
| 115 | } |
| 116 | |
| 117 | describe("layer", () => { |
| 118 | layer(Foo.layer)((it) => { |
| 119 | it.effect("adds context", () => |
| 120 | Effect.gen(function*() { |
| 121 | const foo = yield* Foo |
| 122 | expect(foo).toEqual("foo") |
| 123 | })) |
| 124 | |
| 125 | it.layer(Bar.layer)("nested", (it) => { |
| 126 | it.effect("adds context", () => |
| 127 | Effect.gen(function*() { |
| 128 | const foo = yield* Foo |
| 129 | const bar = yield* Bar |
| 130 | expect(foo).toEqual("foo") |
| 131 | expect(bar).toEqual("bar") |
| 132 | })) |
| 133 | }) |
| 134 | |
| 135 | it.layer(Bar.layer)((it) => { |
| 136 | it.effect("without name", () => |
| 137 | Effect.gen(function*() { |
| 138 | const foo = yield* Foo |
| 139 | const bar = yield* Bar |
| 140 | expect(foo).toEqual("foo") |
| 141 | expect(bar).toEqual("bar") |
| 142 | })) |
| 143 | }) |
| 144 | |
| 145 | describe("release", () => { |
| 146 | let released = false |
| 147 | afterAll(() => { |
| 148 | expect(released).toEqual(true) |
| 149 | }) |
| 150 | |
| 151 | class Scoped extends Context.Service<Scoped, "scoped">()("Scoped") { |
| 152 | static layer = Layer.effect(Scoped)( |
| 153 | Effect.acquireRelease( |
| 154 | Effect.succeed("scoped" as const), |
| 155 | () => Effect.sync(() => released = true) |
| 156 | ) |
| 157 | ) |
| 158 | } |
| 159 | |
| 160 | it.layer(Scoped.layer)((it) => { |
| 161 | it.effect("adds context", () => |
| 162 | Effect.gen(function*() { |
| 163 | const foo = yield* Foo |
| 164 | const scoped = yield* Scoped |
| 165 | expect(foo).toEqual("foo") |
| 166 | expect(scoped).toEqual("scoped") |
| 167 | })) |
| 168 | }) |
| 169 | |
| 170 | it.effect.prop( |
| 171 | "adds context", |
| 172 | [realNumber], |
| 173 | ([num]) => |
| 174 | Effect.gen(function*() { |
| 175 | const foo = yield* Foo |
| 176 | expect(foo).toEqual("foo") |
| 177 | return num === num |
| 178 | }), |
| 179 | { arbitrary: { runs: 200 } } |
| 180 | ) |
| 181 | |
| 182 | it.effect.prop( |
| 183 | "adds context with a Schema property", |
| 184 | [Schema.Int], |
| 185 | ([value]) => |
| 186 | Effect.gen(function*() { |
| 187 | const foo = yield* Foo |
| 188 | assert.strictEqual(foo, "foo") |
| 189 | assert.isTrue(Number.isInteger(value)) |
| 190 | }), |
| 191 | { arbitrary: { runs: 5, seed: "vitest-arbitrary-layer" } } |
| 192 | ) |
| 193 | }) |
| 194 | }) |
| 195 | |
| 196 | describe("release with retried or repeated tests", () => { |
| 197 | const resources: Array<{ open: boolean }> = [] |
| 198 | afterAll(() => { |
| 199 | expect(resources.map((resource) => resource.open)).toEqual([false, false]) |
| 200 | }) |
| 201 | |
| 202 | class Resource extends Context.Service<Resource, { readonly open: boolean }>()("Resource") { |
| 203 | static layer = Layer.effect(Resource)( |
| 204 | Effect.acquireRelease( |
| 205 | Effect.sync(() => { |
| 206 | const resource = { open: true } |
| 207 | resources.push(resource) |
| 208 | return resource |
| 209 | }), |
| 210 | (resource) => Effect.sync(() => resource.open = false) |
| 211 | ) |
| 212 | ) |
| 213 | } |
| 214 | |
| 215 | const assertOpen = Effect.map(Resource, (resource) => expect(resource.open).toEqual(true)) |
| 216 | |
| 217 | it.layer(Resource.layer)((it) => { |
| 218 | it.effect("retried", ({ task }) => |
| 219 | Effect.andThen( |
| 220 | assertOpen, |
| 221 | task.result?.retryCount === 0 ? Effect.die("first attempt fails") : Effect.void |
| 222 | ), { retry: 1 }) |
| 223 | it.effect("after retried", () => assertOpen) |
| 224 | }) |
| 225 | |
| 226 | it.layer(Resource.layer)((it) => { |
| 227 | it.effect("repeated", () => assertOpen, { repeats: 1 }) |
| 228 | it.effect("after repeated", () => assertOpen) |
| 229 | }) |
| 230 | }) |
| 231 | |
| 232 | describe("anonymous layer next to a Vitest fixture", () => { |
| 233 | const withValue = it.extend("value", () => 1) |
| 234 | |
| 235 | layer(Foo.layer)((it) => { |
| 236 | it.effect("provides its context", () => Effect.map(Foo, (foo) => expect(foo).toEqual("foo"))) |
| 237 | }) |
| 238 | |
| 239 | withValue("runs the fixture test", ({ value }) => { |
| 240 | expect(value).toEqual(1) |
| 241 | }) |
| 242 | }) |
| 243 | |
| 244 | layer(Sleeper.layer)("test services", (it) => { |
| 245 | it.effect("TestClock", () => |
| 246 | Effect.gen(function*() { |
| 247 | const sleeper = yield* Sleeper |
| 248 | const fiber = yield* Effect.forkChild(sleeper.sleep(100_000)) |
| 249 | yield* Effect.yieldNow |
| 250 | yield* TestClock.adjust(100_000) |
| 251 | yield* Fiber.join(fiber) |
| 252 | })) |
| 253 | }) |
| 254 | |
| 255 | layer(Foo.layer)("with a name", (it) => { |
| 256 | describe("with a nested describe", () => { |
| 257 | it.effect("adds context", () => |
| 258 | Effect.gen(function*() { |
| 259 | const foo = yield* Foo |
| 260 | expect(foo).toEqual("foo") |
| 261 | })) |
| 262 | }) |
| 263 | it.effect("adds context", () => |
| 264 | Effect.gen(function*() { |
| 265 | const foo = yield* Foo |
| 266 | expect(foo).toEqual("foo") |
| 267 | })) |
| 268 | }) |
| 269 | |
| 270 | layer(Sleeper.layer, { excludeTestServices: true })("live services", (it) => { |
| 271 | it.effect("Clock", () => |
| 272 | Effect.gen(function*() { |
| 273 | const sleeper = yield* Sleeper |
| 274 | yield* sleeper.sleep(1) |
| 275 | })) |
| 276 | }) |
| 277 | }) |
| 278 | |
| 279 | describeWrapped("describeWrapped", (it) => { |
| 280 | it.layer(Foo.layer)("named layer", (it) => { |
| 281 | it.effect("registers in its own suite", ({ task }) => |
| 282 | Effect.gen(function*() { |
| 283 | assert.strictEqual(task.suite?.name, "named layer") |
| 284 | assert.isTrue(task.suite?.tasks.includes(task)) |
| 285 | assert.strictEqual(yield* Foo, "foo") |
| 286 | })) |
| 287 | }) |
| 288 | }) |
| 289 | |
| 290 | describe("Effect hooks", () => { |
| 291 | let beforeAllRan = false |
| 292 | let beforeEachRan = false |
| 293 | |
| 294 | beforeAll(() => Effect.sync(() => (beforeAllRan = true))) |
| 295 | beforeEach(() => Effect.sync(() => (beforeEachRan = true))) |
| 296 | |
| 297 | it("runs Effects returned by beforeAll and beforeEach", () => { |
| 298 | assert.isTrue(beforeAllRan) |
| 299 | assert.isTrue(beforeEachRan) |
| 300 | }) |
| 301 | }) |
| 302 | |
| 303 | describe("scoped Effect hooks", { concurrent: false }, () => { |
| 304 | const events: Array<string> = [] |
| 305 | const resource = (name: string) => |
| 306 | Effect.acquireRelease( |
| 307 | Effect.sync(() => events.push(`${name}:acquired`)), |
| 308 | () => Effect.sleep(10).pipe(Effect.andThen(Effect.sync(() => events.push(`${name}:released`)))) |
| 309 | ) |
| 310 | |
| 311 | describe("suite", () => { |
| 312 | beforeAll(() => resource("all")) |
| 313 | beforeEach(() => resource("each")) |
| 314 | |
| 315 | it("keeps hook resources alive during the test", () => { |
| 316 | assert.deepStrictEqual(events, ["all:acquired", "each:acquired"]) |
| 317 | }) |
| 318 | |
| 319 | it("releases the beforeEach resource before the next test", () => { |
| 320 | assert.deepStrictEqual(events, ["all:acquired", "each:acquired", "each:released", "each:acquired"]) |
| 321 | }) |
| 322 | }) |
| 323 | |
| 324 | it("releases the beforeAll resource when the suite ends", () => { |
| 325 | assert.deepStrictEqual(events, [ |
| 326 | "all:acquired", |
| 327 | "each:acquired", |
| 328 | "each:released", |
| 329 | "each:acquired", |
| 330 | "each:released", |
| 331 | "all:released" |
| 332 | ]) |
| 333 | }) |
| 334 | |
| 335 | describe("failing hook", () => { |
| 336 | beforeEach(() => resource("failed").pipe(Effect.andThen(Effect.fail("boom")))) |
| 337 | it.fails("fails the test", () => {}) |
| 338 | }) |
| 339 | |
| 340 | describe("hook returning a cleanup function", () => { |
| 341 | beforeEach(() => resource("cleanup").pipe(Effect.as(() => events.push("cleanup:ran")))) |
| 342 | it("runs the test", () => {}) |
| 343 | }) |
| 344 | |
| 345 | describe("hook returning a throwing cleanup function", () => { |
| 346 | beforeEach(() => |
| 347 | resource("throwing").pipe(Effect.as(() => { |
| 348 | throw new Error("cleanup failed") |
| 349 | })) |
| 350 | ) |
| 351 | it.fails("reports the cleanup error", () => {}) |
| 352 | }) |
| 353 | |
| 354 | it("closes the scope after a failure and after a returned cleanup function", () => { |
| 355 | assert.deepStrictEqual(events.slice(6), [ |
| 356 | "failed:acquired", |
| 357 | "failed:released", |
| 358 | "cleanup:acquired", |
| 359 | "cleanup:ran", |
| 360 | "cleanup:released", |
| 361 | "throwing:acquired", |
| 362 | "throwing:released" |
| 363 | ]) |
| 364 | }) |
| 365 | }) |
| 366 | |
| 367 | // property testing |
| 368 | |
| 369 | const realNumber = Schema.Finite |
| 370 | const textArbitrary = Arbitrary.schema(Schema.Literals(["a", "b"])) |
| 371 | |
| 372 | it.prop( |
| 373 | "schema with array", |
| 374 | [Schema.String, Schema.Int], |
| 375 | ([text, count]) => typeof text === "string" && Number.isInteger(count) |
| 376 | ) |
| 377 | |
| 378 | it.prop( |
| 379 | "schema with object", |
| 380 | { text: Schema.String, count: Schema.Int }, |
| 381 | ({ text, count }) => typeof text === "string" && Number.isInteger(count) |
| 382 | ) |
| 383 | |
| 384 | let mixedTupleRuns = 0 |
| 385 | let mixedRecordRuns = 0 |
| 386 | afterAll(() => { |
| 387 | assert.strictEqual(mixedTupleRuns, 5) |
| 388 | assert.strictEqual(mixedRecordRuns, 5) |
| 389 | }) |
| 390 | |
| 391 | it.prop( |
| 392 | "Schema and Arbitrary with array", |
| 393 | [Schema.Int, textArbitrary], |
| 394 | ([count, text]) => { |
| 395 | mixedTupleRuns++ |
| 396 | assert.isTrue(Number.isInteger(count)) |
| 397 | assert.include(["a", "b"], text) |
| 398 | }, |
| 399 | { arbitrary: { runs: 5, maxDiscards: 0, seed: "vitest-mixed-tuple" } } |
| 400 | ) |
| 401 | |
| 402 | it.effect.prop( |
| 403 | "Schema and Arbitrary with object", |
| 404 | { count: Schema.Int, text: textArbitrary }, |
| 405 | ({ count, text }) => |
| 406 | Effect.sync(() => { |
| 407 | mixedRecordRuns++ |
| 408 | assert.isTrue(Number.isInteger(count)) |
| 409 | assert.include(["a", "b"], text) |
| 410 | }), |
| 411 | { arbitrary: { runs: 5, maxDiscards: 0, seed: "vitest-mixed-record" } } |
| 412 | ) |
| 413 | |
| 414 | it.prop("symmetry", [realNumber, Schema.Int], ([a, b]) => a + b === b + a) |
| 415 | |
| 416 | it.prop( |
| 417 | "symmetry with object", |
| 418 | { a: realNumber, b: Schema.Int }, |
| 419 | ({ a, b }) => a + b === b + a |
| 420 | ) |
| 421 | |
| 422 | it.live.prop( |
| 423 | "schema with object", |
| 424 | { value: Schema.Int }, |
| 425 | ({ value }) => Effect.sync(() => assert.isTrue(Number.isInteger(value))) |
| 426 | ) |
| 427 | |
| 428 | let arbitraryEffectRuns = 0 |
| 429 | afterAll(() => assert.strictEqual(arbitraryEffectRuns, 5)) |
| 430 | |
| 431 | it.effect.prop( |
| 432 | "schema with Arbitrary options", |
| 433 | [Schema.String, Schema.Int], |
| 434 | ([text, count]) => |
| 435 | Effect.sync(() => { |
| 436 | arbitraryEffectRuns++ |
| 437 | assert.strictEqual(typeof text, "string") |
| 438 | assert.isTrue(Number.isInteger(count)) |
| 439 | }), |
| 440 | { arbitrary: { runs: 5, maxDiscards: 0, seed: "vitest-arbitrary" } } |
| 441 | ) |
| 442 | |
| 443 | it.effect.prop("symmetry", [realNumber, Schema.Int], ([a, b]) => |
| 444 | Effect.gen(function*() { |
| 445 | yield* Effect.void |
| 446 | assert.isTrue(a + b === b + a) |
| 447 | })) |
| 448 | |
| 449 | it.effect.prop("symmetry with object", { a: realNumber, b: Schema.Int }, ({ a, b }) => |
| 450 | Effect.gen(function*() { |
| 451 | yield* Effect.void |
| 452 | assert.strictEqual(a + b, b + a) |
| 453 | })) |
| 454 | |
| 455 | it.effect.prop( |
| 456 | "should detect the substring", |
| 457 | { a: Schema.String, b: Schema.String, c: Schema.String }, |
| 458 | ({ a, b, c }) => |
| 459 | Effect.gen(function*() { |
| 460 | yield* Effect.scope |
| 461 | assert.include(a + b + c, b) |
| 462 | }) |
| 463 | ) |
| 464 | |
| 465 | describe("property failures", () => { |
| 466 | it.prop( |
| 467 | "rejects Effect returns from synchronous properties", |
| 468 | [Schema.Int], |
| 469 | () => Effect.fail("must not silently pass") as unknown as void, |
| 470 | { fails: true } |
| 471 | ) |
| 472 | |
| 473 | const Input = Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 1_000 })) |
| 474 | const pureDefectValues: Array<number> = [] |
| 475 | const effectDefectValues: Array<number> = [] |
| 476 | let interruptedRuns = 0 |
| 477 | let timeoutPropertyStarted = false |
| 478 | let timeoutPropertyReleased = false |
| 479 | |
| 480 | afterAll(() => { |
| 481 | assert.deepStrictEqual(pureDefectValues, [8, 1]) |
| 482 | assert.deepStrictEqual(effectDefectValues, [8, 1]) |
| 483 | assert.strictEqual(interruptedRuns, 1) |
| 484 | assert.isTrue(timeoutPropertyStarted) |
| 485 | assert.isTrue(timeoutPropertyReleased) |
| 486 | }) |
| 487 | |
| 488 | it.prop( |
| 489 | "shrinks synchronous defects", |
| 490 | [Input], |
| 491 | ([value]) => { |
| 492 | pureDefectValues.push(value) |
| 493 | throw new Error("property defect") |
| 494 | }, |
| 495 | { fails: true, arbitrary: { runs: 1, seed: "assertion-shrink" } } |
| 496 | ) |
| 497 | |
| 498 | it.effect.prop( |
| 499 | "shrinks Effect defects", |
| 500 | [Input], |
| 501 | ([value]) => |
| 502 | Effect.sync(() => { |
| 503 | effectDefectValues.push(value) |
| 504 | assert.strictEqual(value, 0) |
| 505 | }), |
| 506 | { fails: true, arbitrary: { runs: 1, seed: "assertion-shrink" } } |
| 507 | ) |
| 508 | |
| 509 | it.effect.prop( |
| 510 | "preserves interruption", |
| 511 | [Input], |
| 512 | () => { |
| 513 | interruptedRuns++ |
| 514 | return Effect.interrupt |
| 515 | }, |
| 516 | { fails: true, arbitrary: { runs: 1, seed: "assertion-shrink" } } |
| 517 | ) |
| 518 | |
| 519 | it.effect.prop( |
| 520 | "interrupts property checking on timeout", |
| 521 | [Schema.Literal("value")], |
| 522 | () => |
| 523 | Effect.acquireUseRelease( |
| 524 | Effect.sync(() => { |
| 525 | timeoutPropertyStarted = true |
| 526 | }), |
| 527 | () => Effect.never, |
| 528 | () => |
| 529 | Effect.sync(() => { |
| 530 | timeoutPropertyReleased = true |
| 531 | }) |
| 532 | ), |
| 533 | { fails: true, timeout: 10, arbitrary: { runs: 1, maxDiscards: 0, seed: "property-timeout" } } |
| 534 | ) |
| 535 | }) |