/
helpers.ts
156 lines (143 loc) · 4.25 KB
/
helpers.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import * as promisify from 'util.promisify';
import { PassThrough } from 'stream';
import { getStream, TEST_DIR, tsNodeTypes } from '../helpers';
import type { ExecutionContext } from 'ava';
import { expect, TestInterface } from '../testlib';
export interface ContextWithTsNodeUnderTest {
tsNodeUnderTest: Pick<
typeof tsNodeTypes,
'create' | 'register' | 'createRepl'
>;
}
export type ContextWithReplHelpers = ContextWithTsNodeUnderTest &
Awaited<ReturnType<typeof contextReplHelpers>>;
export interface CreateReplViaApiOptions {
registerHooks: boolean;
createReplOpts?: Partial<tsNodeTypes.CreateReplOptions>;
createServiceOpts?: Partial<tsNodeTypes.CreateOptions>;
}
export interface ExecuteInReplOptions extends CreateReplViaApiOptions {
waitMs?: number;
waitPattern?: string | RegExp;
/** When specified, calls `startInternal` instead of `start` and passes options */
startInternalOptions?: Parameters<
tsNodeTypes.ReplService['startInternal']
>[0];
}
/**
* pass to test.context() to get REPL testing helper functions
*/
export async function contextReplHelpers(
t: ExecutionContext<ContextWithTsNodeUnderTest>
) {
const { tsNodeUnderTest } = t.context;
return { createReplViaApi, executeInRepl };
function createReplViaApi({
registerHooks,
createReplOpts,
createServiceOpts,
}: CreateReplViaApiOptions) {
const stdin = new PassThrough();
const stdout = new PassThrough();
const stderr = new PassThrough();
const replService = tsNodeUnderTest.createRepl({
stdin,
stdout,
stderr,
...createReplOpts,
});
const service = (
registerHooks ? tsNodeUnderTest.register : tsNodeUnderTest.create
)({
...replService.evalAwarePartialHost,
project: `${TEST_DIR}/tsconfig.json`,
...createServiceOpts,
tsTrace: replService.console.log.bind(replService.console),
});
replService.setService(service);
t.teardown(async () => {
service.enabled(false);
});
return { stdin, stdout, stderr, replService, service };
}
async function executeInRepl(input: string, options: ExecuteInReplOptions) {
const {
waitPattern,
// Wait longer if there's a signal to end it early
waitMs = waitPattern != null ? 20e3 : 1e3,
startInternalOptions,
...rest
} = options;
const { stdin, stdout, stderr, replService } = createReplViaApi(rest);
if (startInternalOptions) {
replService.startInternal(startInternalOptions);
} else {
replService.start();
}
stdin.write(input);
stdin.end();
const stdoutPromise = getStream(stdout, waitPattern);
const stderrPromise = getStream(stderr, waitPattern);
// Wait for expected output pattern or timeout, whichever comes first
await Promise.race([
promisify(setTimeout)(waitMs),
stdoutPromise,
stderrPromise,
]);
stdout.end();
stderr.end();
return {
stdin,
stdout: await stdoutPromise,
stderr: await stderrPromise,
};
}
}
export function replMacros<T extends ContextWithReplHelpers>(
_test: TestInterface<T>
) {
return { noErrorsAndStdoutContains, stderrContains };
function noErrorsAndStdoutContains(
title: string,
script: string,
contains: string,
options?: Partial<ExecuteInReplOptions>
) {
testReplInternal(title, script, contains, undefined, contains, options);
}
function stderrContains(
title: string,
script: string,
errorContains: string,
options?: Partial<ExecuteInReplOptions>
) {
testReplInternal(
title,
script,
undefined,
errorContains,
errorContains,
options
);
}
function testReplInternal(
title: string,
script: string,
stdoutContains: string | undefined,
stderrContains: string | undefined,
waitPattern: string,
options?: Partial<ExecuteInReplOptions>
) {
_test(title, async (t) => {
const { stdout, stderr } = await t.context.executeInRepl(script, {
registerHooks: true,
startInternalOptions: { useGlobal: false },
waitPattern,
...options,
});
if (stderrContains) expect(stderr).toContain(stderrContains);
else expect(stderr).toBe('');
if (stdoutContains) expect(stdout).toContain(stdoutContains);
});
}
}