Skip to main content

Functions

API

Is a function

You can use the function method to check if a value is a function.

import { expect } from "@rbxts/expect";

expect(() => {
throw "Error";
}).to.be.a.function();

Example error

Expected '1' to be of type 'function', but it was a 'number'

Throws

You can use the throws method to check if a function throws an error.

import { expect } from "@rbxts/expect";

expect(() => {
throw "Error";
}).to.throw();

Example error

Expected the function to throw, but it didn't throw at all

Throws with named functions

Calling throws on a named function will use that function's name in its output.

import { expect } from "@rbxts/expect";

function iThrow() {
throw "Error";
}

expect(iThrow).to.throw();

Example error

Expected iDontThrow to throw, but it didn't throw at all

Throws substring

You can use an overload of the throws method to check if a function throws an error that contains a certain string.

import { expect } from "@rbxts/expect";

expect(() => {
throw "You don't have enough money";
}).to.throw("enough money");

Example error

Expected the function to throw with the substring "enough money", but it threw a message without it:
"You don't have enough space"

Throws pattern

You can use the throwsMatch method to check if a function throws an error that matches the given pattern.

import { expect } from "@rbxts/expect";

expect(() => {
throw "You don't have enough money";
}).to.throw("money$");

Example error

Expected iThrow to throw with a message that matched /money$/, but it threw a message without it:
"You don't have enough space"