Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add fails method #3973

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
tim-smart marked this conversation as resolved.
Show resolved Hide resolved
---

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,
tim-smart marked this conversation as resolved.
Show resolved Hide resolved
"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 that the test for dividing 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