@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
42 lines (41 loc) • 1.68 kB
TypeScript
/**
* Writes unit tests as if writing examples, inspired by Golang.
* @module
* @deprecated It turns out that this module isn't really helpful, and has
* compatibility issues with Bun, **tsx** and browsers, it will be removed in
* the future.
*/
/**
* Inspired by Golang's **Example as Test** design, creates a function that
* carries example code with `// output:` comments, when the returned function
* is called, it will automatically check if the actual output matches the one
* declared in the comment.
*
* The example function receives a customized `console` object which will be
* used to log outputs instead of using the built-in `console`.
*
* **NOTE:**
* This function is used to simplify the process of writing tests, currently,
* it does not work in Bun, **tsx** and browsers, because Bun hasn't implement
* the `Console` constructor and removes comments during runtime, **tsx** also
* remove comments, and the function depends on Node.js built-in modules.
*
* @example
* ```ts
* import example from "@ayonli/jsext/example";
*
* it("should output as expected", example(console => {
* console.log("Hello, World!");
* // output:
* // Hello, World!
* }));
* ```
*
* @deprecated It turns out that this function isn't really helpful, and has
* compatibility issues with Bun, **tsx** and browsers, it will be removed in
* the future.
*/
export default function example<T, A extends any[] = any[]>(fn: (this: T, console: Console, ...args: A) => void | Promise<void>, options?: {
/** Suppress logging to the terminal and only check the output. */
suppress?: boolean;
} | undefined): (this: T, ...args: A) => Promise<void>;