|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import { once } from 'events';
|
3 | 3 | import * as sinon from 'sinon';
|
| 4 | +import { Transform } from 'stream'; |
4 | 5 | import { inspect } from 'util';
|
5 | 6 |
|
6 |
| -import { type Collection, type FindCursor, MongoAPIError, type MongoClient } from '../../mongodb'; |
| 7 | +import { |
| 8 | + type Collection, |
| 9 | + type FindCursor, |
| 10 | + MongoAPIError, |
| 11 | + type MongoClient, |
| 12 | + MongoServerError |
| 13 | +} from '../../mongodb'; |
7 | 14 |
|
8 | 15 | describe('class AbstractCursor', function () {
|
9 | 16 | describe('regression tests NODE-5372', function () {
|
@@ -233,4 +240,39 @@ describe('class AbstractCursor', function () {
|
233 | 240 | });
|
234 | 241 | });
|
235 | 242 | });
|
| 243 | + |
| 244 | + describe('transform stream error handling', function () { |
| 245 | + let client: MongoClient; |
| 246 | + let collection: Collection; |
| 247 | + const docs = [{ count: 0 }]; |
| 248 | + beforeEach(async function () { |
| 249 | + client = this.configuration.newClient(); |
| 250 | + |
| 251 | + collection = client.db('abstract_cursor_integration').collection('test'); |
| 252 | + |
| 253 | + await collection.insertMany(docs); |
| 254 | + }); |
| 255 | + |
| 256 | + afterEach(async function () { |
| 257 | + await collection.deleteMany({}); |
| 258 | + await client.close(); |
| 259 | + }); |
| 260 | + |
| 261 | + it('propagates errors to transform stream', async function () { |
| 262 | + const transform = new Transform({ |
| 263 | + transform(data, encoding, callback) { |
| 264 | + callback(null, data); |
| 265 | + } |
| 266 | + }); |
| 267 | + |
| 268 | + // MongoServerError: unknown operator: $bar |
| 269 | + const stream = collection.find({ foo: { $bar: 25 } }).stream({ transform }); |
| 270 | + |
| 271 | + const error: Error | null = await new Promise(resolve => { |
| 272 | + stream.on('error', error => resolve(error)); |
| 273 | + stream.on('end', () => resolve(null)); |
| 274 | + }); |
| 275 | + expect(error).to.be.instanceof(MongoServerError); |
| 276 | + }); |
| 277 | + }); |
236 | 278 | });
|
0 commit comments