///
import { enforce } from 'n4s';
import { DynamicValue, OneOrMoreOf, Maybe, Nullable, CB, ValueOf } from "vest-utils";
import { InjectionToken } from "@angular/core";
import { TIsolate, IsolateKey } from "vestjs-runtime";
declare enum Severity {
WARNINGS = "warnings",
ERRORS = "errors"
}
declare enum TestSeverity {
Error = "error",
Warning = "warning"
}
declare const TestStatus: {
[x: string]: string;
CANCELED: string;
FAILED: string;
OMITTED: string;
PASSING: string;
SKIPPED: string;
UNTESTED: string;
WARNING: string;
};
type TestStatus = ValueOf;
type TestFnPayload = {
signal: AbortSignal;
};
type TestFn = (payload: TestFnPayload) => TestResult;
type AsyncTest = Promise;
type TestResult = Maybe | void;
type WithFieldName = {
fieldName: F;
};
type TIsolateTest = TIsolate & IsolateTestPayload>;
type IsolateTestPayload = CommonTestFields & {
severity: TestSeverity;
status: TestStatus;
asyncTest?: AsyncTest;
};
type CommonTestFields = {
message?: Maybe;
groupName?: G;
fieldName: F;
testFn: TestFn;
};
/**
* Injection token for accessing the current test context in Angular environments.
* Used internally by runSyncTest to provide the test object to the test function.
*/
declare const VEST_TEST_CONTEXT: InjectionToken>;
declare class SummaryFailure implements WithFieldName {
fieldName: F;
message: string | undefined;
groupName: G | undefined;
constructor(fieldName: F, message: string | undefined, groupName: G | undefined);
static fromTestObject(testObject: TIsolateTest): SummaryFailure;
}
interface Done {
(...args: [
cb: (res: SuiteResult) => void
]): SuiteRunResult;
(...args: [
fieldName: F,
cb: (res: SuiteResult) => void
]): SuiteRunResult;
}
// eslint-disable-next-line max-lines-per-function, max-statements
declare function suiteSelectors(summary: SuiteSummary): SuiteSelectors;
interface SuiteSelectors {
getWarning(): SummaryFailure | undefined;
getWarning(fieldName: F): string | undefined;
getWarning(fieldName?: F): SummaryFailure | string | undefined;
getError(): SummaryFailure | undefined;
getError(fieldName: F): string | undefined;
getError(fieldName?: F): SummaryFailure | string | undefined;
getMessage(fieldName: F): string | undefined;
getErrors(): FailureMessages;
getErrors(fieldName: F): string[];
getErrors(fieldName?: F): string[] | FailureMessages;
getWarnings(): FailureMessages;
getWarnings(fieldName: F): string[];
getWarnings(fieldName?: F): string[] | FailureMessages;
getErrorsByGroup(groupName: G): FailureMessages;
getErrorsByGroup(groupName: G, fieldName: F): string[];
getErrorsByGroup(groupName: G, fieldName?: F): string[] | FailureMessages;
getWarningsByGroup(groupName: G): FailureMessages;
getWarningsByGroup(groupName: G, fieldName: F): string[];
getWarningsByGroup(groupName: G, fieldName?: F): string[] | FailureMessages;
hasErrors(fieldName?: F): boolean;
hasWarnings(fieldName?: F): boolean;
hasErrorsByGroup(groupName: G, fieldName?: F): boolean;
hasWarningsByGroup(groupName: G, fieldName?: F): boolean;
isTested(fieldName: F): boolean;
isPending(fieldName?: F): boolean;
isValid(fieldName?: F): boolean;
isValidByGroup(groupName: G, fieldName?: F): boolean;
}
declare class SummaryBase {
errorCount: number;
warnCount: number;
testCount: number;
pendingCount: number;
}
declare class SuiteSummary extends SummaryBase {
[Severity.ERRORS]: SummaryFailure[];
[Severity.WARNINGS]: SummaryFailure[];
groups: Groups;
tests: Tests;
valid: Nullable;
}
type GroupTestSummary = SingleTestSummary;
type Groups = Record>;
type Group = Record;
type Tests = Record;
type SingleTestSummary = SummaryBase & {
errors: string[];
warnings: string[];
valid: Nullable;
pendingCount: number;
};
type FailureMessages = Record;
type SuiteResult = SuiteSummary & SuiteSelectors & {
suiteName: SuiteName;
};
type SuiteRunResult = SuiteResult & {
done: Done;
};
type SuiteName = Maybe;
type TFieldName = T;
type TGroupName = G;
type OptionalFields = Record;
type OptionalsInput = OneOrMoreOf | OptionalsObject;
type OptionalsObject = Record;
type ImmediateOptionalFieldDeclaration = {
type: OptionalFieldTypes.CUSTOM_LOGIC;
rule: TOptionalRule;
applied: boolean;
};
type DelayedOptionalFieldDeclaration = {
type: OptionalFieldTypes.AUTO;
applied: boolean;
rule: null;
};
type TOptionalRule = DynamicValue;
type OptionalFieldDeclaration = ImmediateOptionalFieldDeclaration | DelayedOptionalFieldDeclaration;
declare enum OptionalFieldTypes {
CUSTOM_LOGIC = 0,
AUTO = 1
}
// @vx-allow use-use
declare function optional(optionals: OptionalsInput): void;
declare enum Modes {
EAGER = "EAGER",
ALL = "ALL",
ONE = "ONE"
}
type TIsolateSuite = TIsolate<{
optional: OptionalFields;
}>;
type Events = "TEST_RUN_STARTED" | "TEST_COMPLETED" | "ALL_RUNNING_TESTS_FINISHED" | "REMOVE_FIELD" | "RESET_FIELD" | "RESET_SUITE" | "SUITE_RUN_STARTED" | "SUITE_CALLBACK_RUN_FINISHED" | "DONE_TEST_OMISSION_PASS";
type Subscribe = {
(event: Events, cb: CB): CB;
(cb: CB): CB;
};
declare enum FocusModes {
ONLY = 0,
SKIP = 1
}
type FieldExclusion = Maybe>;
type TIsolateFocused = TIsolate;
type IsolateFocusedPayload = {
focusMode: FocusModes;
match: FieldExclusion;
matchAll: boolean;
};
/**
* Adds a field or a list of fields into the inclusion list
*
* @example
*
* only('username');
*/
// @vx-allow use-use
declare function only(match: FieldExclusion | false): TIsolateFocused;
/**
* Adds a field or a list of fields into the exclusion list
*
* @example
*
* skip('username');
*/
// @vx-allow use-use
declare function skip(match: FieldExclusion | boolean): TIsolateFocused;
declare function vestTest(fieldName: F, message: string, cb: TestFn): TIsolateTest;
declare function vestTest(fieldName: F, cb: TestFn): TIsolateTest;
declare function vestTest(fieldName: F, message: string, cb: TestFn, key: IsolateKey): TIsolateTest;
declare function vestTest(fieldName: F, cb: TestFn, key: IsolateKey): TIsolateTest;
declare const test: typeof vestTest & {
memo: TestMemo;
};
type TestMemo = {
(fieldName: F, ...args: ParametersWithoutMessage): TIsolateTest;
(fieldName: F, ...args: ParametersWithMessage): TIsolateTest;
};
type ParametersWithoutMessage = [
test: TestFn,
dependencies: unknown[]
];
type ParametersWithMessage = [
message: string,
test: TestFn,
dependencies: unknown[]
];
type TTypedMethods = {
include: (fieldName: F) => {
when: (condition: F | TDraftCondition) => void;
};
omitWhen: (conditional: TDraftCondition, callback: CB) => void;
only: {
(item: FieldExclusion): void;
};
optional: (optionals: OptionalsInput) => void;
skip: {
(item: FieldExclusion): void;
};
skipWhen: (condition: TDraftCondition, callback: CB) => void;
test: {
(fieldName: F, message: string, cb: TestFn): TIsolateTest;
(fieldName: F, cb: TestFn): TIsolateTest;
(fieldName: F, message: string, cb: TestFn, key: IsolateKey): TIsolateTest;
(fieldName: F, cb: TestFn, key: IsolateKey): TIsolateTest;
} & {
memo: TestMemo;
};
group: {
(callback: () => void): TIsolate;
(groupName: G, callback: () => void): TIsolate;
};
};
type TDraftCondition = DynamicValue
]>;
declare function createSuite(suiteName: SuiteName, suiteCallback: T): Suite;
declare function createSuite(suiteCallback: T): Suite;
/**
* Creates a static suite for server-side validation.
*
* @param {Function} validationFn - The validation function that defines the suite's tests.
* @returns {Function} - A function that runs the validations defined in the suite.
*
* @example
* import { staticSuite, test, enforce } from 'vest';
*
* const suite = staticSuite(data => {
* test('username', 'username is required', () => {
* enforce(data.username).isNotEmpty();
* });
* });
*
* suite(data);
*/
declare function staticSuite(suiteName: SuiteName, suiteCallback: T): StaticSuite;
declare function staticSuite(suiteCallback: T): StaticSuite;
type StaticSuite = (...args: Parameters) => StaticSuiteRunResult;
type StaticSuiteRunResult = Promise> & WithDump & TTypedMethods>;
type WithDump = T & {
dump: CB;
};
type SuiteWithDump = WithDump>;
type Suite = ((...args: Parameters) => SuiteRunResult) & SuiteMethods;
type SuiteMethods = {
dump: CB;
get: CB>;
resume: CB;
reset: CB;
remove: CB;
resetField: CB;
runStatic: CB, Parameters>;
subscribe: Subscribe;
} & TTypedMethods & SuiteSelectors;
declare function registerReconciler(reconciler: IsolateReconciler): void;
type IsolateReconciler = {
match(currentNode: TIsolate, historyNode: TIsolate): boolean;
reconcile(elecurrentNode: TIsolate, historyNode: TIsolate): TIsolate;
};
/**
* Iterates over an array of items, allowing to run tests individually per item.
*
* Requires setting a "key" property on each item tested.
*
* @example
*
* each(itemsArray, (item) => {
* test(item.name, 'Item value must not be empty', () => {
* enforce(item.value).isNotEmpty();
* }, item.id)
* })
*/
declare function each(list: T[], callback: (arg: T, index: number) => void): void;
declare function group(groupName: G, callback: CB): TIsolate;
declare function group(callback: CB): TIsolate;
/**
* Conditionally includes a field for testing, based on specified criteria.
*
* @param {string} fieldName - The name of the field to include for testing.
*
* @example
* include('confirm').when('password');
* // Includes the "confirm" field for testing when the "password" field is included
*
* include('confirm').when(someValue);
* // Includes the "confirm" field for testing when the value of `someValue` is true
*
* include('confirm').when(() => someValue);
* // Includes the "confirm" field for testing when the callback function returns true
*
* include('username').when(result => result.hasErrors('username'));
* // Includes the "username" field for testing when there are errors associated with it in the current suite result
*/
// @vx-allow use-use
declare function include(fieldName: F): {
when: (condition: F | TFieldName | TDraftCondition) => void;
};
/**
* Sets the current execution mode for the current suite.
*
* Supported modes:
* - `EAGER` - (default) Runs all tests, but stops on first failure for each given field.
* - `ALL` - Runs all tests, regardless of failures.
* - `ONE` - Stops suite execution on first failure of any field.
*
* @example
* ```js
* import {Modes, create} from 'vest';
*
* const suite = create('suite_name', () => {
* vest.mode(Modes.ALL);
*
* // ...
* });
* ```
* @param 'ALL' | 'EAGER' | 'ONE' mode - The mode to set.
*/
// @vx-allow use-use
declare function mode(mode: Modes): void;
/**
* Conditionally omits tests from the suite.
*
* @example
*
* omitWhen(res => res.hasErrors('username'), () => {
* test('username', 'User already taken', async () => await doesUserExist(username)
* });
*/
// @vx-allow use-use
declare function omitWhen(conditional: TDraftCondition, callback: CB): void;
/**
* Conditionally skips running tests within the callback.
*
* @example
*
* skipWhen(res => res.hasErrors('username'), () => {
* test('username', 'User already taken', async () => await doesUserExist(username)
* });
*/
// @vx-allow use-use
declare function skipWhen(condition: TDraftCondition, callback: CB): void;
/**
* Sets the severity level of a test to `warn`, allowing it to fail without marking the suite as invalid.
* Use this function within the body of a test to create warn-only tests.
*
* @returns {void}
*
* @example
* test('password', 'Your password strength is: WEAK', () => {
* warn();
*
* enforce(data.password).matches(/0-9/);
* });
*
* @limitations
* - The `warn` function should only be used within the body of a `test` function.
* - When using `warn()` in an async test, it should be called in the synchronous portion of the test, not after an `await` call or in the Promise body.
* - It is recommended to call `warn()` at the top of the test function.
*/
// @vx-allow use-use
declare function warn(): void;
export { createSuite as create, test, group, optional, enforce, skip, skipWhen, omitWhen, only, warn, include, suiteSelectors, each, mode, staticSuite, Modes, registerReconciler, VEST_TEST_CONTEXT };
export type { SuiteResult, SuiteRunResult, SuiteSummary, Suite, StaticSuite };
//# sourceMappingURL=vest.d.ts.map