strom/tests/parse.spec.ts

46 lines
1.0 KiB
TypeScript
Raw Permalink Normal View History

2019-12-06 21:38:52 +00:00
import { Readable, finished } from "stream";
2019-08-15 21:06:54 +00:00
import test from "ava";
import { expect } from "chai";
import mhysa from "../src";
const { parse } = mhysa();
2019-08-15 21:06:54 +00:00
test.cb("parse() parses the streamed elements as JSON", t => {
t.plan(3);
const source = new Readable({ objectMode: true });
const expectedElements = ["abc", {}, []];
let i = 0;
source
.pipe(parse())
.on("data", part => {
expect(part).to.deep.equal(expectedElements[i]);
t.pass();
i++;
})
.on("error", t.end)
.on("end", t.end);
source.push('"abc"');
source.push("{}");
source.push("[]");
source.push(null);
});
test.cb("parse() emits errors on invalid JSON", t => {
2019-12-06 21:38:52 +00:00
t.plan(1);
2019-08-15 21:06:54 +00:00
const source = new Readable({ objectMode: true });
2019-12-06 21:38:52 +00:00
2019-08-15 21:06:54 +00:00
source
.pipe(parse())
.resume()
2019-12-06 21:38:52 +00:00
.on("error", (d: any) => {
t.pass();
t.end();
})
.on("end", t.fail);
2019-08-15 21:06:54 +00:00
source.push("{}");
source.push({});
source.push([]);
source.push(null);
});