Skip to content

Commit fb55d6e

Browse files
authored
test: add test utils (#294)
1 parent 93d7e44 commit fb55d6e

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

packages/prompts/src/index.test.ts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,8 @@
1-
import { EventEmitter, Readable, Writable } from 'node:stream';
1+
import { EventEmitter } from 'node:stream';
22
import colors from 'picocolors';
33
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
44
import * as prompts from './index.js';
5-
6-
// TODO (43081j): move this into a util?
7-
class MockWritable extends Writable {
8-
public buffer: string[] = [];
9-
10-
_write(
11-
chunk: any,
12-
_encoding: BufferEncoding,
13-
callback: (error?: Error | null | undefined) => void
14-
): void {
15-
this.buffer.push(chunk.toString());
16-
callback();
17-
}
18-
}
19-
20-
class MockReadable extends Readable {
21-
protected _buffer: unknown[] | null = [];
22-
23-
_read() {
24-
if (this._buffer === null) {
25-
this.push(null);
26-
return;
27-
}
28-
29-
for (const val of this._buffer) {
30-
this.push(val);
31-
}
32-
33-
this._buffer = [];
34-
}
35-
36-
pushValue(val: unknown): void {
37-
this._buffer?.push(val);
38-
}
39-
40-
close(): void {
41-
this._buffer = null;
42-
}
43-
}
5+
import { MockReadable, MockWritable } from './test-utils.js';
446

457
describe.each(['true', 'false'])('prompts (isCI = %s)', (isCI) => {
468
let originalCI: string | undefined;

packages/prompts/src/test-utils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Readable, Writable } from 'node:stream';
2+
3+
export class MockWritable extends Writable {
4+
public buffer: string[] = [];
5+
6+
_write(
7+
chunk: any,
8+
_encoding: BufferEncoding,
9+
callback: (error?: Error | null | undefined) => void
10+
): void {
11+
this.buffer.push(chunk.toString());
12+
callback();
13+
}
14+
}
15+
16+
export class MockReadable extends Readable {
17+
protected _buffer: unknown[] | null = [];
18+
19+
_read() {
20+
if (this._buffer === null) {
21+
this.push(null);
22+
return;
23+
}
24+
25+
for (const val of this._buffer) {
26+
this.push(val);
27+
}
28+
29+
this._buffer = [];
30+
}
31+
32+
pushValue(val: unknown): void {
33+
this._buffer?.push(val);
34+
}
35+
36+
close(): void {
37+
this._buffer = null;
38+
}
39+
}

0 commit comments

Comments
 (0)