Skip to content

Commit 19ba411

Browse files
committed
added first line of the stack to the non-detailed variants and changes tests according to that and also removed from message. toJSONDetailed
1 parent 03bb5a8 commit 19ba411

2 files changed

Lines changed: 32 additions & 78 deletions

File tree

src/formatters/ConsoleFormatter.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,12 @@ export class ConsoleFormatter {
159159
}
160160

161161
// The short format for a console message.
162-
toString(): string {
163-
return `msgid=${this.#id} [${this.#type}] ${this.#text} (${this.#argCount} args)`;
162+
toString(): string {
163+
const topFrame = this.#getTopFrameLocation();
164+
const locationStr = topFrame
165+
? ` (${topFrame.url}:${topFrame.lineNumber}:${topFrame.columnNumber})`
166+
: '';
167+
return `msgid=${this.#id} [${this.#type}] ${this.#text}${locationStr} (${this.#argCount} args)`;
164168
}
165169

166170
// The verbose format for a console message, including all details.
@@ -305,16 +309,17 @@ export class ConsoleFormatter {
305309
}
306310

307311
toJSON(): object {
312+
const location = this.#getTopFrameLocation();
308313
return {
309314
type: this.#type,
310315
text: this.#text,
311316
argsCount: this.#argCount,
312317
id: this.#id,
318+
...(location ? { location } : {}),
313319
};
314320
}
315321

316322
toJSONDetailed(): object {
317-
const location = this.#getTopFrameLocation();
318323

319324
return {
320325
id: this.#id,
@@ -324,7 +329,6 @@ export class ConsoleFormatter {
324329
typeof arg === 'object' ? arg : String(arg),
325330
),
326331
stackTrace: this.#stack,
327-
...(location ? { location } : {}),
328332
};
329333
}
330334

tests/formatters/ConsoleFormatter.test.ts

Lines changed: 24 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -676,48 +676,6 @@ describe('ConsoleFormatter', () => {
676676

677677
describe('ConsoleFormatter - Source Map & Heavy Error Tests', () => {
678678

679-
it('includes top frame location in JSON detailed output', async () => {
680-
const mockFrame = {
681-
name: 'myFunction',
682-
uiSourceCode: {
683-
uiLocation: (line: number, column: number) => ({
684-
uiSourceCode: { url: () => 'http://example.com/app.js' },
685-
lineNumber: line,
686-
columnNumber: column,
687-
linkText: () => 'app.js:10:5',
688-
}),
689-
},
690-
line: 9,
691-
column: 4,
692-
};
693-
694-
const mockStackTrace = {
695-
syncFragment: { frames: [mockFrame] },
696-
asyncFragments: [],
697-
} as unknown as DevTools.StackTrace.StackTrace.StackTrace;
698-
699-
const mockError = {
700-
message: 'Something went wrong',
701-
stackTrace: mockStackTrace,
702-
cause: undefined,
703-
} as unknown as SymbolizedError;
704-
705-
const uncaughtError = new UncaughtError({} as Protocol.Runtime.ExceptionDetails, 'target-1');
706-
707-
const formatter = await ConsoleFormatter.from(uncaughtError, {
708-
id: 42,
709-
resolvedStackTraceForTesting: mockStackTrace,
710-
resolvedCauseForTesting: mockError,
711-
});
712-
713-
const json = formatter.toJSONDetailed();
714-
assert.deepStrictEqual((json as any).location, {
715-
url: 'http://example.com/app.js',
716-
lineNumber: 10,
717-
columnNumber: 5,
718-
});
719-
});
720-
721679
it('handles heavy/nested errors with cause chain', async () => {
722680
const innerFrame = { line: 5, column: 1, url: 'lib.js', name: 'inner' };
723681
const outerFrame = { line: 10, column: 2, url: 'app.js', name: 'outer' };
@@ -775,47 +733,39 @@ describe('ConsoleFormatter - Source Map & Heavy Error Tests', () => {
775733
assert.ok(detailed.includes('asyncFunc'));
776734
});
777735

778-
it('includes correct top frame location even when all others ignored', async () => {
779-
const ignoredFrame = { name: 'ignored', line: 1, column: 1, url: 'ignore.js' };
780-
const topFrame = {
781-
name: 'topFrame',
782-
line: 10,
783-
column: 2,
784-
uiSourceCode: {
785-
uiLocation: (line: number, column: number) => ({
786-
uiSourceCode: { url: () => 'top.js' },
787-
lineNumber: line,
788-
columnNumber: column,
789-
linkText: () => 'top.js:11:3',
790-
}),
791-
},
792-
};
793-
736+
it('includes first stack line in toString()', async () => {
737+
const mockFrame = { name: 'firstFunc', url: 'first.js', line: 10, column: 5 };
794738
const stackTrace = {
795-
syncFragment: { frames: [ignoredFrame as any, topFrame as any] },
739+
syncFragment: { frames: [mockFrame] },
796740
asyncFragments: [],
797741
} as unknown as DevTools.StackTrace.StackTrace.StackTrace;
798742

799-
const error = SymbolizedError.createForTesting(
800-
'Test error',
801-
stackTrace,
802-
);
743+
const msg = createMockMessage({ type: () => 'error', text: () => 'Test error' });
744+
const formatter = await ConsoleFormatter.from(msg, {
745+
id: 200,
746+
resolvedStackTraceForTesting: stackTrace,
747+
});
803748

804-
const uncaughtError = new UncaughtError({} as Protocol.Runtime.ExceptionDetails, 'ignore-test');
749+
const str = formatter.toString();
750+
assert.ok(str.includes('at firstFunc (first.js:10:5)'), 'First stack line should appear in toString()');
751+
});
805752

806-
const formatter = await ConsoleFormatter.from(uncaughtError, {
807-
id: 101,
753+
it('includes first stack line in toJSON()', async () => {
754+
const mockFrame = { name: 'firstFunc', url: 'main.js', line: 15, column: 3 };
755+
const stackTrace = {
756+
syncFragment: { frames: [mockFrame] },
757+
asyncFragments: [],
758+
} as unknown as DevTools.StackTrace.StackTrace.StackTrace;
759+
760+
const msg = createMockMessage({ type: () => 'log', text: () => 'Logging error' });
761+
const formatter = await ConsoleFormatter.from(msg, {
762+
id: 201,
808763
resolvedStackTraceForTesting: stackTrace,
809-
resolvedCauseForTesting: error,
810-
isIgnoredForTesting: frame => frame.url === 'ignore.js',
811764
});
812765

813-
const json = formatter.toJSONDetailed();
814-
assert.deepStrictEqual((json as any).location, {
815-
url: 'top.js',
816-
lineNumber: 11,
817-
columnNumber: 3,
818-
});
766+
const json = formatter.toJSON();
767+
const str = formatter.toString();
768+
assert.ok(str.includes('at firstFunc (main.js:15:3)'), 'First stack line should appear in toJSON() output via toString() check');
819769
});
820770

821771
});

0 commit comments

Comments
 (0)