diff --git a/src/sessions.ts b/src/sessions.ts index e4f37281e1..803a174dec 100644 --- a/src/sessions.ts +++ b/src/sessions.ts @@ -169,10 +169,10 @@ export class ClientSession extends TypedEventEmitter { this[kServerSession] = this.explicit ? this.sessionPool.acquire() : null; this[kTxnNumberIncrement] = 0; - const canEnableCausalConsistency = this.explicit && options.snapshot !== true; + const defaultCausalConsistencyValue = this.explicit && options.snapshot !== true; this.supports = { // if we can enable causal consistency, do so by default - causalConsistency: canEnableCausalConsistency && options.causalConsistency !== false + causalConsistency: options.causalConsistency ?? defaultCausalConsistencyValue }; this.clusterTime = options.initialClusterTime; diff --git a/test/unit/sessions.test.js b/test/unit/sessions.test.js index 4ee8408e5e..7d23c381cf 100644 --- a/test/unit/sessions.test.js +++ b/test/unit/sessions.test.js @@ -193,18 +193,20 @@ describe('Sessions - unit', function () { expect(session.supports).property('causalConsistency', false); }); - it('should set `causalConsistency` to `false` in implicit sessions regardless of options', function () { - const sessionTrue = new ClientSession(client, serverSessionPool, { + it('should respect `causalConsistency=false` option in implicit sessions', function () { + const session = new ClientSession(client, serverSessionPool, { explicit: false, - causalConsistency: true + causalConsistency: false }); - expect(sessionTrue.supports).property('causalConsistency', false); + expect(session.supports).property('causalConsistency', false); + }); - const sessionFalse = new ClientSession(client, serverSessionPool, { + it('should respect `causalConsistency=true` option in implicit sessions', function () { + const session = new ClientSession(client, serverSessionPool, { explicit: false, - causalConsistency: false + causalConsistency: true }); - expect(sessionFalse.supports).property('causalConsistency', false); + expect(session.supports).property('causalConsistency', true); }); it('should default to `null` for `clusterTime`', function () {