/
try-commit.cts
67 lines (56 loc) 路 1.6 KB
/
try-commit.cts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import test, {ExecutionContext} from 'ava';
import {expectType} from 'tsd';
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
void t.try(tt => tt.pass());
void t.try('test', tt => tt.pass());
// Some params
void t.try((tt, a, b) => tt.is(a.length, b), 'hello', 5);
void t.try('test', (tt, a, b) => tt.is(a.length, b), 'hello', 5);
// Macro with title
const macro1 = test.macro<[string, number]>({
exec: (tt, a, b) => tt.is(a.length, b),
title: (title, a, b) => `${title ? `${String(title)} ` : ''}str: "${String(a)}" with len: "${String(b)}"`,
});
const macro2 = test.macro<[string, number]>((tt, a, b) => tt.is(a.slice(b), ''));
void t.try(macro1, 'hello', 5);
void t.try(macro2, 'hello', 5);
void t.try('title', macro1, 'hello', 5);
void t.try('title', macro2, 'hello', 5);
});