/
watch.ts
145 lines (121 loc) · 3.48 KB
/
watch.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
import path from 'path';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { tsx } from '../utils/tsx';
export default testSuite(async ({ describe }, fixturePath: string) => {
describe('watch', ({ test, describe }) => {
test('require file path', async () => {
const tsxProcess = await tsx({
args: ['watch'],
});
expect(tsxProcess.exitCode).toBe(1);
expect(tsxProcess.stderr).toMatch('Error: Missing required parameter "script path"');
});
test('watch files for changes', async () => {
const fixture = await createFixture({
'index.js': 'console.log(1)',
});
const tsxProcess = tsx({
args: [
'watch',
path.join(fixture.path, 'index.js'),
],
});
await new Promise<void>((resolve) => {
async function onStdOut(data: Buffer) {
const chunkString = data.toString();
if (chunkString.match('1\n')) {
await fixture.writeFile('index.js', 'console.log(2)');
} else if (chunkString.match('2\n')) {
tsxProcess.kill();
resolve();
}
}
tsxProcess.stdout!.on('data', onStdOut);
tsxProcess.stderr!.on('data', onStdOut);
});
}, 5000);
test('suppresses warnings & clear screen', async () => {
const tsxProcess = tsx({
args: [
'watch',
path.join(fixturePath, 'log-argv.ts'),
],
});
const stdout = await new Promise<string>((resolve) => {
let aggregateStdout = '';
let hitEnter = false;
function onStdOut(data: Buffer) {
const chunkString = data.toString();
// console.log({ chunkString });
aggregateStdout += chunkString;
if (chunkString.match('log-argv.ts')) {
if (!hitEnter) {
hitEnter = true;
tsxProcess.stdin?.write('enter');
} else {
tsxProcess.kill();
resolve(aggregateStdout);
}
}
}
tsxProcess.stdout!.on('data', onStdOut);
tsxProcess.stderr!.on('data', onStdOut);
});
expect(stdout).not.toMatch('Warning');
expect(stdout).toMatch('\u001Bc');
}, 5000);
test('passes flags', async () => {
const tsxProcess = tsx({
args: [
'watch',
path.join(fixturePath, 'log-argv.ts'),
'--some-flag',
],
});
const stdout = await new Promise<string>((resolve) => {
tsxProcess.stdout!.on('data', (chunk) => {
const chunkString = chunk.toString();
if (chunkString.startsWith('[')) {
resolve(chunkString);
}
});
});
tsxProcess.kill();
expect(stdout).toMatch('"--some-flag"');
await tsxProcess;
}, 2000);
describe('help', ({ test }) => {
test('shows help', async () => {
const tsxProcess = await tsx({
args: ['watch', '--help'],
});
expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch('Run the script and watch for changes');
expect(tsxProcess.stderr).toBe('');
});
// BUGL Currently doesn't pass through `--help
// test('doesn\'t show help with file', async () => {
// const tsxProcess = tsx({
// args: [
// 'watch',
// path.join(fixturePath, 'log-argv.ts'),
// '--help',
// ],
// });
// const stdout = await new Promise<string>((resolve) => {
// tsxProcess.stdout!.on('data', (chunk) => {
// const chunkString = chunk.toString();
// if (chunkString.startsWith('[')) {
// resolve(chunkString);
// }
// });
// });
// tsxProcess.kill();
// console.log(stdout);
// expect(stdout).toMatch('"--help"');
// await tsxProcess;
// }, 2000);
});
});
});