Skip to content

Commit 6715962

Browse files
committed
Type experimental macros
1 parent 8a8a575 commit 6715962

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

experimental.d.ts

+40
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,35 @@ import {ExecutionContext, ImplementationResult, MetaInterface} from '.';
3939
export type Implementation<Context = unknown> = (t: ExecutionContext<Context>) => ImplementationResult;
4040
export type ImplementationWithArgs<Args extends any[], Context = unknown> = (t: ExecutionContext<Context>, ...args: Args) => ImplementationResult;
4141

42+
export type Macro<Args extends any[] = [], Context = unknown> = {
43+
exec (t: ExecutionContext<Context>, ...args: Args): ImplementationResult;
44+
title? (providedTitle?: string, ...args: Args): string;
45+
};
46+
47+
export interface MacroInterface<InheritedContext = unknown> {
48+
<Args extends any[] = [], Context = InheritedContext> (implementation: ImplementationWithArgs<Args, Context>): Macro<Args, Context>;
49+
<Args extends any[] = [], Context = InheritedContext> (macro: Macro<Args, Context>): Macro<Args, Context>;
50+
}
51+
4252
export interface TestInterface<Context = unknown> {
4353
/** Declare a concurrent test. */
4454
(title: string, implementation: Implementation<Context>): void;
4555

4656
/** Declare a concurrent test. */
4757
<Args extends any[]> (title: string, implementation: ImplementationWithArgs<Args, Context>, ...args: Args): void;
4858

59+
/** Declare a concurrent test. */
60+
(title: string, macro: Macro<[], Context>): void;
61+
62+
/** Declare a concurrent test. */
63+
<Args extends any[]> (title: string, macro: Macro<Args, Context>, ...args: Args): void;
64+
65+
/** Declare a concurrent test. */
66+
(macro: Macro<[], Context>): void;
67+
68+
/** Declare a concurrent test. */
69+
<Args extends any[]> (macro: Macro<Args, Context>, ...args: Args): void;
70+
4971
/** Declare a hook that is run once, after all tests have passed. */
5072
after: AfterInterface<Context>;
5173

@@ -58,6 +80,9 @@ export interface TestInterface<Context = unknown> {
5880
/** Declare a hook that is run before each test. */
5981
beforeEach: BeforeInterface<Context>;
6082

83+
/** Create a macro you can reuse in multiple tests. */
84+
macro: MacroInterface<Context>;
85+
6186
/** Declare a test that is expected to fail. */
6287
failing: FailingInterface<Context>;
6388

@@ -161,6 +186,18 @@ export interface SerialInterface<Context = unknown> {
161186
/** Declare a serial test. */
162187
<Args extends any[]> (title: string, implementation: ImplementationWithArgs<Args, Context>, ...args: Args): void;
163188

189+
/** Declare a serial test. */
190+
(title: string, macro: Macro<[], Context>): void;
191+
192+
/** Declare a serial test. */
193+
<Args extends any[]> (title: string, macro: Macro<Args, Context>, ...args: Args): void;
194+
195+
/** Declare a serial test. */
196+
(macro: Macro<[], Context>): void;
197+
198+
/** Declare a serial test. */
199+
<Args extends any[]> (macro: Macro<Args, Context>, ...args: Args): void;
200+
164201
/** Declare a serial hook that is run once, after all tests have passed. */
165202
after: AfterInterface<Context>;
166203

@@ -173,6 +210,9 @@ export interface SerialInterface<Context = unknown> {
173210
/** Declare a serial hook that is run before each test. */
174211
beforeEach: BeforeInterface<Context>;
175212

213+
/** Create a macro you can reuse in multiple tests. */
214+
macro: MacroInterface<Context>;
215+
176216
/** Declare a serial test that is expected to fail. */
177217
failing: FailingInterface<Context>;
178218

test-d/experimental-macro.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import {expectError} from 'tsd';
2+
import test, {TestInterface} from '../experimental';
3+
4+
{
5+
const macro = test.macro(t => t.pass());
6+
test(macro);
7+
test('title', macro);
8+
test.serial(macro);
9+
test.serial('title', macro);
10+
expectError(test(macro, 'foo'));
11+
expectError(test('title', macro, 'foo'));
12+
expectError(test.serial(macro, 'foo'));
13+
expectError(test.serial('title', macro, 'foo'));
14+
}
15+
16+
{
17+
const macro = test.serial.macro(t => t.pass());
18+
test(macro);
19+
test.serial(macro);
20+
}
21+
22+
{
23+
const macro = test.macro<[string]>((t, string) => t.is(string, 'foo'));
24+
test(macro, 'foo');
25+
test('title', macro, 'foo');
26+
test.serial(macro, 'foo');
27+
test.serial('title', macro, 'foo');
28+
expectError(test(macro));
29+
expectError(test('title', macro));
30+
expectError(test.serial(macro));
31+
expectError(test.serial('title', macro));
32+
}
33+
34+
{
35+
const macro = test.macro<[string]>({
36+
exec: (t, string) => t.is(string, 'foo')
37+
});
38+
test(macro, 'foo');
39+
test('title', macro, 'foo');
40+
test.serial(macro, 'foo');
41+
test.serial('title', macro, 'foo');
42+
expectError(test.serial(macro));
43+
expectError(test.serial('title', macro));
44+
}
45+
46+
{
47+
const macro = test.macro<[string]>({
48+
exec: (t, string) => t.is(string, 'foo'),
49+
title: (prefix, string) => `${prefix ?? 'title'} ${string}`
50+
});
51+
test(macro, 'foo');
52+
test('title', macro, 'foo');
53+
test.serial(macro, 'foo');
54+
test.serial('title', macro, 'foo');
55+
expectError(test(macro));
56+
expectError(test('title', macro));
57+
expectError(test.serial(macro));
58+
expectError(test.serial('title', macro));
59+
}
60+
61+
test.serial.macro<[], { str: string }>(t => t.is(t.context.str, 'foo'));
62+
test.serial.macro<[string], { str: string }>((t, string) => t.is(t.context.str, string));
63+
(test as TestInterface<{ str: string }>).macro(t => t.is(t.context.str, 'foo'));
64+
(test as TestInterface<{ str: string }>).macro<[string]>((t, string) => t.is(t.context.str, string));
65+
66+
{
67+
const macro = test.macro<[], { foo: string }>(t => t.is(t.context.foo, 'foo'));
68+
// ;(test as TestInterface<{foo: string, bar: string}>)(macro)
69+
expectError((test as TestInterface<{bar: string}>)(macro));
70+
}
71+
72+
{
73+
const macro = test.macro(t => t.pass());
74+
expectError(test.before(macro));
75+
expectError(test.beforeEach(macro));
76+
expectError(test.after(macro));
77+
expectError(test.after.always(macro));
78+
expectError(test.afterEach(macro));
79+
expectError(test.afterEach.always(macro));
80+
}

0 commit comments

Comments
 (0)