The nameOf
function provides type-safe ways to get formatted names from functions, classes and their properties.
import { nameOf } from "@replrn/testing-utility";
import { expect } from "expect";
class MyClass {
myProperty: string = "";
myOtherProperty: number = 0;
}
const className = nameOf(MyClass);
expect(className).toBe("My Class");
const propertyName = nameOf(MyClass, "myProperty");
expect(propertyName).toBe("My Class: My Property");
const properties = nameOf(MyClass, ["myProperty", "myOtherProperty"]);
expect(properties).toBe("My Class: My Property | My Other Property");
const withDetail = nameOf(MyClass, "myProperty", "detail");
expect(withDetail).toBe("My Class: My Property (detail)");
import { nameOf } from "@replrn/testing-utility";
import { expect } from "expect";
function myFunction() { }
const functionName = nameOf(myFunction);
expect(functionName).toBe("My Function");
const withDetail = nameOf(myFunction, "detail");
expect(withDetail).toBe("My Function: Detail");
The isDefined
function is a type guard for Vitest's expect
. It narrows the type of the expectation to exclude undefined/null.
import { expectIsDefined } from "@replrn/testing-utility";
import { expectTypeOf } from "expect-type";
let x: number | undefined;
expectTypeOf(x).toBeNullable();
expectTypeOf(x).not.toBeNumber();
expectIsDefined(x);
expectTypeOf(x).not.toBeNullable();
expectTypeOf(x).toBeNumber();