Files
bun.sh/test/bun.js/node-stream.test.js
Derrick Farris 9f9db85a94 fix(stream): Fix Readable.pipe() (#1606)
* fix(stream): fix some debug logs that were breaking .pipe

* fix(stream): another debug fix

* test(stream): add test for .pipe
2022-12-12 18:58:28 -08:00

74 lines
1.9 KiB
JavaScript

import { expect, describe, it } from "bun:test";
import {
Readable,
Writable,
Duplex,
Transform,
PassThrough,
} from "node:stream";
describe("Readable", () => {
it("should be able to be piped via .pipe", () => {
const readable = new Readable({
_read() {
this.push("Hello World!");
this.push(null);
},
});
const writable = new Writable({
_write(chunk, encoding, callback) {
expect(chunk.toString()).toBe("Hello World!");
callback();
},
});
readable.pipe(writable);
});
});
describe("Duplex", () => {
it("should allow subclasses to be derived via .call() on class", () => {
function Subclass(opts) {
if (!(this instanceof Subclass)) return new Subclass(opts);
Duplex.call(this, opts);
}
Object.setPrototypeOf(Subclass.prototype, Duplex.prototype);
Object.setPrototypeOf(Subclass, Duplex);
const subclass = new Subclass();
expect(subclass instanceof Duplex).toBe(true);
});
});
describe("Transform", () => {
it("should allow subclasses to be derived via .call() on class", () => {
function Subclass(opts) {
if (!(this instanceof Subclass)) return new Subclass(opts);
Transform.call(this, opts);
}
Object.setPrototypeOf(Subclass.prototype, Transform.prototype);
Object.setPrototypeOf(Subclass, Transform);
const subclass = new Subclass();
expect(subclass instanceof Transform).toBe(true);
});
});
describe("PassThrough", () => {
it("should allow subclasses to be derived via .call() on class", () => {
function Subclass(opts) {
if (!(this instanceof Subclass)) return new Subclass(opts);
PassThrough.call(this, opts);
}
Object.setPrototypeOf(Subclass.prototype, PassThrough.prototype);
Object.setPrototypeOf(Subclass, PassThrough);
const subclass = new Subclass();
expect(subclass instanceof PassThrough).toBe(true);
});
});