Skip to content

romeda-labs/zest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

1 Commit
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Zest

Zest is a general testing library for roblox-ts inspired by tiniest.

Installation

npm install @rbxts/zest
yarn add @rbxts/zest
pnpm add @rbxts/zest

Example

A 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();
}

License

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/.

About

๐Ÿ‹ Zest is a general testing library for roblox-ts inspired by tiniest.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages