UNPKG

micro-test-runner

Version:
65 lines (64 loc) 2.68 kB
type Candidate<Args extends unknown[], Return = unknown> = (...args: Args) => Promise<Return> | Return; type FailureSeverity = 'log' | 'warn' | 'error'; type PerformanceLogFormat = 'none' | 'average' | 'table'; type Validator<Return = unknown> = Return | ((result: Return, run: number, duration: number) => boolean); declare class MicroTestRunner<Args extends unknown[], Return = unknown> { private candidate; private log; private candidateContext; private args; private conditions; private runs; private currentRun; private performance; private passing; private result; constructor(candidate: Candidate<Args, Return>); private get logMessage(); /** * Log the test result with the appropriate severity. */ private logResult; /** * Test an expression or function. * @param candidate The expression/function to test. * @returns The test-runner. */ static test<Args extends unknown[], Return = unknown>(candidate: Candidate<Args, Return>): MicroTestRunner<Args, Return>; /** * Automatically log the outcome of the test. * @param name The name of the test. * @param severity `(Optional)` The severity used to log the test's failure. Either `'log'`, `'warn'`, or `'error'`. * @param icons `(Optional)` Icons used to visually indicate the outcome of the test. * Default: `['✓', '✕']`. * @param performance `(Optional)` Log the performance of each test run in the desired format: * * • `'average'` - The average of all runs. * * • `'table'` - A table showing the performance of each run. */ logging(name: string, severity?: FailureSeverity, icons?: [string, string], performance?: PerformanceLogFormat): MicroTestRunner<Args, Return>; /** * Run the candidate function within a given context. Useful if the candidate needs access to a particular `this`. * @param context The context. */ context(context: any): MicroTestRunner<Args, Return>; /** * Run each test multiple times. * @param number The number of times to run each test. */ times(number: number): MicroTestRunner<Args, Return>; /** * Provide arguments to the candidate. * @param args The arguments. */ with(...args: Args): MicroTestRunner<Args, Return>; /** * The results to expect from each test. * @param conditions The expected results. * @returns `true` if all test runs passed, `false` if any run failed. */ expect(...conditions: Validator<Awaited<Return>>[]): Promise<boolean>; } declare const _default: typeof MicroTestRunner.test; export default _default;