/
watch.ts
210 lines (178 loc) · 5.53 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
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
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;
}, 5000);
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);
});
describe('ignore', ({ test }) => {
test('multiple files ignored', async () => {
// given
const initialValue = 'first round';
const alteredValue = 'second round';
const includedFilename = 'included.js';
const ignoredFilenames = ['ignored-1.js', 'ignored-2.js'];
const fixtures = await createFixture({
[includedFilename]: `
import { value as value1 } from './ignored-1';
import { value as value2 } from './ignored-2';
console.log(value1 + value2);
`.trim(),
[ignoredFilenames[0]]: `export const value = '${initialValue}';`,
[ignoredFilenames[1]]: `export const value = '${initialValue}';`,
});
const tsxProcess = tsx({
args: [
'watch',
'--clear-screen=false',
`--ignore=${path.join(fixtures.path, ignoredFilenames[0])}`,
`--ignore=${path.join(fixtures.path, ignoredFilenames[1])}`,
path.join(fixtures.path, includedFilename),
],
});
let aggregatedOutput = '';
async function onStdOut(data: Buffer) {
const chunkString = data.toString();
aggregatedOutput += chunkString;
if (new RegExp(`${initialValue}\n`).test(chunkString)) {
await Promise.all(ignoredFilenames.map(ignoredFilename => fixtures.writeFile(ignoredFilename, `export const value = '${alteredValue}';`)));
// make sure to wait for chokidar to pick up changes
// in the ignored file before manually triggering a rerun
setTimeout(async () => {
await fixtures.writeFile(includedFilename, 'console.log(\'TERMINATE\');');
}, 500);
} else if (chunkString.match('TERMINATE\n')) {
// cleanup
await fixtures.rm();
tsxProcess.kill();
}
}
tsxProcess.stdout?.on('data', onStdOut);
let error = false;
tsxProcess.stderr?.on('data', () => {
error = true;
});
// when
await tsxProcess;
// then
if (error) {
// manten does not come with a fail() utility.
expect('No error throwing').toEqual('but was thrown.');
}
expect(aggregatedOutput).not.toMatch(alteredValue);
});
});
});
});