Skip to content

Commit

Permalink
feat: add fails method
Browse files Browse the repository at this point in the history
  • Loading branch information
lishaduck committed Nov 20, 2024
1 parent 6da6284 commit a4329ab
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-pans-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/vitest": minor
---

Add fails method.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.preferences.importModuleSpecifier": "relative",
"typescript.enablePromptUseWorkspaceTsdk": true,
"deno.enable": false,
"biome.enabled": false,
"editor.formatOnSave": true,
"eslint.format.enable": true,
"[json]": {
Expand Down
24 changes: 24 additions & 0 deletions packages/vitest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,30 @@ it.effect.only("test failure as Exit", () =>
)
```

## Expecting Tests to Fail

When adding new failing tests, you might not be able to fix them right away. Instead of skipping them, you may want to assert it fails, so that when you fix them, you'll know and can re-enable them before it regresses.

**Example** (Asserting one test fails)

```ts
import { it } from "@effect/vitest"
import { Effect, Exit } from "effect"

function divide(a: number, b: number): number {
if (b === 0) return Effect.fail("Cannot divide by zero")
return Effect.succeed(a / b)
}

// Temporarily assert the test for divide by zero fails.
it.effect.fails("dividing by zero special cases", ({ expect }) =>
Effect.gen(function* () {
const result = yield* Effect.exit(divide(4, 0))
expect(result).toStrictEqual(0)
})
)
```

## Logging

By default, `it.effect` suppresses log output, which can be useful for keeping test results clean. However, if you want to enable logging during tests, you can use `it.live` or provide a custom logger to control the output.
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export namespace Vitest {
each: <T>(
cases: ReadonlyArray<T>
) => <A, E>(name: string, self: TestFunction<A, E, R, Array<T>>, timeout?: number | V.TestOptions) => void
fails: Vitest.Test<R>

/**
* @since 1.0.0
Expand Down
5 changes: 4 additions & 1 deletion packages/vitest/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ const makeTester = <R>(
(args, ctx) => run(ctx, [args], self) as any
)

const fails: Vitest.Vitest.Tester<R>["fails"] = (name, self, timeout) =>
V.it.fails(name, (ctx) => run(ctx, [ctx], self), timeout)

const prop: Vitest.Vitest.Tester<R>["prop"] = (name, schemaObj, self, timeout) => {
if (Array.isArray(schemaObj)) {
const arbs = schemaObj.map((schema) => Arbitrary.make(schema))
Expand All @@ -123,7 +126,7 @@ const makeTester = <R>(
)
}

return Object.assign(f, { skip, skipIf, runIf, only, each, prop })
return Object.assign(f, { skip, skipIf, runIf, only, each, fails, prop })
}

export const prop: Vitest.Vitest.Methods["prop"] = (name, schemaObj, self, timeout) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/vitest/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ it.effect.runIf(false)("effect runIf (false)", () => Effect.die("not run anyway"

// The following test is expected to fail because it simulates a test timeout.
// Be aware that eventual "failure" of the test is only logged out.
it.scopedLive("interrupts on timeout", (ctx) =>
it.scopedLive.fails("interrupts on timeout", (ctx) =>
Effect.gen(function*() {
let acquired = false

Expand All @@ -84,7 +84,7 @@ it.scopedLive("interrupts on timeout", (ctx) =>
() => Effect.sync(() => acquired = false)
)
yield* Effect.sleep(1000)
}), { timeout: 100, fails: true })
}), 1)

class Foo extends Context.Tag("Foo")<Foo, "foo">() {
static Live = Layer.succeed(Foo, "foo")
Expand Down

0 comments on commit a4329ab

Please sign in to comment.