|
| 1 | +import 'should' |
| 2 | +import {parse as parseStream} from '../lib/stream.js' |
| 3 | +import { CsvError } from '../lib/index.js' |
| 4 | + |
| 5 | +describe('API Web Stream', () => { |
| 6 | + |
| 7 | + describe('stream/web/TransformStream', () => { |
| 8 | + |
| 9 | + it('simple parse', async () => { |
| 10 | + const stream = parseStream(); |
| 11 | + const writer = stream.writable.getWriter(); |
| 12 | + const reader = stream.readable.getReader(); |
| 13 | + await writer.write(Buffer.from("A,B,C\nD,E,F")); |
| 14 | + await writer.close(); |
| 15 | + await reader.read().should.finally.eql({ |
| 16 | + done: false, |
| 17 | + value: ['A', 'B', 'C'], |
| 18 | + }); |
| 19 | + await reader.read().should.finally.eql({ |
| 20 | + done: false, |
| 21 | + value: ['D', 'E', 'F'], |
| 22 | + }); |
| 23 | + await reader.read().should.finally.eql({ |
| 24 | + done: true, |
| 25 | + value: undefined, |
| 26 | + }); |
| 27 | + }) |
| 28 | + |
| 29 | + it("cat error parse", async function () { |
| 30 | + const stream = parseStream(); |
| 31 | + const writer = stream.writable.getWriter(); |
| 32 | + try { |
| 33 | + await writer.write(Buffer.from("A,B,C\nD,E")); |
| 34 | + await writer.close(); |
| 35 | + throw Error("Shall not be called"); |
| 36 | + } catch (err) { |
| 37 | + if (!(err instanceof CsvError)) { |
| 38 | + throw Error("Invalid error type"); |
| 39 | + } |
| 40 | + err.code.should.eql("CSV_RECORD_INCONSISTENT_FIELDS_LENGTH"); |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + }) |
| 45 | +}) |
0 commit comments