Skip to content

Commit

Permalink
Fix experimental typings for t.try()
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Apr 4, 2020
1 parent e33b3a2 commit 81a46ac
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 3 deletions.
79 changes: 76 additions & 3 deletions experimental.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export {
CommitDiscardOptions,
Constructor,
DeepEqualAssertion,
ExecutionContext,
FailAssertion,
FalseAssertion,
FalsyAssertion,
Expand All @@ -30,11 +29,85 @@ export {
TimeoutFn,
TrueAssertion,
TruthyAssertion,
TryFn,
TryResult
} from '.';

import {ExecutionContext, ImplementationResult, MetaInterface} from '.';
import {
Assertions,
ImplementationResult,
MetaInterface,
LogFn,
PlanFn,
TimeoutFn,
TryResult
} from '.';

export interface ExecutionContext<Context = unknown> extends Assertions {
/** Test context, shared with hooks. */
context: Context;

/** Title of the test or hook. */
readonly title: string;

/** Whether the test has passed. Only accurate in afterEach hooks. */
readonly passed: boolean;

log: LogFn;
plan: PlanFn;
timeout: TimeoutFn;
try: TryFn<Context>;
}

export interface TryFn<Context = unknown> {
/**
* Attempt to run some assertions. The result must be explicitly committed or discarded or else
* the test will fail. The title may help distinguish attempts from one another.
*/
(title: string, implementation: Implementation<Context>): Promise<TryResult>;

/**
* Attempt to run some assertions. The result must be explicitly committed or discarded or else
* the test will fail. The title may help distinguish attempts from one another.
*/
<Args extends any[]> (title: string, implementation: ImplementationWithArgs<Args, Context>, ...args: Args): Promise<TryResult>;

/**
* Attempt to run some assertions. The result must be explicitly committed or discarded or else
* the test will fail. A macro may be provided. The title may help distinguish attempts from
* one another.
*/
(title: string, macro: Macro<[], Context>): Promise<TryResult>;

/**
* Attempt to run some assertions. The result must be explicitly committed or discarded or else
* the test will fail. A macro may be provided.
*/
<Args extends any[]> (title: string, macro: Macro<Args, Context>, ...args: Args): Promise<TryResult>;

/**
* Attempt to run some assertions. The result must be explicitly committed or discarded or else
* the test will fail.
*/
(implementation: Implementation<Context>): Promise<TryResult>;

/**
* Attempt to run some assertions. The result must be explicitly committed or discarded or else
* the test will fail.
*/
<Args extends any[]> (implementation: ImplementationWithArgs<Args, Context>, ...args: Args): Promise<TryResult>;

/**
* Attempt to run some assertions. The result must be explicitly committed or discarded or else
* the test will fail. A macro may be provided.
*/
(macro: Macro<[], Context>): Promise<TryResult>;

/**
* Attempt to run some assertions. The result must be explicitly committed or discarded or else
* the test will fail. A macro may be provided.
*/
<Args extends any[]> (macro: Macro<Args, Context>, ...args: Args): Promise<TryResult>;
}

export type Implementation<Context = unknown> = (t: ExecutionContext<Context>) => ImplementationResult;
export type ImplementationWithArgs<Args extends any[], Context = unknown> = (t: ExecutionContext<Context>, ...args: Args) => ImplementationResult;
Expand Down
66 changes: 66 additions & 0 deletions test-d/experimental-try-commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {expectType} from 'tsd';
import test, {ExecutionContext, Macro} from '../experimental';

test('attempt', async t => {
const attempt = await t.try(
(u, a, b) => {
expectType<ExecutionContext>(u);
expectType<string>(a);
expectType<number>(b);
},
'string',
6
);
attempt.commit();
});

test('attempt with title', async t => {
const attempt = await t.try(
'attempt title',
(u, a, b) => {
expectType<ExecutionContext>(u);
expectType<string>(a);
expectType<number>(b);
},
'string',
6
);
attempt.commit();
});

{
const lengthCheck = (t: ExecutionContext, a: string, b: number): void => {
t.is(a.length, b);
};

test('attempt with helper', async t => {
const attempt = await t.try(lengthCheck, 'string', 6);
attempt.commit();
});

test('attempt with title', async t => {
const attempt = await t.try('title', lengthCheck, 'string', 6);
attempt.commit();
});
}

test('all possible variants to pass to t.try', async t => {
// No params
t.try(tt => tt.pass());

t.try('test', tt => tt.pass());

// Some params
t.try((tt, a, b) => tt.is(a.length, b), 'hello', 5);

t.try('test', (tt, a, b) => tt.is(a.length, b), 'hello', 5);

// Macro with title
const macro1 = test.macro({
exec: (tt, a, b) => tt.is(a.length, b),
title: (title, a, b) => `${title ? `${String(title)} ` : ''}str: "${String(a)}" with len: "${String(b)}"`
});

t.try(macro1, 'hello', 5);
t.try('title', macro1, 'hello', 5);
});

0 comments on commit 81a46ac

Please sign in to comment.