Skip to content

Commit 370288f

Browse files
committed
fixup: add e2e tests
1 parent 2bc07c3 commit 370288f

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

packages/cli-repl/src/format-output.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ function dateInspect(
428428
function inspect(output: unknown, options: FormatOptions): string {
429429
// Set a custom inspection function for 'Date' objects. Since we only want this
430430
// to affect mongosh scripts, we unset it later.
431+
const originalDateInspect = (Date.prototype as any)[util.inspect.custom];
431432
(Date.prototype as any)[util.inspect.custom] = dateInspect;
432433
try {
433434
return util.inspect(
@@ -443,6 +444,8 @@ function inspect(output: unknown, options: FormatOptions): string {
443444
);
444445
} finally {
445446
delete (Date.prototype as any)[util.inspect.custom];
447+
if (originalDateInspect)
448+
(Date.prototype as any)[util.inspect.custom] = originalDateInspect;
446449
}
447450
}
448451

packages/e2e-tests/test/e2e-bson.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,53 @@ describe('BSON e2e', function () {
588588
shell.assertNoErrors();
589589
});
590590
});
591+
describe('inspect nesting depth', function () {
592+
it('inspects a full bson document when it is read from the server', async function () {
593+
await shell.executeLine(`use ${dbName}`);
594+
await shell.executeLine(`deepAndNested = ({
595+
a: { b: { c: { d: { e: { f: { g: { h: "foundme" } } } } } } },
596+
array: [...Array(100000).keys()].map(i => ({ num: i })),
597+
str: 'All work and no play makes Jack a dull boy'.repeat(4096) + 'The End'
598+
});`);
599+
await shell.executeLine(`db.coll.insertOne(deepAndNested)`);
600+
// Deeply nested object from the server should be fully printed
601+
const output = await shell.executeLine('db.coll.findOne()');
602+
expect(output).not.to.include('[Object');
603+
expect(output).not.to.include('more items');
604+
expect(output).to.include('foundme');
605+
expect(output).to.include('num: 99999');
606+
expect(output).to.include('The End');
607+
// Same object doesn't need to be fully printed if created by the user
608+
const output2 = await shell.executeLine('deepAndNested');
609+
expect(output2).to.include('[Object');
610+
expect(output2).to.include('more items');
611+
expect(output2).not.to.include('foundme');
612+
expect(output2).not.to.include('num: 99999');
613+
expect(output2).not.to.include('The End');
614+
shell.assertNoErrors();
615+
});
616+
it('can parse serverStatus back to its original form', async function () {
617+
// Dates get special treatment but that doesn't currently apply
618+
// to mongosh's util.inspect that's available to users
619+
// (although maybe it should?).
620+
await shell.executeLine(
621+
`Date.prototype[Symbol.for('nodejs.util.inspect.custom')] = function(){ return 'ISODate("' + this.toISOString() + '")'; };`
622+
);
623+
// 'void 0' to avoid large output in the shell from serverStatus
624+
await shell.executeLine(
625+
'A = db.adminCommand({ serverStatus: 1 }); void 0'
626+
);
627+
await shell.executeLine('util.inspect(A)');
628+
await shell.executeLine(`B = eval('(' + util.inspect(A) + ')'); void 0`);
629+
shell.assertNoErrors();
630+
const output1 = await shell.executeLineWithJSONResult('A', {
631+
parseAsEJSON: false,
632+
});
633+
const output2 = await shell.executeLineWithJSONResult('B', {
634+
parseAsEJSON: false,
635+
});
636+
expect(output1).to.deep.equal(output2);
637+
shell.assertNoErrors();
638+
});
639+
});
591640
});

packages/e2e-tests/test/test-shell.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,14 +302,17 @@ export class TestShell {
302302
return this._output.slice(previousOutputLength);
303303
}
304304

305-
async executeLineWithJSONResult(line: string): Promise<any> {
305+
async executeLineWithJSONResult(
306+
line: string,
307+
{ parseAsEJSON = true } = {}
308+
): Promise<any> {
306309
const output = await this.executeLine(
307310
`">>>>>>" + EJSON.stringify(${line}, {relaxed:false}) + "<<<<<<"`
308311
);
309312
const matching = output.match(/>>>>>>(.+)<<<<<</)?.[1];
310313
if (!matching)
311314
throw new Error(`Could not parse output from line: '${output}'`);
312-
return EJSON.parse(matching);
315+
return (parseAsEJSON ? EJSON : JSON).parse(matching);
313316
}
314317

315318
assertNoErrors(): void {

0 commit comments

Comments
 (0)