|
| 1 | + |
| 2 | +import * as stream from 'node:stream/promises' |
| 3 | +import { Readable } from 'stream' |
| 4 | +import { generate } from 'csv-generate' |
| 5 | +import { parse } from '../lib/index.js' |
| 6 | + |
| 7 | +describe 'API stream.finished', -> |
| 8 | + |
| 9 | + it 'resolved at the end', -> |
| 10 | + # See https://github.com/adaltas/node-csv/issues/333 |
| 11 | + records = [] |
| 12 | + parser = generate(length: 10).pipe parse() |
| 13 | + parser.on 'readable', () => |
| 14 | + while (record = parser.read()) isnt null |
| 15 | + records.push record |
| 16 | + await stream.finished parser |
| 17 | + records.length.should.eql 10 |
| 18 | + |
| 19 | + it 'resolved with `to_line`', -> |
| 20 | + # See https://github.com/adaltas/node-csv/issues/333 |
| 21 | + records = [] |
| 22 | + parser = generate(length: 10).pipe parse to_line: 3 |
| 23 | + parser.on 'readable', () => |
| 24 | + while (record = parser.read()) isnt null |
| 25 | + records.push record |
| 26 | + await stream.finished parser |
| 27 | + records.length.should.eql 3 |
| 28 | + |
| 29 | + it 'rejected on error', -> |
| 30 | + parser = parse to_line: 3 |
| 31 | + parser.write 'a,b,c\n' |
| 32 | + parser.write 'd,e,f\n' |
| 33 | + parser.write 'h,i,j,ohno\n' |
| 34 | + parser.write 'k,l,m\n' |
| 35 | + parser.end() |
| 36 | + parser.on 'readable', () => |
| 37 | + while (record = parser.read()) isnt null then true |
| 38 | + stream |
| 39 | + .finished parser |
| 40 | + .should.be.rejectedWith |
| 41 | + code: 'CSV_RECORD_INCONSISTENT_FIELDS_LENGTH' |
0 commit comments