How to test Options
To assert Option values without unwrapping them by hand, install byenull-testing and register its matchers. toBeSome and toBeNone read better than toEqual, and their failure messages show the received value.
Install the package
pnpm add -D byenull-testing
Set up Vitest
Register the matchers in a setup file, load it from your Vitest configuration, and augment the matcher types:
vitest.setup.ts
import { expect } from "vitest";
import { toBeNone, toBeSome } from "byenull-testing";
import type { OptionMatchers } from "byenull-testing";
declare module "vitest" {
interface Matchers<R, T> extends OptionMatchers<R, T> {}
}
expect.extend({ toBeSome, toBeNone });
vitest.config.ts
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
setupFiles: ["./vitest.setup.ts"],
},
});
Set up Jest
The same matchers work in Jest. Register them in a setup file and augment the expect module:
jest.setup.ts
import { expect } from "@jest/globals";
import { toBeNone, toBeSome } from "byenull-testing";
import type { OptionMatchers } from "byenull-testing";
declare module "expect" {
interface Matchers<R, T> extends OptionMatchers<R, T> {}
}
expect.extend({ toBeSome, toBeNone });
jest.config.ts
export default {
setupFilesAfterEnv: ["./jest.setup.ts"],
};
Assert a Some and its value
To check that a value is a Some and assert on its contents in one step, pass a callback to toBeSome. The callback parameter is typed as the Some value:
import { Option } from "byenull";
import { expect, test } from "vitest";
import type { OptionMatchers } from "byenull-testing";
declare module "vitest" {
interface Matchers<R, T> extends OptionMatchers<R, T> {}
}
// ---cut-before---
type User = { id: string; name: string };
const findUser = Option.fn((id: string, users: User[]) => users.find((user) => user.id === id));
test("finds the user", () => {
const users: User[] = [{ id: "u1", name: "Alice" }];
expect(findUser("u1", users)).toBeSome((user) => {
expect(user.name).toBe("Alice"); // user is typed as User
});
});
The callback only runs when the value is a Some. A None fails the matcher and the callback is skipped.
Assert a None
To check that a lookup found nothing, use toBeNone:
import { Option } from "byenull";
import { expect, test } from "vitest";
import type { OptionMatchers } from "byenull-testing";
declare module "vitest" {
interface Matchers<R, T> extends OptionMatchers<R, T> {}
}
// ---cut-before---
type User = { id: string; name: string };
const findUser = Option.fn((id: string, users: User[]) => users.find((user) => user.id === id));
test("finds no user", () => {
const users: User[] = [];
expect(findUser("u404", users)).toBeNone();
});
Do not combine the matchers with .not
.not.toBeSome and .not.toBeNone always pass. The matchers are built so that negation is pointless. To assert the opposite outcome, use the other matcher: toBeSome for presence, toBeNone for absence.