Zest is a general testing library for roblox-ts inspired by tiniest.
npm install @rbxts/zest
yarn add @rbxts/zest
pnpm add @rbxts/zestA real spec from the OCPlace codebase:
// dissolve.ts
import { Derivable, read } from "@rbxts/vide";
/**
* Dissolves a source into a constant value if it has no `read` dependencies.
*/
export function dissolve<T>(source: (read: <T>(value: Derivable<T>) => T) => T): Derivable<T> {
let readsDependency = false;
function mockRead(value: unknown) {
if (typeIs(value, "function")) {
readsDependency = true;
throw "used dependency";
}
return value;
}
const [initialSuccess, dissolvedOrError] = pcall(source, mockRead as never);
if (initialSuccess) return dissolvedOrError;
if (readsDependency) {
return () => {
return source(read as never);
};
}
throw `Dissolving source threw an error: ${dissolvedOrError}`;
}// dissolve.spec.ts
import { read, source } from "@rbxts/vide";
import { expect, it } from "@rbxts/zest";
import { dissolve } from "./dissolve";
export = () => {
it("dissolves no read dependencies", () => {
const constant = {};
const dissolved = dissolve(() => constant);
expect(dissolved).toNeverBeA("function");
expect(dissolved).toBeA("table");
expect(dissolved).toBe(constant);
});
it("dissolves constants", () => {
const constant = {};
const dissolved = dissolve((read) => read(constant));
expect(dissolved).toNeverBeA("function");
expect(dissolved).toBeA("table");
expect(dissolved).toBe(constant);
});
it("keeps read dependencies", () => {
const constant = {};
const derivable = source(constant);
const dissolved = dissolve((read) => read(derivable));
expect(dissolved).toNeverBeA("table");
expect(dissolved).toBeA("function");
expect(read(dissolved)).toBe(constant);
});
};Bootstrapping:
// main.server.ts
if (RunService.IsStudio()) {
const specModules = [
...ReplicatedStorage.WaitForChild("Shared").GetDescendants(),
...ServerScriptService.WaitForChild("Server").GetDescendants(),
].filter((instance) => instance.IsA("ModuleScript") && instance.Name.match("%.spec$")[0] !== undefined);
logger.trace("Running zest suite");
new Suite()
.collectModules(specModules)
.runAndPrint();
}This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.