Skip to content

Commit c51fef1

Browse files
authored
test(instr-mongodb): fix tests mongdb tests for 6.19.0 (#3003)
1 parent 41d272d commit c51fef1

File tree

3 files changed

+32
-21
lines changed

3 files changed

+32
-21
lines changed

package-lock.json

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/instrumentation-mongodb/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"@types/mocha": "10.0.10",
6666
"@types/node": "18.18.14",
6767
"cross-env": "7.0.3",
68-
"mongodb": "6.15.0",
68+
"mongodb": "6.19.0",
6969
"nyc": "17.1.0",
7070
"rimraf": "5.0.10",
7171
"test-all-versions": "6.1.0",

packages/instrumentation-mongodb/test/mongodb-v5-v6.test.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,18 +509,29 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
509509
create({
510510
responseHook: (span: Span, result: any) => {
511511
const { data } = result;
512-
if (data.n) {
513-
span.setAttribute('mongodb_insert_count', result.data.n);
512+
513+
// from 6.19.0 insert returns a MongoDBResponse class with a
514+
// `toObject()` method instead of the plain { ok: 1, n: [Number] } response
515+
let insertCount = data.n;
516+
if (!insertCount && typeof data.toObject === 'function') {
517+
insertCount = data.toObject().n;
518+
}
519+
520+
if (insertCount) {
521+
span.setAttribute('mongodb_insert_count', insertCount);
514522
}
523+
515524
// from v6.8.0 the cursor property is not an object but an instance of
516525
// `CursorResponse`. We need to use the `toObject` method to be able to inspect the data
517-
const cursorObj = data.cursor.firstBatch
518-
? data.cursor
519-
: data.cursor.toObject();
520-
span.setAttribute(
521-
'mongodb_first_result',
522-
JSON.stringify(cursorObj.firstBatch[0])
523-
);
526+
if (data.cursor) {
527+
const cursorObj = data.cursor.firstBatch
528+
? data.cursor
529+
: data.cursor.toObject();
530+
span.setAttribute(
531+
'mongodb_first_result',
532+
JSON.stringify(cursorObj.firstBatch[0])
533+
);
534+
}
524535
},
525536
});
526537
});

0 commit comments

Comments
 (0)