1
+ import { mockPromise } from '#mocks/Promise.js' ;
1
2
import { Buffer } from 'node:buffer' ;
2
3
import { readFile } from 'node:fs/promises' ;
3
4
import { lastValueFrom , map , of } from 'rxjs' ;
4
5
import { TestScheduler } from 'rxjs/testing' ;
5
- import { afterAll , beforeEach , describe , expect , test , vi } from 'vitest' ;
6
+ import { afterAll , beforeAll , beforeEach , afterEach , describe , expect , test , vi } from 'vitest' ;
6
7
7
- import { deserialize , serialize } from './json' ;
8
-
9
- describe ( 'log' , ( ) => {
8
+ describe ( 'json' , ( ) => {
10
9
let testScheduler ;
11
10
11
+ beforeAll ( ( ) => {
12
+ mockPromise ( ) ;
13
+ } ) ;
14
+
12
15
beforeEach ( ( ) => {
13
16
testScheduler = new TestScheduler ( ( actual , expected ) => expect ( actual ) . deep . equal ( expected ) ) ;
14
17
} ) ;
15
18
19
+ afterEach ( ( ) => {
20
+ //
21
+ } ) ;
22
+
16
23
afterAll ( ( ) => {
17
24
vi . restoreAllMocks ( ) ;
25
+ vi . unstubAllGlobals ( ) ;
18
26
} ) ;
19
27
20
28
test ( 'boolean - serialize' , async ( ) => {
21
29
const { serialize } = await import ( './json' ) ;
22
-
23
30
const triggerVal = { a : true , b : false } ;
24
31
const expectedVal = { a : 'true' , b : 'false' } ;
25
32
@@ -163,11 +170,16 @@ describe('log', () => {
163
170
test ( 'symbol - deserialize' , async ( ) => {
164
171
const { deserialize } = await import ( './json' ) ;
165
172
166
- const triggerVal = { a : '"gSymbol(bar)"' } ;
167
- const expectedVal = { a : Symbol . for ( 'bar' ) } ;
173
+ const triggerVal = { a : '"gSymbol(bar)"' , b : '"Symbol(foo)"' } ;
174
+ const expectedVal = { a : Symbol , b : Symbol } ;
168
175
169
176
testScheduler . run ( ( { cold, expectObservable } ) => {
170
- expectObservable ( cold ( 'a|' , triggerVal ) . pipe ( deserialize ( ) ) ) . toBe ( 'a|' , expectedVal ) ;
177
+ expectObservable (
178
+ cold ( 'ab|' , triggerVal ) . pipe (
179
+ deserialize ( ) ,
180
+ map ( value => value . constructor )
181
+ )
182
+ ) . toBe ( 'ab|' , expectedVal ) ;
171
183
} ) ;
172
184
} ) ;
173
185
@@ -273,6 +285,32 @@ describe('log', () => {
273
285
} ) ;
274
286
} ) ;
275
287
288
+ test ( 'promise - serialize' , async ( ) => {
289
+ const { serialize } = await import ( './json' ) ;
290
+
291
+ const triggerVal = {
292
+ a : Promise . resolve ( {
293
+ boolean : Promise . resolve ( true ) ,
294
+ string : Promise . resolve ( 'hello world' ) ,
295
+ integer : Promise . resolve ( 42 ) ,
296
+ float : Promise . resolve ( 4.2 ) ,
297
+ bigInt : Promise . resolve ( BigInt ( 42 ) ) ,
298
+ url : Promise . resolve ( new URL ( 'https://www.example.com/' ) ) ,
299
+ date : Promise . resolve ( new Date ( 2025 , 2 , 8 , 14 , 42 , 27 , 357 ) ) ,
300
+ regexp : Promise . resolve ( / [ \w ? \s ] + / gm) ,
301
+ symbol : Promise . resolve ( Symbol . for ( 'bar' ) )
302
+ } )
303
+ } ;
304
+
305
+ const expectedVal = {
306
+ a : '{"boolean":true,"string":"hello world","integer":42,"float":4.2,"bigInt":"42n","url":"https://www.example.com/","date":"2025-03-08T13:42:27.357Z","regexp":"/[\\\\w?\\\\s]+/gm","symbol":"gSymbol(bar)"}'
307
+ } ;
308
+
309
+ testScheduler . run ( ( { cold, expectObservable } ) => {
310
+ expectObservable ( cold ( 'a|' , triggerVal ) . pipe ( serialize ( ) ) ) . toBe ( 'a|' , expectedVal ) ;
311
+ } ) ;
312
+ } ) ;
313
+
276
314
test ( 'observable - serialize' , async ( ) => {
277
315
const { serialize } = await import ( './json' ) ;
278
316
@@ -298,32 +336,6 @@ describe('log', () => {
298
336
} ) ;
299
337
} ) ;
300
338
301
- test ( 'observable - deserialize' , async ( ) => {
302
- const { deserialize } = await import ( './json' ) ;
303
-
304
- const triggerVal = {
305
- a : '{"boolean":true,"string":"hello world","integer":42,"float":4.2,"bigInt":"42n","url":"https://www.example.com/","date":"2025-03-08T13:42:27.357Z","regexp":"/[\\\\w?\\\\s]+/gm","symbol":"gSymbol(bar)"}'
306
- } ;
307
-
308
- const expectedVal = {
309
- a : {
310
- boolean : true ,
311
- string : 'hello world' ,
312
- integer : 42 ,
313
- float : 4.2 ,
314
- bigInt : BigInt ( 42 ) ,
315
- url : new URL ( 'https://www.example.com/' ) ,
316
- date : new Date ( 2025 , 2 , 8 , 14 , 42 , 27 , 357 ) ,
317
- regexp : / [ \w ? \s ] + / gm,
318
- symbol : Symbol . for ( 'bar' )
319
- }
320
- } ;
321
-
322
- testScheduler . run ( ( { cold, expectObservable } ) => {
323
- expectObservable ( cold ( 'a|' , triggerVal ) . pipe ( deserialize ( ) ) ) . toBe ( 'a|' , expectedVal ) ;
324
- } ) ;
325
- } ) ;
326
-
327
339
test ( 'mixed - serialize' , async ( ) => {
328
340
const { serialize } = await import ( './json' ) ;
329
341
@@ -349,11 +361,12 @@ describe('log', () => {
349
361
/ [ \w ? \s ] + / gm,
350
362
Symbol . for ( 'bar' )
351
363
] ) ,
352
- observable : of ( 'foo bar' )
364
+ observable : of ( 'foo bar' ) ,
365
+ promise : Promise . resolve ( 'test' )
353
366
}
354
367
} ;
355
368
const expectedVal = {
356
- a : '{"boolean":true,"string":"hello world","integer":42,"float":4.2,"bigInt":"42n","url":"https://www.example.com/","date":"2025-03-08T13:42:27.357Z","regexp":"/[\\\\w?\\\\s]+/gm","symbol":"gSymbol(bar)","array":[true,"hello world",42,4.2,"42n","https://www.example.com/","2025-03-08T13:42:27.357Z","/[\\\\w?\\\\s]+/gm","gSymbol(bar)"],"observable":"foo bar"}'
369
+ a : '{"boolean":true,"string":"hello world","integer":42,"float":4.2,"bigInt":"42n","url":"https://www.example.com/","date":"2025-03-08T13:42:27.357Z","regexp":"/[\\\\w?\\\\s]+/gm","symbol":"gSymbol(bar)","array":[true,"hello world",42,4.2,"42n","https://www.example.com/","2025-03-08T13:42:27.357Z","/[\\\\w?\\\\s]+/gm","gSymbol(bar)"],"observable":"foo bar","promise":"test" }'
357
370
} ;
358
371
359
372
testScheduler . run ( ( { cold, expectObservable } ) => {
@@ -365,7 +378,7 @@ describe('log', () => {
365
378
const { deserialize } = await import ( './json' ) ;
366
379
367
380
const triggerVal = {
368
- a : '{"boolean":true,"string":"hello world","integer":42,"float":4.2,"bigInt":"42n","url":"https://www.example.com/","date":"2025-03-08T13:42:27.357Z","regexp":"/[\\\\w?\\\\s]+/gm","symbol":"gSymbol(bar)","array":[true,"hello world",42,4.2,"42n","https://www.example.com/","2025-03-08T13:42:27.357Z","/[\\\\w?\\\\s]+/gm","gSymbol(bar)"],"observable":"foo bar"}'
381
+ a : '{"boolean":true,"string":"hello world","integer":42,"float":4.2,"bigInt":"42n","url":"https://www.example.com/","date":"2025-03-08T13:42:27.357Z","regexp":"/[\\\\w?\\\\s]+/gm","symbol":"gSymbol(bar)","array":[true,"hello world",42,4.2,"42n","https://www.example.com/","2025-03-08T13:42:27.357Z","/[\\\\w?\\\\s]+/gm","gSymbol(bar)"],"observable":"foo bar","promise":"test" }'
369
382
} ;
370
383
371
384
const expectedVal = {
@@ -390,7 +403,8 @@ describe('log', () => {
390
403
/ [ \w ? \s ] + / gm,
391
404
Symbol . for ( 'bar' )
392
405
] ,
393
- observable : 'foo bar'
406
+ observable : 'foo bar' ,
407
+ promise : 'test'
394
408
}
395
409
} ;
396
410
@@ -401,6 +415,8 @@ describe('log', () => {
401
415
402
416
/* v8 ignore start */
403
417
test . skip ( 'default' , async ( ) => {
418
+ const { serialize, deserialize } = await import ( './json' ) ;
419
+
404
420
const replacer = [
405
421
{
406
422
validator : value => value ?. constructor === Buffer ,
0 commit comments