Skip to content

Commit c3c67bd

Browse files
committed
test: add tests for history()
1 parent 4069d38 commit c3c67bd

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

packages/cli-repl/src/mongosh-repl.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { MongoshIOProvider, MongoshNodeReplOptions } from './mongosh-repl';
2222
import MongoshNodeRepl from './mongosh-repl';
2323
import { parseAnyLogEntry } from '../../shell-api/src/log-entry';
2424
import stripAnsi from 'strip-ansi';
25+
import { formatOutput } from './format-output';
2526

2627
function nonnull<T>(value: T | null | undefined): NonNullable<T> {
2728
if (!value) throw new Error();
@@ -526,18 +527,69 @@ describe('MongoshNodeRepl', function () {
526527
]) {
527528
context(mode, function () {
528529
let getHistory: () => string[];
530+
let getAllHistoryItems: () => string[];
529531

530532
beforeEach(function () {
531533
const { history } = mongoshRepl.runtimeState().repl as unknown as {
532534
history: string[];
533535
};
534536
getHistory = () =>
535537
history.filter((line) => !line.startsWith('prefill-'));
538+
getAllHistoryItems = () => history;
536539
for (let i = 0; i < prefill; i++) {
537540
history.unshift(`prefill-${i}`);
538541
}
539542
});
540543

544+
describe('history() command', function () {
545+
it('returns a formatted array of history', async function () {
546+
output = '';
547+
input.write('history()\n');
548+
await waitEval(bus);
549+
expect(output).includes(
550+
formatOutput(
551+
{
552+
value: getAllHistoryItems()
553+
.slice(1, getAllHistoryItems().length)
554+
.reverse(),
555+
},
556+
{ colors: true, maxArrayLength: Infinity }
557+
)
558+
);
559+
});
560+
561+
it('works with array operations', async function () {
562+
output = '';
563+
input.write('history().slice(history().length-10).reverse()\n');
564+
await waitEval(bus);
565+
const history = getAllHistoryItems().slice(1).reverse();
566+
expect(output).includes(
567+
formatOutput(
568+
{
569+
value: history.slice(history.length - 10).reverse(),
570+
},
571+
{ colors: false, maxArrayLength: Infinity }
572+
)
573+
);
574+
});
575+
576+
it('works without ()', async function () {
577+
output = '';
578+
input.write('history\n');
579+
await waitEval(bus);
580+
expect(output).includes(
581+
formatOutput(
582+
{
583+
value: getAllHistoryItems()
584+
.slice(1, getAllHistoryItems().length)
585+
.reverse(),
586+
},
587+
{ colors: true, maxArrayLength: Infinity }
588+
)
589+
);
590+
});
591+
});
592+
541593
it('looks up existing entries, if there are any', async function () {
542594
output = '';
543595
input.write(arrowUp);

packages/cli-repl/src/mongosh-repl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ class MongoshNodeRepl implements EvaluationListener {
402402
const replHistory: string[] = (this.runtimeState().repl as any).history;
403403
const formattedHistory =
404404
// Remove the history call from the formatted history
405-
replHistory.slice(1, replHistory.length).reverse();
405+
replHistory.slice(1).reverse();
406406

407407
// eslint-disable-next-line @typescript-eslint/no-explicit-any
408408
formattedHistory[util.inspect.custom as any] = (() => {

0 commit comments

Comments
 (0)