1
1
import { describe , expect , test } from 'bun:test' ;
2
2
import { VirtualConsole } from 'happy-dom' ;
3
+ import * as setupExports from '../setup' ;
4
+ import { originalConsoleCtor , reset } from '../setup' ;
5
+
6
+ describe ( 'exports' , ( ) => {
7
+ const exports = [ 'originalConsoleCtor' , 'reset' ] ;
8
+
9
+ test . each ( exports ) ( 'has "%s" named export' , ( exportName ) => {
10
+ expect ( setupExports ) . toHaveProperty ( exportName ) ;
11
+ } ) ;
12
+
13
+ test ( 'does not have a default export' , ( ) => {
14
+ expect ( setupExports ) . not . toHaveProperty ( 'default' ) ;
15
+ } ) ;
16
+
17
+ test ( 'does not export anything else' , ( ) => {
18
+ expect ( Object . keys ( setupExports ) ) . toHaveLength ( exports . length ) ;
19
+ } ) ;
20
+ } ) ;
3
21
4
22
describe ( 'matcher: toBePlainObject' , ( ) => {
5
23
const plainObjects = [
@@ -67,15 +85,12 @@ describe('console2', () => {
67
85
expect ( console2 ) . toBeDefined ( ) ;
68
86
} ) ;
69
87
70
- // TODO: How to test this? Since setup.ts is preloaded, there's no way to get
71
- // the original console.
72
- // test('is the original console', () => {
73
- // expect(console2).toBe(originalConsole);
74
- // });
88
+ test ( 'is the original console' , ( ) => {
89
+ expect ( console2 ) . toBeInstanceOf ( originalConsoleCtor ) ;
90
+ } ) ;
75
91
76
- test ( 'is not a happy-dom virtual console' , ( ) => {
77
- expect ( window . console ) . toBeInstanceOf ( VirtualConsole ) ;
78
- expect ( console ) . toStrictEqual ( window . console ) ;
92
+ test ( 'is not the happy-dom virtual console' , ( ) => {
93
+ expect ( console2 ) . not . toBeInstanceOf ( VirtualConsole ) ;
79
94
expect ( console2 ) . not . toBe ( console ) ;
80
95
expect ( console2 ) . not . toBe ( window . console ) ;
81
96
} ) ;
@@ -87,13 +102,12 @@ describe('happy-dom', () => {
87
102
'window' ,
88
103
'document' ,
89
104
'console' ,
105
+ 'fetch' ,
90
106
'setTimeout' ,
91
107
'clearTimeout' ,
92
108
'DocumentFragment' ,
93
109
'CSSStyleSheet' ,
94
110
'Text' ,
95
- 'fetch' ,
96
- 'MutationObserver' ,
97
111
] ;
98
112
99
113
test . each ( globals ) ( '"%s" global exists' , ( global ) => {
@@ -103,6 +117,116 @@ describe('happy-dom', () => {
103
117
test ( 'console is a virtual console' , ( ) => {
104
118
expect ( window . console ) . toBeInstanceOf ( VirtualConsole ) ;
105
119
expect ( console ) . toBeInstanceOf ( VirtualConsole ) ;
106
- expect ( console ) . toStrictEqual ( window . console ) ;
120
+ expect ( console ) . toBe ( window . console ) ; // same instance
121
+ } ) ;
122
+
123
+ test ( 'console is not the original console' , ( ) => {
124
+ expect ( console ) . not . toBeInstanceOf ( originalConsoleCtor ) ;
125
+ expect ( console ) . not . toBe ( console2 ) ;
126
+ } ) ;
127
+
128
+ describe ( 'virtual console' , ( ) => {
129
+ test ( 'has no log entries by default' , ( ) => {
130
+ const logs = happyDOM . virtualConsolePrinter . read ( ) ;
131
+ expect ( logs ) . toBeArray ( ) ;
132
+ expect ( logs ) . toHaveLength ( 0 ) ;
133
+ } ) ;
134
+
135
+ // types shouldn't include @types/node Console['Console'] property
136
+ const methods : ( keyof Omit < Console , 'Console' > ) [ ] = [
137
+ 'assert' ,
138
+ // 'clear', // clears log entries so we can't test it
139
+ 'count' ,
140
+ 'countReset' ,
141
+ 'debug' ,
142
+ 'dir' ,
143
+ 'dirxml' ,
144
+ 'error' ,
145
+ // @ts -expect-error - alias for console.error
146
+ 'exception' ,
147
+ 'group' ,
148
+ 'groupCollapsed' ,
149
+ // 'groupEnd', // doesn't log anything
150
+ 'info' ,
151
+ 'log' ,
152
+ // 'profile', // not implemented in happy-dom
153
+ // 'profileEnd',
154
+ 'table' ,
155
+ // 'time', // doesn't log anything
156
+ // 'timeStamp',
157
+ // 'timeLog',
158
+ // 'timeEnd',
159
+ 'trace' ,
160
+ 'warn' ,
161
+ ] ;
162
+
163
+ test . each ( methods ) ( 'has log entry after "%s" call' , ( method ) => {
164
+ // eslint-disable-next-line no-console
165
+ console [ method ] ( ) ;
166
+ expect ( happyDOM . virtualConsolePrinter . read ( ) ) . toHaveLength ( 1 ) ;
167
+ } ) ;
168
+
169
+ test ( 'clears log entries after read' , ( ) => {
170
+ expect ( happyDOM . virtualConsolePrinter . read ( ) ) . toHaveLength ( 0 ) ;
171
+ // eslint-disable-next-line no-console
172
+ console . log ( ) ;
173
+ expect ( happyDOM . virtualConsolePrinter . read ( ) ) . toHaveLength ( 1 ) ;
174
+ expect ( happyDOM . virtualConsolePrinter . read ( ) ) . toHaveLength ( 0 ) ;
175
+ } ) ;
176
+ } ) ;
177
+ } ) ;
178
+
179
+ describe ( 'reset' , ( ) => {
180
+ test ( 'is a function' , ( ) => {
181
+ expect ( reset ) . toBeFunction ( ) ;
182
+ } ) ;
183
+
184
+ test ( 'takes no arguments' , ( ) => {
185
+ expect ( reset ) . toHaveLength ( 0 ) ;
186
+ } ) ;
187
+
188
+ test ( 'resets global chrome instance' , async ( ) => {
189
+ ( chrome as typeof chrome & { foo ?: string } ) . foo = 'bar' ;
190
+ expect ( chrome ) . toHaveProperty ( 'foo' , 'bar' ) ;
191
+ await reset ( ) ;
192
+ expect ( chrome ) . not . toHaveProperty ( 'foo' ) ;
193
+ } ) ;
194
+
195
+ test ( 'resets global window instance' , async ( ) => {
196
+ ( window as Window & { foo ?: string } ) . foo = 'bar' ;
197
+ expect ( window ) . toHaveProperty ( 'foo' , 'bar' ) ;
198
+ await reset ( ) ;
199
+ expect ( window ) . not . toHaveProperty ( 'foo' ) ;
200
+ } ) ;
201
+
202
+ test ( 'resets global document instance' , async ( ) => {
203
+ const h1 = document . createElement ( 'h1' ) ;
204
+ h1 . textContent = 'foo' ;
205
+ document . body . appendChild ( h1 ) ;
206
+ expect ( document . documentElement . innerHTML ) . toBe ( '<head></head><body><h1>foo</h1></body>' ) ;
207
+ await reset ( ) ;
208
+ expect ( document . documentElement . innerHTML ) . toBe ( '<head></head><body></body>' ) ;
209
+ } ) ;
210
+
211
+ test ( 'resets expected globals instances' , async ( ) => {
212
+ const oldChrome = chrome ;
213
+ const oldHappyDOM = happyDOM ;
214
+ const oldWindow = window ;
215
+ const oldDocument = document ;
216
+ const oldConsole = console ;
217
+ const oldFetch = fetch ;
218
+ const oldSetTimeout = setTimeout ;
219
+ const oldClearTimeout = clearTimeout ;
220
+ const oldDocumentFragment = DocumentFragment ;
221
+ await reset ( ) ;
222
+ expect ( chrome ) . not . toBe ( oldChrome ) ;
223
+ expect ( happyDOM ) . not . toBe ( oldHappyDOM ) ;
224
+ expect ( window ) . not . toBe ( oldWindow ) ;
225
+ expect ( document ) . not . toBe ( oldDocument ) ;
226
+ expect ( console ) . not . toBe ( oldConsole ) ;
227
+ expect ( fetch ) . not . toBe ( oldFetch ) ;
228
+ expect ( setTimeout ) . not . toBe ( oldSetTimeout ) ;
229
+ expect ( clearTimeout ) . not . toBe ( oldClearTimeout ) ;
230
+ expect ( DocumentFragment ) . not . toBe ( oldDocumentFragment ) ;
107
231
} ) ;
108
232
} ) ;
0 commit comments