Skip to content

Make assertions return Booleans #2601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 143 additions & 51 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,52 +39,90 @@ export type SnapshotOptions = {
};

export interface Assertions {
/** Assert that `actual` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). Comes with power-assert. */
/**
* Assert that `actual` is
* [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy),
* returning Boolean indicating whether the assertion passes. Comes with
* power-assert.
*/
assert: AssertAssertion;

/** Assert that `actual` is [deeply equal](https://github.com/concordancejs/concordance#comparison-details) to `expected`. */
/**
* Assert that `actual` is [deeply
* equal](https://github.com/concordancejs/concordance#comparison-details) to
* `expected`, returning Boolean indicating whether the assertion passes.
*/
deepEqual: DeepEqualAssertion;

/** Assert that `actual` is like `expected`. */
/**
* Assert that `value` is like `selector`, returning Boolean indicating
* whether the assertion passes.
*/
like: LikeAssertion;

/** Fail the test. */
/** Fail the test, always returning `false`. */
fail: FailAssertion;

/** Assert that `actual` is strictly false. */
/**
* Assert that `actual` is strictly false, returning Boolean indicating
* whether the assertion passes.
*/
false: FalseAssertion;

/** Assert that `actual` is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). */
/**
* Assert that `actual` is
* [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), returning
* Boolean whether the assertion passes.
*/
falsy: FalsyAssertion;

/**
* Assert that `actual` is [the same
* value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) as `expected`.
* value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)
* as `expected`, returning Boolean indicating whether the assertion passes.
*/
is: IsAssertion;

/**
* Assert that `actual` is not [the same
* value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) as `expected`.
* value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)
* as `expected`, returning Boolean indicating whether the assertion passes.
*/
not: NotAssertion;

/** Assert that `actual` is not [deeply equal](https://github.com/concordancejs/concordance#comparison-details) to `expected`. */
/**
* Assert that `actual` is not [deeply
* equal](https://github.com/concordancejs/concordance#comparison-details) to
* `expected`, returning Boolean indicating whether the assertion passes.
*/
notDeepEqual: NotDeepEqualAssertion;

/** Assert that `string` does not match the regular expression. */
/**
* Assert that `string` does not match the regular expression, returning
* Boolean indicating whether the assertion passes.
*/
notRegex: NotRegexAssertion;

/** Assert that the function does not throw. */
/**
* Assert that the function does not throw, returning Boolean indicating
* whether the assertion passes.
*/
notThrows: NotThrowsAssertion;

/** Assert that the async function does not throw, or that the promise does not reject. Must be awaited. */
/**
* Assert that the async function does not throw, or that the promise does
* not reject, returning Boolean indicating whether the assertion passes. Must
* be awaited.
*/
notThrowsAsync: NotThrowsAsyncAssertion;

/** Count a passing assertion. */
/** Count a passing assertion, always returning `true`. */
pass: PassAssertion;

/** Assert that `string` matches the regular expression. */
/**
* Assert that `string` matches the regular expression, returning Boolean
* indicating whether the assertion passes.
*/
regex: RegexAssertion;

/**
Expand All @@ -105,56 +143,82 @@ export interface Assertions {
*/
throwsAsync: ThrowsAsyncAssertion;

/** Assert that `actual` is strictly true. */
/**
* Assert that `actual` is strictly true, returning Boolean indicating
* whether the assertion passes.
*/
true: TrueAssertion;

/** Assert that `actual` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). */
/**
* Assert that `actual` is
* [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy),
* returning Boolean indicating whether the assertion passes.
*/
truthy: TruthyAssertion;
}

export interface AssertAssertion {
/** Assert that `actual` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). Comes with power-assert. */
(actual: any, message?: string): void;
/**
* Assert that `actual` is
* [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy),
* returning Boolean indicating whether the assertion passes. Comes with
* power-assert.
*/
(actual: any, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, message?: string): void;
}

export interface DeepEqualAssertion {
/** Assert that `actual` is [deeply equal](https://github.com/concordancejs/concordance#comparison-details) to `expected`. */
<ValueType = any>(actual: ValueType, expected: ValueType, message?: string): void;
/**
* Assert that `actual` is [deeply
* equal](https://github.com/concordancejs/concordance#comparison-details) to
* `expected`, returning Boolean indicating whether the assertion passes.
*/
<ValueType = any>(actual: ValueType, expected: ValueType, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, expected: any, message?: string): void;
}

export interface LikeAssertion {
/** Assert that `value` is like `selector`. */
(value: any, selector: Record<string, any>, message?: string): void;
/**
* Assert that `value` is like `selector`, returning Boolean indicating
* whether the assertion passes.
*/
(value: any, selector: Record<string, any>, message?: string): boolean;

/** Skip this assertion. */
skip(value: any, selector: any, message?: string): void;
}

export interface FailAssertion {
/** Fail the test. */
(message?: string): void;
/** Fail the test, always returning `false`. */
(message?: string): false;

/** Skip this assertion. */
skip(message?: string): void;
}

export interface FalseAssertion {
/** Assert that `actual` is strictly false. */
(actual: any, message?: string): void;
/**
* Assert that `actual` is strictly false, returning Boolean indicating
* whether the assertion passes.
*/
(actual: any, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, message?: string): void;
}

