TestContext - huang2002/3h-test Wiki

TestContext

/**
 * Class of assertion errors.
 */
class AssertionError extends Error {
}

/**
 * Type of context options.
 */
type TestContextOptions = Partial<{
    timeout: number;
}>;

/**
 * Class of test contexts.
 * (Usually created internally and passed to test case callbacks)
 */
class TestContext implements Required<TestContextOptions> {

    /**
     * Assert there are not pending labels in the given context.
     * (usually used internally)
     */
    static checkPendingLabels(context: TestContext): void;

    /**
     * Constructor of {@link TestContext}.
     */
    constructor(options?: TestContextOptions | null);

    /**
     * Maximum timeout in milliseconds.
     * (After this limit, the context will check whether
     * there are remaining pending labels left by asynchronous
     * operations and report existing labels as an error.)
     * @default 5000
     */
    timeout: number;

    /**
     * Whether the test case is finished.
     * (assigned internally)
     */
    finished: boolean;

    /**
     * Count of raised errors.
     */
    errorCount: number;

    /**
     * (this should be modified only by
     * `this.addPendingLabel` & `this.deletePendingLabel`.)
     */
    pendingLabels: Set<string>;

    /**
     * A promise resolved when the test case is finished.
     */
    readonly promise: Promise<void>;

    /**
     * Finish the test case.
     * (usually invoked internally and automatically)
     */
    finish: () => void;

    /**
     * Assert the test case is not finished.
     * (usually used internally)
     */
    checkFinished(): void;

    /**
     * Raise an error and invoke `this.checkFinished`
     * unless the `checkFinished` parameter is `false`.
     */
    throw(message: string, checkFinished?: boolean): void;

    /**
     * Assert the given condition is `true`.
     */
    assert(condition: boolean, message?: string): void;

    /**
     * Assert `value == expected`.
     */
    assertEqual(value: unknown, expected: unknown): void;

    /**
     * Assert `value === expected`.
     */
    assertStrictEqual(value: unknown, expected: unknown): void;

    /**
     * Assert `value` and `expected` are shallowly equal.
     * (using `Utils.compare` internally)
     */
    assertShallowEqual(value: unknown, expected: unknown, message?: string): void;

    /**
     * Assert `value` and `expected` are deeply equal.
     * (using `Utils.compareDeeply` internally)
     */
    assertDeepEqual(value: unknown, expected: unknown, message?: string): void;

    /**
     * Assert `value` and `expected` are equal in JSON format.
     * (using `JSON.stringify` & `Utils.compare` internally.)
     */
    assertJSONEqual(value: unknown, expected: unknown, message?: string): void;

    /**
     * Assert the given callback to throw a specific type of error.
     * (when `errorType` is a string, assert the type of the raised error
     * equals `errorType`; otherwise, assert the raised error is
     * an instance of `errorType`.)
     */
    expectThrow<T extends any[], U>(errorType: Function | string, callback: (this: U, ...args: T) => any, args?: T, thisArg?: U, errorCallback?: (error: unknown) => void): void;

    /**
     * Set up pending check.
     */
    protected _setPendingCheck(): void;

    /**
     * Add a pending label.
     */
    addPendingLabel(label: string): void;

    /**
     * Remove a pending label.
     */
    deletePendingLabel(label: string): void;

    /**
     * Expect the given promise to be resolved.
     * @param promise Target promise.
     * @param label A pending label for debug use.
     * @param callback A callback invoked with resolved
     * data when the promise is resolved, which can be
     * used to do some extra check on the data.
     */
    expectResolved<T>(promise: Promise<T>, label: string, callback?: (data: T) => void): void;

    /**
     * Expect the given promise to be rejected.
     * @param promise Target promise.
     * @param label A pending label for debug use.
     * @param callback A callback invoked with rejected
     * reason when the promise is rejected, which can be
     * used to do some extra check on the reason.
     */
    expectRejected(promise: Promise<any>, label: string, callback?: (reason: unknown) => void): void;

    /**
     * Invoke `callback` after specific milliseconds
     * to do some tests later.
     * @param callback The function to invoke.
     * @param milliseconds Timeout in milliseconds.
     * @param label A pending label for debug use.
     * @returns A canceler function
     * which can be used to cancel the invocation.
     */
    setTimeout(callback: () => void, milliseconds: number, label: string): () => void;
}
⚠️ **GitHub.com Fallback** ⚠️