Skip to content

Instantly share code, notes, and snippets.

@DanielRosenwasser
Created August 16, 2016 19:25
Show Gist options
  • Save DanielRosenwasser/c2a916b53c6fc0b214f936eac95fd4e9 to your computer and use it in GitHub Desktop.
Save DanielRosenwasser/c2a916b53c6fc0b214f936eac95fd4e9 to your computer and use it in GitHub Desktop.
declare function ava( run: ava.ContextualTest): void;
declare function ava(name: string, run: ava.ContextualTest): void;
declare function ava( run: ava.Macros<ava.ContextualTestContext>, ...args: Array<any>): void;
declare function ava(name: string, run: ava.Macros<ava.ContextualTestContext>, ...args: Array<any>): void;
declare namespace ava {
/**
* Misc Setup Types
*/
export type PromiseLike<R> = {
then<U>(
onFulfill?: (value: R) => Promise<U> | U,
onReject?: (error: any) => Promise<U> | U
): Promise<U>;
}
export interface ObservableLike {
subscribe(observer: (value: {}) => void): void;
};
export type SpecialReturnTypes = PromiseLike<any>
| Iterator<any>
| ObservableLike;
export interface Constructor {
constructor(...args: Array<any>): any;
}
export type ErrorValidator = Constructor
| RegExp
| string
| ((error: any) => boolean);
/**
* Asertion Types
*/
export interface AssertContext {
// Passing assertion.
pass(message?: string): void;
// Failing assertion.
fail(message?: string): void;
// Assert that value is truthy.
truthy(value: any, message?: string): void;
// Assert that value is falsy.
falsy(value: any, message?: string): void;
// DEPRECATED, use `truthy`. Assert that value is truthy.
ok(value: any, message?: string): void;
// DEPRECATED, use `falsy`. Assert that value is falsy.
notOk(value: any, message?: string): void;
// Assert that value is true.
true(value: boolean, message?: string): void;
// Assert that value is false.
false(value: boolean, message?: string): void;
// Assert that value is equal to expected.
is<U>(value: U, expected: U, message?: string): void;
// Assert that value is not equal to expected.
not<U>(value: U, expected: U, message?: string): void;
// Assert that value is deep equal to expected.
deepEqual<U>(value: U, expected: U, message?: string): void;
// Assert that value is not deep equal to expected.
notDeepEqual<U>(value: U, expected: U, message?: string): void;
// Assert that function throws an error or promise rejects.
// DEPRECATED, use `deepEqual`. Assert that value is deep equal to expected.
// @param error Can be a constructor, regex, error message or validation function.
same<U>(value: U, expected: U, message?: string): void;
// DEPRECATED use `notDeepEqual`. Assert that value is not deep equal to expected.
notSame<U>(value: U, expected: U, message?: string): void;
// Assert that function throws an error or promise rejects.
// @param error Can be a constructor, regex, error message or validation function.
throws(value: PromiseLike<any>, error?: ErrorValidator, message?: string): Promise<any>;
throws(value: () => void, error?: ErrorValidator, message?: string): any;
// Assert that function doesn't throw an error or promise resolves.
notThrows<U>(value: PromiseLike<U>, message?: string): Promise<U>;
notThrows(value: () => void, message?: string): void;
// Assert that contents matches regex.
regex(contents: string, regex: RegExp, message?: string): void;
// Assert that contents does not match regex.
notRegex(contents: string, regex: RegExp, message?: string): void;
// Assert that error is falsy.
ifError(error: any, message?: string): void;
};
/**
* Context Types
*/
export interface TestContext extends AssertContext {
plan(count: number): void;
skip: AssertContext;
};
export interface CallbackTestContext extends TestContext { end(): void; };
export interface ContextualTestContext extends TestContext { context: any; };
export interface ContextualCallbackTestContext extends CallbackTestContext { context: any; };
/**
* Test Types
*/
export type Test = (t: TestContext) => SpecialReturnTypes | void;
export type CallbackTest = (t: CallbackTestContext) => void;
export type ContextualTest = (t: ContextualTestContext) => SpecialReturnTypes | void;
export type ContextualCallbackTest = (t: ContextualCallbackTestContext) => void;
/**
* Macro Types
*/
export interface Macro<T> {
(t: T, ...args: Array<any>): void;
title?: (providedTitle: string, ...args: Array<any>) => string;
};
export type Macros<T> = Macro<T> | Array<Macro<T>>;
/**
* Method Types
*/
export interface Method<TestType, TestContextType> {
( implementation: TestType): void;
(name: string, implementation: TestType): void;
( implementation: Macros<TestContextType>, ...args: Array<any>): void;
(name: string, implementation: Macros<TestContextType>, ...args: Array<any>): void;
}
export type TestMethod = Method<Test, TestContext>;
export type CallbackTestMethod = Method<CallbackTest, CallbackTestContext>;
export type ContextualTestMethod = Method<ContextualTest, ContextualTestContext>;
export type ContextualCallbackTestMethod = Method<ContextualCallbackTest, ContextualCallbackTestContext>;
/**
* Chain Types
*/
export type Chain<T, TCallback> = T & {
serial : Chain<T, TCallback>;
before : Chain<T, TCallback>;
after : Chain<T, TCallback>;
skip : Chain<T, TCallback>;
todo : Chain<T, TCallback>;
failing : Chain<T, TCallback>;
only : Chain<T, TCallback>;
beforeEach : Chain<T, TCallback>;
afterEach : Chain<T, TCallback>;
cb : Chain<TCallback, TCallback>;
always : Chain<T, TCallback>;
};
export type TestChain = Chain<TestMethod, CallbackTestMethod>;
export type ContextualTestChain = Chain<ContextualTestMethod, ContextualCallbackTestMethod>
export type ContextualCallbackTestChain = Chain<ContextualCallbackTestMethod, ContextualCallbackTestMethod>;
/**
* Public API
*/
export var beforeEach : TestChain;
export var afterEach : TestChain;
export var serial : ContextualTestChain;
export var before : ContextualTestChain;
export var after : ContextualTestChain;
export var skip : ContextualTestChain;
export var todo : ContextualTestChain;
export var failing : ContextualTestChain;
export var only : ContextualTestChain;
export var cb : ContextualCallbackTestChain;
export var always : ContextualTestChain;
}
export = ava;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment