diff --git a/src/cmap/connection.ts b/src/cmap/connection.ts index 7aa913fbe4..4d5fb74935 100644 --- a/src/cmap/connection.ts +++ b/src/cmap/connection.ts @@ -384,7 +384,7 @@ export class Connection extends TypedEventEmitter { } else { // Get the first orphaned operation description. const entry = this[kQueue].entries().next(); - if (entry) { + if (entry.value !== undefined) { const [requestId, orphaned]: [number, OperationDescription] = entry.value; // If the orphaned operation description exists then set it. operationDescription = orphaned; diff --git a/test/unit/cmap/connection.test.ts b/test/unit/cmap/connection.test.ts index a9b95bcea2..cb516858b3 100644 --- a/test/unit/cmap/connection.test.ts +++ b/test/unit/cmap/connection.test.ts @@ -287,6 +287,34 @@ describe('new Connection()', function () { }); }); + context('when no operation description is in the queue', function () { + const document = { ok: 1 }; + + beforeEach(function () { + // @ts-expect-error: driverSocket does not fully satisfy the stream type, but that's okay + connection = sinon.spy(new Connection(driverSocket, connectionOptionsDefaults)); + connection.isMonitoringConnection = true; + const queueSymbol = getSymbolFrom(connection, 'queue'); + queue = connection[queueSymbol]; + + // Emit a message that matches the existing operation description. + const msg = generateOpMsgBuffer(document); + const msgHeader: MessageHeader = { + length: msg.readInt32LE(0), + requestId: 2, + responseTo: 1, + opCode: msg.readInt32LE(12) + }; + const msgBody = msg.subarray(16); + + const message = new BinMsg(msg, msgHeader, msgBody); + connection.onMessage(message); + }); + + it('does not error', function () { + }); + }); + context('when more than one operation description is in the queue', function () { let spyOne; let spyTwo;