TestTube master READ ONLY 0
  • explorer
  • search
a [App]
a CG.TestTube
a README.md
a app.ts
a [Test]
a test
  • README.md

TestTube

TestTubes is a simple library for the CG platform.

Usage

Basic Usage

First, import the required functions from the library:

const {test, expect, runTests} = CG.TestTube;

Writing Tests

You can write tests using the test function. Each test is defined with a description, a test function, and optional tags for categorization:

test("adds numbers correctly", () => {
    expect(1 + 2).toBe(3);
}, ["math"]);

Running Tests

To run all registered tests, use the runTests function:

runTests();

You can also run tests with a specific tag:

runTests("math");

Assertions

TestTubes provides a simple expect function to make assertions, including:

  • toBe(expected): Asserts that the actual value is strictly equal to the expected value.
  • toEqual(expected): Asserts that the actual value is deeply equal to the expected value.
  • toBeTruthy(): Asserts that the actual value is truthy.
  • toBeFalsy(): Asserts that the actual value is falsy.
  • toThrow(expectedMessage?): Asserts that the actual function throws an error, optionally matching the expected message.

Example

Here's a very simple example demonstrating the usage of TestTubes for a simple math library and a DOM manipulation library:

const {test, expect, runTests} = CG.TestTube;

// Math library
export const MathLib = {
    add(a: number, b: number): number {
        return a + b;
    },
    subtract(a: number, b: number): number {
        // Typing error
        return a + b;
    },
    multiply(a: number, b: number): number {
        return a * b;
    },
    divide(a: number, b: number): number {
        if (b === 0) throw new Error("Cannot divide by zero");
        return a / b;
    }
};

// DOM manipulation library
export const DomLib = {
    createElement(tag: string, content: string): HTMLElement {
        const element = document.createElement(tag);
        element.textContent = content;
        return element;
    },
    appendToBody(element: HTMLElement): void {
        document.body.appendChild(element);
    }
};

// Math library tests
test("adds numbers correctly", () => {
    expect(MathLib.add(1, 2)).toBe(3);
}, ["math"]);

test("subtracts numbers correctly", () => {
    expect(MathLib.subtract(2, 1)).toBe(1);
}, ["math"]);

test("multiplies numbers correctly", () => {
    expect(MathLib.multiply(2, 2)).toBe(4);
}, ["math"]);

test("divides numbers correctly", () => {
    expect(MathLib.divide(4, 2)).toBe(2);
}, ["math", "division"]);

test("throws error when dividing by zero", () => {
    expect(() => MathLib.divide(4, 0)).toThrow("Cannot divide by zero");
}, ["math", "division"]);

// DOM manipulation tests
test("creates an element with correct tag and content", () => {
    const element = DomLib.createElement("div", "Hello, World!");
    expect(element.tagName.toLowerCase()).toBe("div");
    expect(element.textContent).toBe("Hello, World!");
}, ["dom"]);

test("appends an element to the body", () => {
    const element = DomLib.createElement("div", "Appended Element");
    DomLib.appendToBody(element);
    expect(document.body.contains(element)).toBeTruthy();
}, ["dom"]);

// Run all tests
runTests();

// Optionally run only tests with a specific tag
// runTests("math");

The output looks like this:

 Running 7 tests...
✅ adds numbers correctly
❌ subtracts numbers correctly
Expected 3 to be 1
✅ multiplies numbers correctly
✅ divides numbers correctly
✅ throws error when dividing by zero
✅ creates an element with correct tag and content
✅ appends an element to the body 

API

test(description: string, fn: TestFunction, tags: string[] = [])

Registers a new test.

  • description: A description of the test.
  • fn: The test function to execute.
  • tags: Optional tags to categorize the test.

runTests(tag?: string)

Runs all registered tests, or only tests with the specified tag.

  • tag: Optional tag to filter tests to run.

expect(actual: unknown)

Creates an expectation for a value. Returns an object with assertion methods.

Authors

FOBShippingPoint

READ ONLY
  • problem
  • console
No problems have been detected so far.
Ln 1, Col 1, Sel 0
Markdown