Enums
toBeEnum(enum)
Also available as an asymmetric matcher: expect.ofEnum(enum).
Use .toBeEnum to assert that a value is a member of an enum (or const object).
- Numeric enum
- String enum
- Heterogeneous enum
- Const object
enum Direction {
Up,
Down,
Left,
Right,
}
it("is a member of the numeric enum", () => {
expect(Direction.Up).toBeEnum(Direction);
});
enum Direction {
Up = "UP",
Down = "DOWN",
Left = "LEFT",
Right = "RIGHT",
}
it("is a member of the string enum", () => {
expect(Direction.Up).toBeEnum(Direction);
});
enum Direction {
Up = "UP",
Down = 1,
Left = "LEFT",
Right = 2,
}
it("is a member of the heterogeneous enum", () => {
expect(Direction.Up).toBeEnum(Direction);
});
const Direction = {
Up: "UP",
Down: "DOWN",
Left: "LEFT",
Right: "RIGHT",
} as const;
it("is a member of the const object", () => {
expect(Direction.Up).toBeEnum(Direction);
});
Aliasing
expect.ofEnum to expect.enumAs enum is a reserved word in Javascript, it is not possible to export a matcher with this name. However, you can alias it in your setup file:
- Jest
- Jest (no globals)
- Vitest
- Rstest
test-setup.ts
import { exactly, ofEnum } from "mix-n-matchers";
import type { MixNMatchersFrom, AsymmetricMixNMatchersFrom } from "mix-n-matchers";
const matchers = {
exactly,
enum: ofEnum,
};
expect.extend(matchers);
declare global {
namespace jest {
interface Matchers<R, T> extends MixNMatchersFrom<typeof matchers, R, T> {}
interface Expect extends AsymmetricMixNMatchersFrom<typeof matchers> {
enum: AsymmetricMatchers["ofEnum"];
}
interface InverseAsymmetricMatchers
extends AsymmetricMixNMatchersFrom<typeof matchers> {
enum: AsymmetricMatchers["ofEnum"];
}
}
}
test-setup.ts
import { expect } from "@jest/globals";
import { exactly, ofEnum } from "mix-n-matchers";
import type { MixNMatchersFrom, AsymmetricMixNMatchersFrom } from "mix-n-matchers";
const matchers = {
exactly,
enum: ofEnum,
};
expect.extend(matchers);
declare module "@jest/expect" {
interface Matchers<R, T> extends MixNMatchersFrom<typeof matchers, R, T> {}
interface AsymmetricMatchers
extends AsymmetricMixNMatchersFrom<typeof matchers> {
enum: AsymmetricMatchers["ofEnum"];
}
}
test-setup.ts
import { expect } from "vitest";
import { exactly, ofEnum } from "mix-n-matchers";
import type { MixNMatchersFrom, AsymmetricMixNMatchersFrom } from "mix-n-matchers";
const matchers = {
exactly,
enum: ofEnum,
};
expect.extend(matchers);
declare module "vitest" {
interface Assertion<T> extends MixNMatchersFrom<typeof matchers, void, T> {}
interface AsymmetricMatchersContaining
extends AsymmetricMixNMatchersFrom<typeof matchers> {
enum: AsymmetricMatchers["ofEnum"];
}
}
test-setup.ts
import { expect } from "@rstest/core";
import { exactly, ofEnum } from "mix-n-matchers";
import type { MixNMatchersFrom, AsymmetricMixNMatchersFrom } from "mix-n-matchers";
const matchers = {
exactly,
enum: ofEnum,
};
expect.extend(matchers);
declare module "@rstest/core" {
interface Assertion<T> extends MixNMatchersFrom<typeof matchers, void, T> {}
interface AsymmetricMatchersContaining
extends AsymmetricMixNMatchersFrom<typeof matchers> {
enum: AsymmetricMatchers["ofEnum"];
}
}
After this setup, you should be able to use expect.enum as a matcher.
expect(mock).toHaveBeenCalledWith(expect.enum(MyEnum));
This is automatically done for you with the auto-setup files.