export interface FalsyAssertion {
/** Assert that `actual` is [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy). */
(actual: any, message?: string): void;
/**
* Assert that `actual` is
* [falsy](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), returning
* Boolean whether the assertion passes.
*/
(actual: any, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, message?: string): void;
Expand All @@ -163,9 +227,10 @@ export interface FalsyAssertion {
export interface IsAssertion {
/**
* Assert that `actual` is [the same
* value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) as `expected`.
* value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)
* as `expected`, returning Boolean indicating whether the assertion passes.
*/
<ValueType = any>(actual: ValueType, expected: ValueType, message?: string): void;
<ValueType = any>(actual: ValueType, expected: ValueType, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, expected: any, message?: string): void;
Expand All @@ -174,60 +239,80 @@ export interface IsAssertion {
export interface NotAssertion {
/**
* Assert that `actual` is not [the same
* value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) as `expected`.
* value](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is)
* as `expected`, returning Boolean indicating whether the assertion passes.
*/
<ValueType = any>(actual: ValueType, expected: ValueType, message?: string): void;
<ValueType = any>(actual: ValueType, expected: ValueType, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, expected: any, message?: string): void;
}

export interface NotDeepEqualAssertion {
/** Assert that `actual` is not [deeply equal](https://github.com/concordancejs/concordance#comparison-details) to `expected`. */
<ValueType = any>(actual: ValueType, expected: ValueType, message?: string): void;
/**
* Assert that `actual` is not [deeply
* equal](https://github.com/concordancejs/concordance#comparison-details) to
* `expected`, returning Boolean indicating whether the assertion passes.
*/
<ValueType = any>(actual: ValueType, expected: ValueType, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, expected: any, message?: string): void;
}

export interface NotRegexAssertion {
/** Assert that `string` does not match the regular expression. */
(string: string, regex: RegExp, message?: string): void;
/**
* Assert that `string` does not match the regular expression, returning
* Boolean indicating whether the assertion passes.
*/
(string: string, regex: RegExp, message?: string): boolean;

/** Skip this assertion. */
skip(string: string, regex: RegExp, message?: string): void;
}

export interface NotThrowsAssertion {
/** Assert that the function does not throw. */
(fn: () => any, message?: string): void;
/**
* Assert that the function does not throw, returning Boolean indicating
* whether the assertion passes.
*/
(fn: () => any, message?: string): boolean;

/** Skip this assertion. */
skip(fn: () => any, message?: string): void;
}

export interface NotThrowsAsyncAssertion {
/** Assert that the async function does not throw. You must await the result. */
(fn: () => PromiseLike<any>, message?: string): Promise<void>;
/**
* Assert that the async function does not throw, returning Boolean
* indicating whether the assertion passes. You must await the result.
*/
(fn: () => PromiseLike<any>, message?: string): Promise<boolean>;

/** Assert that the promise does not reject. You must await the result. */
(promise: PromiseLike<any>, message?: string): Promise<void>;
/**
* Assert that the promise does not reject, returning Boolean indicating
* whether the assertion passes. You must await the result.
*/
(promise: PromiseLike<any>, message?: string): Promise<boolean>;

/** Skip this assertion. */
skip(nonThrower: any, message?: string): void;
}

export interface PassAssertion {
/** Count a passing assertion. */
(message?: string): void;
/** Count a passing assertion, always returning `true`. */
(message?: string): true;

/** Skip this assertion. */
skip(message?: string): void;
}

export interface RegexAssertion {
/** Assert that `string` matches the regular expression. */
(string: string, regex: RegExp, message?: string): void;
/**
* Assert that `string` matches the regular expression, returning Boolean
* indicating whether the assertion passes.
*/
(string: string, regex: RegExp, message?: string): boolean;

/** Skip this assertion. */
skip(string: string, regex: RegExp, message?: string): void;
Expand Down Expand Up @@ -296,16 +381,23 @@ export interface ThrowsAsyncAssertion {
}

export interface TrueAssertion {
/** Assert that `actual` is strictly true. */
(actual: any, message?: string): void;
/**
* Assert that `actual` is strictly true, returning Boolean indicating
* whether the assertion passes.
*/
(actual: any, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, message?: string): void;
}

export interface TruthyAssertion {
/** Assert that `actual` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy). */
(actual: any, message?: string): void;
/**
* Assert that `actual` is
* [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy),
* returning Boolean indicating whether the assertion passes.
*/
(actual: any, message?: string): boolean;

/** Skip this assertion. */
skip(actual: any, message?: string): void;
Expand Down Expand Up @@ -433,7 +525,7 @@ export interface CbExecutionContext<Context = unknown> extends ExecutionContext<
end(error?: any): void;
}

export type ImplementationResult = PromiseLike<void> | Subscribable | void;
export type ImplementationResult = PromiseLike<void> | Subscribable | boolean | void;
export type Implementation<Context = unknown> = (t: ExecutionContext<Context>) => ImplementationResult;
export type CbImplementation<Context = unknown> = (t: CbExecutionContext<Context>) => ImplementationResult;

Expand Down
Loading