-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathe2e.test.ts
More file actions
377 lines (301 loc) · 10.5 KB
/
e2e.test.ts
File metadata and controls
377 lines (301 loc) · 10.5 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { test, expect, Shell } from "@microsoft/tui-test";
import os from "node:os";
const shell =
os.platform() == "darwin"
? Shell.Zsh
: os.platform() == "linux"
? Shell.Bash
: Shell.Powershell;
test.use({ shell: shell, columns: 80, rows: 15 });
const isWindows = os.platform() == "win32";
const isNotMacOS = os.platform() != "darwin";
const isLinux = os.platform() == "linux";
const windowsShells = [
Shell.Cmd,
Shell.Powershell,
Shell.WindowsPowershell,
Shell.Xonsh,
];
const unixShells = [Shell.Bash, Shell.Fish, Shell.Zsh];
test.describe("locators", () => {
test.describe("getByText", () => {
test("string driven", async ({ terminal }) => {
await expect(terminal.getByText(">")).toBeVisible();
});
test("regex driven", async ({ terminal }) => {
await expect(terminal.getByText(/>/g)).toBeVisible();
});
test.fail("regex driven non-global regex", async ({ terminal }) => {
await expect(terminal.getByText(/>/m)).toBeVisible();
});
});
});
test.describe("key controls", () => {
test("left key", async ({ terminal }) => {
terminal.write("bar");
terminal.keyLeft(3);
terminal.write("foo");
await expect(terminal.getByText("foobar")).toBeVisible();
expect(terminal.getCursor().x).toBe(5);
});
test("right key", async ({ terminal }) => {
terminal.write("bar");
terminal.keyLeft(3);
terminal.write("foo");
terminal.keyRight(3);
terminal.write("baz");
await expect(terminal.getByText("foobarbaz")).toBeVisible();
expect(terminal.getCursor().x).toBe(11);
});
test("up arrow", async ({ terminal }) => {
terminal.write("clear");
await expect(terminal.getByText("clear")).toBeVisible();
terminal.submit();
await expect(terminal.getByText("clear")).not.toBeVisible();
terminal.keyUp();
await expect(terminal.getByText("clear")).toBeVisible();
expect(terminal.getCursor().x).toBe(7);
});
test("down arrow", async ({ terminal }) => {
terminal.write("clear");
await expect(terminal.getByText("clear")).toBeVisible();
terminal.submit();
await expect(terminal.getByText("clear")).not.toBeVisible();
terminal.keyUp();
await expect(terminal.getByText("clear")).toBeVisible();
expect(terminal.getCursor().x).toBe(7);
terminal.keyDown();
await expect(terminal.getByText("clear")).not.toBeVisible();
expect(terminal.getCursor().x).toBe(2);
});
test.when(isLinux, "ctrl+c", async ({ terminal }) => {
await expect(terminal).toMatchSnapshot();
terminal.keyCtrlC();
await expect(terminal.getByText("^C")).toBeVisible();
await expect(terminal).toMatchSnapshot();
});
test.when(isWindows, "ctrl+d", async ({ terminal }) => {
terminal.keyCtrlD();
await expect(terminal.getByText("^D")).toBeVisible();
});
test("backspace", async ({ terminal }) => {
terminal.write("foo");
await expect(terminal.getByText("foo")).toBeVisible();
terminal.keyBackspace(3);
await expect(terminal.getByText("foo")).not.toBeVisible();
});
test("delete", async ({ terminal }) => {
terminal.write("foo");
await expect(terminal.getByText("foo")).toBeVisible();
terminal.keyLeft(3);
terminal.keyDelete(3);
await expect(terminal.getByText("foo")).not.toBeVisible();
});
});
test.describe("terminal functions", () => {
test("resize", async ({ terminal }) => {
terminal.write("foo");
await expect(terminal.getByText("foo")).toBeVisible();
await expect(terminal).toMatchSnapshot();
terminal.resize(40, 10);
terminal.write("bar");
await expect(terminal.getByText("foobar")).toBeVisible();
await expect(terminal).toMatchSnapshot();
});
test("cursor position", async ({ terminal }) => {
terminal.resize(40, 10);
expect(terminal.getCursor().x).toBe(2);
expect(terminal.getCursor().y).toBe(0);
expect(terminal.getCursor().baseY).toBe(0);
terminal.submit("echo foo");
terminal.submit("echo bar");
await expect(terminal.getByText("bar", { strict: false })).toBeVisible();
await expect(terminal.getByText("> ")).toBeVisible();
expect(terminal.getCursor().x).toBe(2);
expect(terminal.getCursor().y).toBeGreaterThanOrEqual(4);
expect(terminal.getCursor().baseY).toBe(0);
for (let i = 0; i < 20; i++) {
terminal.submit(`echo ${i}`);
}
await expect(terminal.getByText("19", { strict: false })).toBeVisible();
await expect(terminal.getByText("> ")).toBeVisible();
expect(terminal.getCursor().x).toBe(2);
expect(terminal.getCursor().y).toBe(9);
expect(terminal.getCursor().baseY).toBeGreaterThanOrEqual(35);
});
});
test.describe("test variations", () => {
test.skip("skip", async () => {
throw new Error("this must be skipped");
});
test.fail("fail", async () => {
throw new Error("this must pass");
});
});
test.describe("use variations", () => {
test.describe("powershell", () => {
test.use({ shell: Shell.Powershell });
test.when(isWindows, "doesn't have logo", async ({ terminal }) => {
await expect(terminal.getByText("> ")).toBeVisible();
await expect(
terminal.getByText("Microsoft Corporation")
).not.toBeVisible();
});
});
test.describe("cmd", () => {
test.use({ shell: Shell.Cmd });
test.when(isWindows, "has logo", async ({ terminal }) => {
await expect(terminal.getByText("Microsoft Corporation")).toBeVisible();
});
});
test.describe("program", () => {
test.use({ program: { file: "git" } });
test("git shows usage message", async ({ terminal }) => {
await expect(
terminal.getByText("usage: git", { full: true })
).toBeVisible();
});
});
test.describe("program with args", () => {
test.use({ program: { file: "git", args: ["status"] } });
test("git shows status message", async ({ terminal }) => {
await expect(
terminal.getByText(/On branch|HEAD detached at/g, { full: true })
).toBeVisible();
});
test("git doesn't show usage", async ({ terminal }) => {
await expect(
terminal.getByText("usage: git", { full: true })
).not.toBeVisible();
});
});
});
test.describe("locators", () => {
test.describe("getByText", () => {
test("match found", async ({ terminal }) => {
await expect(terminal.getByText(">")).toHaveBgColor(0);
});
test.fail("match not found", async ({ terminal }) => {
await expect(terminal.getByText("apple")).toHaveBgColor(0);
});
test.fail("strict mode failure", async ({ terminal }) => {
terminal.submit("echo bar");
terminal.write("foo");
await expect(terminal.getByText("foo")).toBeVisible();
await expect(terminal.getByText("bar")).toBeVisible();
});
test.fail("strict mode failure with .not", async ({ terminal }) => {
terminal.submit("echo bar");
terminal.write("foo");
await expect(terminal.getByText("foo")).toBeVisible();
await expect(terminal.getByText("bar")).not.toBeVisible();
});
});
});
test.describe("color detection", () => {
test("checks the default background color", async ({ terminal }) => {
await expect(terminal.getByText(">")).toHaveBgColor(0);
await expect(terminal.getByText(">")).toHaveBgColor([0, 0, 0]);
});
test("checks the default foreground color", async ({ terminal }) => {
await expect(terminal.getByText(">")).toHaveFgColor(0);
});
test.when(isLinux, "checks background color", async ({ terminal }) => {
terminal.write(String.raw`printf "\x1b[42m%s%s\n\x1b[0m" foo bar`);
terminal.submit();
await expect(terminal.getByText("foobar")).toHaveBgColor(2);
});
test.when(isLinux, "checks foreground color", async ({ terminal }) => {
terminal.write(String.raw`printf "\x1b[31m%s%s\n\x1b[0m" foo bar`);
terminal.submit();
await expect(terminal.getByText("foobar")).toHaveFgColor(1);
});
test.fail(
"checks failure on background color when it doesn't match",
async ({ terminal }) => {
await expect(terminal.getByText(">")).toHaveBgColor(16);
}
);
test.fail(
"checks failure on background color when it matches .not",
async ({ terminal }) => {
await expect(terminal.getByText(">")).not.toHaveBgColor(0);
}
);
});
test.describe("shells", () => {
const shells = isWindows ? windowsShells : unixShells;
shells.forEach((shell) => {
test.describe(`[${shell}]`, () => {
test.use({ shell });
test("simple controls", async ({ terminal }) => {
terminal.submit("echo bar");
await expect(
terminal.getByText("bar", { strict: false })
).toBeVisible();
await expect(terminal.getByText("> ")).toBeVisible();
terminal.write("tomato");
await expect(terminal.getByText("tomato")).toBeVisible();
});
});
});
});
let fileBeforeAllCount = 0;
test.beforeAll(() => {
fileBeforeAllCount++;
});
test.describe("lifecycle hooks", () => {
let describeBeforeAllCount = 0;
let beforeEachCount = 0;
let afterEachCount = 0;
test.beforeAll(() => {
describeBeforeAllCount++;
});
test.afterAll(() => {});
test.beforeEach(async () => {
beforeEachCount++;
});
test.afterEach(async () => {
afterEachCount++;
});
test("file-level beforeAll ran once", async () => {
expect(fileBeforeAllCount).toBe(1);
});
test("describe-level beforeAll ran once", async () => {
expect(describeBeforeAllCount).toBe(1);
});
test("beforeEach runs before each test", async () => {
expect(beforeEachCount).toBeGreaterThanOrEqual(1);
});
test("afterEach ran for prior test", async () => {
expect(afterEachCount).toBeGreaterThanOrEqual(1);
});
test.describe("nested hooks", () => {
let nestedBeforeAllCount = 0;
let nestedBeforeEachCount = 0;
test.beforeAll(() => {
nestedBeforeAllCount++;
});
test.beforeEach(async () => {
nestedBeforeEachCount++;
});
test.afterEach(async () => {});
test("file-level beforeAll also visible in nested scope", async () => {
expect(fileBeforeAllCount).toBe(1);
});
test("outer describe beforeAll also visible in nested scope", async () => {
expect(describeBeforeAllCount).toBe(1);
});
test("nested beforeAll ran once", async () => {
expect(nestedBeforeAllCount).toBe(1);
});
test("outer beforeEach also runs for nested tests", async () => {
expect(beforeEachCount).toBeGreaterThanOrEqual(1);
});
test("nested beforeEach runs for nested tests", async () => {
expect(nestedBeforeEachCount).toBeGreaterThanOrEqual(1);
});
});
});