Skip to content

Commit 7e97acb

Browse files
fix: [#1845] Replace implementing Node js Console with common IConsole interface to support latest version of Bun (#2102)
* fix: [#1845] Replace implements Console with custom IVirtualConsole interface * chore: [#2102] Changes name from IVirtualConsole to IConsole * chore: [#2102] Changes name from IVirtualConsole to IConsole * chore: [#2102] Changes name from IVirtualConsole to IConsole --------- Co-authored-by: David Ortner <david@ortner.se>
1 parent 3373929 commit 7e97acb

11 files changed

Lines changed: 183 additions & 17 deletions

File tree

packages/happy-dom/src/browser/Browser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type IOptionalBrowserSettings from './types/IOptionalBrowserSettings.js';
44
import BrowserSettingsFactory from './BrowserSettingsFactory.js';
55
import type BrowserPage from './BrowserPage.js';
66
import type IBrowser from './types/IBrowser.js';
7+
import type IConsole from '../console/IConsole.js';
78
import BrowserExceptionObserver from './utilities/BrowserExceptionObserver.js';
89
import * as PropertySymbol from '../PropertySymbol.js';
910
import BrowserErrorCaptureEnum from './enums/BrowserErrorCaptureEnum.js';
@@ -14,7 +15,7 @@ import BrowserErrorCaptureEnum from './enums/BrowserErrorCaptureEnum.js';
1415
export default class Browser implements IBrowser {
1516
public readonly contexts: BrowserContext[];
1617
public readonly settings: IBrowserSettings;
17-
public readonly console: Console | null;
18+
public readonly console: IConsole | null;
1819
public [PropertySymbol.exceptionObserver]: BrowserExceptionObserver | null = null;
1920

2021
/**
@@ -24,7 +25,7 @@ export default class Browser implements IBrowser {
2425
* @param [options.settings] Browser settings.
2526
* @param [options.console] Console.
2627
*/
27-
constructor(options?: { settings?: IOptionalBrowserSettings; console?: Console }) {
28+
constructor(options?: { settings?: IOptionalBrowserSettings; console?: IConsole }) {
2829
this.console = options?.console || null;
2930
this.settings = BrowserSettingsFactory.createSettings(options?.settings);
3031
if (this.settings.errorCapture === BrowserErrorCaptureEnum.processLevel) {

packages/happy-dom/src/browser/BrowserPage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import VirtualConsolePrinter from '../console/VirtualConsolePrinter.js';
2+
import type IConsole from '../console/IConsole.js';
23
import BrowserFrame from './BrowserFrame.js';
34
import type BrowserContext from './BrowserContext.js';
45
import VirtualConsole from '../console/VirtualConsole.js';
@@ -19,7 +20,7 @@ export default class BrowserPage implements IBrowserPage {
1920
public readonly virtualConsolePrinter = new VirtualConsolePrinter();
2021
public readonly mainFrame: BrowserFrame;
2122
public readonly context: BrowserContext;
22-
public readonly console: Console;
23+
public readonly console: IConsole;
2324
public readonly viewport: IBrowserPageViewport;
2425
public readonly closed: boolean = false;
2526

packages/happy-dom/src/browser/detached-browser/DetachedBrowser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import BrowserSettingsFactory from '../BrowserSettingsFactory.js';
55
import type DetachedBrowserPage from './DetachedBrowserPage.js';
66
import type IBrowser from '../types/IBrowser.js';
77
import type IBrowserFrame from '../types/IBrowserFrame.js';
8+
import type IConsole from '../../console/IConsole.js';
89
import type BrowserWindow from '../../window/BrowserWindow.js';
910
import * as PropertySymbol from '../../PropertySymbol.js';
1011
import BrowserErrorCaptureEnum from '../enums/BrowserErrorCaptureEnum.js';
@@ -17,7 +18,7 @@ import type BrowserContext from '../BrowserContext.js';
1718
export default class DetachedBrowser implements IBrowser {
1819
public readonly contexts: DetachedBrowserContext[];
1920
public readonly settings: IBrowserSettings;
20-
public readonly console: Console | null;
21+
public readonly console: IConsole | null;
2122
public readonly windowClass: new (
2223
browserFrame: IBrowserFrame,
2324
options?: { url?: string; width?: number; height?: number }
@@ -37,7 +38,7 @@ export default class DetachedBrowser implements IBrowser {
3738
browserFrame: IBrowserFrame,
3839
options?: { url?: string; width?: number; height?: number }
3940
) => BrowserWindow,
40-
options?: { settings?: IOptionalBrowserSettings; console?: Console }
41+
options?: { settings?: IOptionalBrowserSettings; console?: IConsole }
4142
) {
4243
this.windowClass = windowClass;
4344
this.console = options?.console || null;

packages/happy-dom/src/browser/detached-browser/DetachedBrowserPage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import VirtualConsolePrinter from '../../console/VirtualConsolePrinter.js';
2+
import type IConsole from '../../console/IConsole.js';
23
import DetachedBrowserFrame from './DetachedBrowserFrame.js';
34
import type DetachedBrowserContext from './DetachedBrowserContext.js';
45
import VirtualConsole from '../../console/VirtualConsole.js';
@@ -19,7 +20,7 @@ export default class DetachedBrowserPage implements IBrowserPage {
1920
public readonly virtualConsolePrinter = new VirtualConsolePrinter();
2021
public readonly mainFrame: DetachedBrowserFrame;
2122
public readonly context: DetachedBrowserContext;
22-
public readonly console: Console;
23+
public readonly console: IConsole;
2324
public readonly viewport: IBrowserPageViewport;
2425
public readonly closed: boolean = false;
2526

packages/happy-dom/src/browser/types/IBrowser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type IBrowserContext from './IBrowserContext.js';
22
import type IBrowserPage from './IBrowserPage.js';
33
import type IBrowserSettings from './IBrowserSettings.js';
4+
import type IConsole from '../../console/IConsole.js';
45
import type * as PropertySymbol from '../../PropertySymbol.js';
56
import type BrowserExceptionObserver from '../utilities/BrowserExceptionObserver.js';
67

@@ -13,7 +14,7 @@ export default interface IBrowser {
1314
readonly defaultContext: IBrowserContext;
1415
readonly contexts: IBrowserContext[];
1516
readonly settings: IBrowserSettings;
16-
readonly console: Console | null;
17+
readonly console: IConsole | null;
1718
readonly closed: boolean;
1819
readonly [PropertySymbol.exceptionObserver]: BrowserExceptionObserver | null;
1920

packages/happy-dom/src/browser/types/IBrowserPage.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type IBrowserPageViewport from '../types/IBrowserPageViewport.js';
2+
import type IConsole from '../../console/IConsole.js';
23
import type VirtualConsolePrinter from '../../console/VirtualConsolePrinter.js';
34
import type IBrowserFrame from './IBrowserFrame.js';
45
import type IBrowserContext from './IBrowserContext.js';
@@ -15,7 +16,7 @@ export default interface IBrowserPage {
1516
readonly virtualConsolePrinter: VirtualConsolePrinter;
1617
readonly mainFrame: IBrowserFrame;
1718
readonly context: IBrowserContext;
18-
readonly console: Console;
19+
readonly console: IConsole;
1920
readonly frames: IBrowserFrame[];
2021
readonly viewport: IBrowserPageViewport;
2122
readonly closed: boolean;
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/**
2+
* Console.
3+
*
4+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Console
5+
*/
6+
export default interface IConsole {
7+
/**
8+
* Writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
9+
*
10+
* @param assertion Assertion.
11+
* @param message Message.
12+
* @param args Arguments.
13+
*/
14+
assert(assertion: boolean, message?: any, ...args: Array<object | string>): void;
15+
16+
/**
17+
* Clears the console.
18+
*/
19+
clear(): void;
20+
21+
/**
22+
* Logs the number of times that this particular call to count() has been called.
23+
*
24+
* @param label Label.
25+
*/
26+
count(label?: string): void;
27+
28+
/**
29+
* Resets the counter.
30+
*
31+
* @param label Label.
32+
*/
33+
countReset(label?: string): void;
34+
35+
/**
36+
* Outputs a message to the web console at the "debug" log level.
37+
*
38+
* @param message Message.
39+
* @param args Arguments.
40+
*/
41+
debug(message?: any, ...args: Array<object | string>): void;
42+
43+
/**
44+
* Displays an interactive list of the properties of the specified JavaScript object.
45+
*
46+
* @param data Data.
47+
*/
48+
dir(data: any): void;
49+
50+
/**
51+
* Displays an interactive tree of the descendant elements of the specified XML/HTML element.
52+
*
53+
* @param data Data.
54+
*/
55+
dirxml(data: any[]): void;
56+
57+
/**
58+
* Outputs an error message to the console.
59+
*
60+
* @param message Message.
61+
* @param args Arguments.
62+
*/
63+
error(message?: any, ...args: Array<object | string>): void;
64+
65+
/**
66+
* Creates a new inline group in the console, causing any subsequent console messages to be indented by an additional level, until console.groupEnd() is called.
67+
*
68+
* @param label Label.
69+
*/
70+
group(label?: string): void;
71+
72+
/**
73+
* Creates a new inline group in the console, but prints it as collapsed, requiring the use of a disclosure button to expand it.
74+
*
75+
* @param label Label.
76+
*/
77+
groupCollapsed(label?: string): void;
78+
79+
/**
80+
* Exits the current inline group in the console.
81+
*/
82+
groupEnd(): void;
83+
84+
/**
85+
* Outputs an informational message to the console.
86+
*
87+
* @param message Message.
88+
* @param args Arguments.
89+
*/
90+
info(message?: any, ...args: Array<object | string>): void;
91+
92+
/**
93+
* Outputs a message to the console.
94+
*
95+
* @param message Message.
96+
* @param args Arguments.
97+
*/
98+
log(message?: any, ...args: Array<object | string>): void;
99+
100+
/**
101+
* Starts recording a performance profile.
102+
*/
103+
profile(): void;
104+
105+
/**
106+
* Stops recording a performance profile.
107+
*/
108+
profileEnd(): void;
109+
110+
/**
111+
* Displays tabular data as a table.
112+
*
113+
* @param data Data.
114+
*/
115+
table(data: { [key: string]: number | string | boolean } | string[]): void;
116+
117+
/**
118+
* Starts a timer you can use to track how long an operation takes.
119+
*
120+
* @param label Label.
121+
*/
122+
time(label?: string): void;
123+
124+
/**
125+
* Stops a timer that was previously started by calling console.time().
126+
* The method logs the elapsed time in milliseconds.
127+
*
128+
* @param label Label.
129+
*/
130+
timeEnd(label?: string): void;
131+
132+
/**
133+
* Logs the current value of a timer that was previously started by calling console.time().
134+
* The method logs the elapsed time in milliseconds.
135+
*
136+
* @param label Label.
137+
* @param args Arguments.
138+
*/
139+
timeLog(label?: string, ...args: Array<object | string>): void;
140+
141+
/**
142+
* Adds a single marker to the browser's Performance tool.
143+
*/
144+
timeStamp(): void;
145+
146+
/**
147+
* Outputs a stack trace to the console.
148+
*
149+
* @param message Message.
150+
* @param args Arguments.
151+
*/
152+
trace(message?: any, ...args: Array<object | string>): void;
153+
154+
/**
155+
* Outputs a warning message to the console.
156+
*
157+
* @param message Message.
158+
* @param args Arguments.
159+
*/
160+
warn(message?: any, ...args: Array<object | string>): void;
161+
}

packages/happy-dom/src/console/VirtualConsole.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type IVirtualConsolePrinter from './IVirtualConsolePrinter.js';
2+
import type IConsole from './IConsole.js';
23
import VirtualConsoleLogLevelEnum from './enums/VirtualConsoleLogLevelEnum.js';
34
import VirtualConsoleLogTypeEnum from './enums/VirtualConsoleLogTypeEnum.js';
45
import type IVirtualConsoleLogGroup from './IVirtualConsoleLogGroup.js';
@@ -8,13 +9,7 @@ import type IVirtualConsoleLogGroup from './IVirtualConsoleLogGroup.js';
89
*
910
* @see https://developer.mozilla.org/en-US/docs/Web/API/Console
1011
*/
11-
export default class VirtualConsole implements Console {
12-
// The NodeJS Console interface includes a reference to ConsoleConstructor as a property.
13-
// This is not part of the browser specs, but we need to declare it for type compatibility.
14-
// Using an indexed access type avoids importing from the 'console' module, which can fail
15-
// in consumer projects that don't resolve Node.js built-in module types.
16-
public declare Console: Console['Console'];
17-
12+
export default class VirtualConsole implements IConsole {
1813
#printer: IVirtualConsolePrinter;
1914
#count: { [label: string]: number } = {};
2015
#time: { [label: string]: number } = {};

packages/happy-dom/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ import type ITouchEventInit from './event/events/ITouchEventInit.js';
222222
import type IWheelEventInit from './event/events/IWheelEventInit.js';
223223
import type IFetchInterceptor from './fetch/types/IFetchInterceptor.js';
224224
import type ISyncResponse from './fetch/types/ISyncResponse.js';
225+
import type IConsole from './console/IConsole.js';
225226
import type IVirtualServer from './fetch/types/IVirtualServer.js';
226227

227228
export type {
@@ -251,6 +252,7 @@ export type {
251252
ITouchEventInit,
252253
ITouchInit,
253254
IUIEventInit,
255+
IConsole,
254256
IVirtualServer,
255257
IWheelEventInit,
256258
TEventListener

packages/happy-dom/src/window/BrowserWindow.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import VM from 'vm';
88
import * as PropertySymbol from '../PropertySymbol.js';
99
import Base64 from '../base64/Base64.js';
1010
import BrowserErrorCaptureEnum from '../browser/enums/BrowserErrorCaptureEnum.js';
11+
import type IConsole from '../console/IConsole.js';
1112
import type IBrowserFrame from '../browser/types/IBrowserFrame.js';
1213
import Clipboard from '../clipboard/Clipboard.js';
1314
import ClipboardItem from '../clipboard/ClipboardItem.js';
@@ -756,7 +757,7 @@ export default class BrowserWindow extends EventTarget implements INodeJSGlobal
756757
public readonly TextEncoder: typeof TextEncoder = TextEncoder;
757758
public readonly TextDecoder: typeof TextDecoder = TextDecoder;
758759
public readonly closed = false;
759-
public console: Console;
760+
public console: IConsole;
760761
public name = '';
761762

762763
// Node.js Globals (populated by VMGlobalPropertyScript)

0 commit comments

Comments
 (0)