UNPKG

ebdd

Version:

Extended BDD interface for Mocha

107 lines (106 loc) 5.62 kB
import ExtensibleArray from './extensible-array'; import type { Context, Done, HookFunction, MochaOptions, Suite, Test, interfaces } from 'mocha'; declare global { interface Mocha { constructor: { readonly Suite: typeof Suite; readonly Test: typeof Test; readonly interfaces: typeof interfaces; new (options?: MochaOptions): Mocha; }; } namespace Mocha { interface ExclusiveSuiteFunction extends UnparameterizedSuiteFunction { } interface ExclusiveTestFunction extends UnparameterizedTestFunction { } interface InterfaceContributions { ebdd: never; } interface MochaGlobals extends EBDDGlobals { } interface PendingSuiteFunction extends UnparameterizedSuiteFunction { } interface PendingTestFunction extends UnparameterizedTestFunction { } interface SuiteFunction extends UnparameterizedSuiteFunction { } interface TestFunction extends UnparameterizedTestFunction { } namespace interfaces { function ebdd(suite: Suite): void; } } const only: <ParamType>(param: ParamType) => ParamInfo<ParamType>; const skip: <ParamType>(param: ParamType) => ParamInfo<ParamType>; const when: <ParamType>(condition: boolean, param: ParamType) => ParamInfo<ParamType>; } export interface EBDDGlobals { after: HookFunction; afterEach: HookFunction; before: HookFunction; beforeEach: HookFunction; context: UnparameterizedSuiteFunction; describe: UnparameterizedSuiteFunction; it: UnparameterizedTestFunction; only: <ParamType>(param: ParamType) => ParamInfo<ParamType>; run?: () => void; skip: <ParamType>(param: ParamType) => ParamInfo<ParamType>; specify: UnparameterizedTestFunction; when: <ParamType>(condition: boolean, param: ParamType) => ParamInfo<ParamType>; xcontext: UnparameterizedSuiteFunction; xdescribe: UnparameterizedSuiteFunction; xit: UnparameterizedTestFunction; xspecify: UnparameterizedTestFunction; } declare enum Mode { NORMAL = 0, ONLY = 1, SKIP = 2 } export type ParamCollection<ParamType> = ArrayLike<ParamOrParamInfo<ParamType>>; export type ParamMapper<InParamType, OutParamType> = (param: InParamType) => ParamOrParamInfo<OutParamType>; export type ParamOrParamInfo<ParamType> = ParamType | ParamInfo<ParamType>; interface ParameterizableFunction<SubType extends ParameterizableFunction<SubType>> { readonly only: SubType; readonly skip: SubType; readonly when: (condition: boolean) => SubType; per<ParamType>(params: ParamCollection<ParamType>): unknown; per<InParamType, OutParamType>(params: ParamCollection<InParamType>, paramMapper: ParamMapper<InParamType, OutParamType>): unknown; } export interface ParameterizedSuiteFunction<ParamListType extends unknown[]> extends ParameterizableFunction<ParameterizedSuiteFunction<ParamListType>> { (titlePattern: string, fn: SuiteCallback<ParamListType>): SpecItemArray<Suite>; per<ParamType>(params: ParamCollection<ParamType>): ParameterizedSuiteFunction<[...ParamListType, ParamType]>; per<InParamType, OutParamType>(params: ParamCollection<InParamType>, paramMapper: ParamMapper<InParamType, OutParamType>): ParameterizedSuiteFunction<[...ParamListType, OutParamType]>; } export interface ParameterizedTestFunction<ParamListType extends unknown[]> extends ParameterizableFunction<ParameterizedTestFunction<ParamListType>> { (titlePattern: string, fn: TestCallback<ParamListType>): SpecItemArray<Test>; per<ParamType>(params: ParamCollection<ParamType>): ParameterizedTestFunction<[...ParamListType, ParamType]>; per<InParamType, OutParamType>(params: ParamCollection<InParamType>, paramMapper: ParamMapper<InParamType, OutParamType>): ParameterizedTestFunction<[...ParamListType, OutParamType]>; } /** Callback function used for suites. */ type SuiteCallback<ParamListType extends unknown[] = []> = (this: Suite, ...params: ParamListType) => void; /** Callback function used for tests and hooks. */ type TestCallback<ParamListType extends unknown[] = []> = ((this: Context, ...params: ParamListType) => PromiseLike<any>) | ((this: Context, ...paramsAndDone: [...ParamListType, Done]) => void); export interface UnparameterizedSuiteFunction extends ParameterizableFunction<UnparameterizedSuiteFunction> { (title: string, fn: SuiteCallback): Suite; per<ParamType>(params: ParamCollection<ParamType>): ParameterizedSuiteFunction<[ParamType]>; per<InParamType, OutParamType>(params: ParamCollection<InParamType>, paramMapper: ParamMapper<InParamType, OutParamType>): ParameterizedSuiteFunction<[OutParamType]>; } export interface UnparameterizedTestFunction extends ParameterizableFunction<UnparameterizedTestFunction> { (title: string, fn: TestCallback): Test; per<ParamType>(params: ParamCollection<ParamType>): ParameterizedTestFunction<[ParamType]>; per<InParamType, OutParamType>(params: ParamCollection<InParamType>, paramMapper: ParamMapper<InParamType, OutParamType>): ParameterizedTestFunction<[OutParamType]>; } export type { ParamInfo, SpecItemArray }; declare class ParamInfo<ParamType> { readonly param: ParamType; readonly mode: Mode; constructor(param: ParamType, mode: Mode); } declare class SpecItemArray<SpecItemType extends Suite | Test> extends ExtensibleArray<SpecItemType> { parent: Suite | undefined; timeout(): number; timeout(ms: string | number): this; } export default function ebdd(suite: Suite): void;