1
1
'use strict'
2
2
3
3
const test = require ( 'tap' ) . test
4
- const { DateTime } = require ( 'luxon' )
5
4
const validator = require ( 'is-my-json-valid' )
6
5
const build = require ( '..' )
7
6
7
+ process . env . TZ = 'UTC'
8
+
8
9
test ( 'render a date in a string as JSON' , ( t ) => {
9
10
t . plan ( 2 )
10
11
11
12
const schema = {
12
13
title : 'a date in a string' ,
13
14
type : 'string'
14
15
}
15
- const toStringify = new Date ( )
16
+ const toStringify = new Date ( 1674263005800 )
16
17
17
18
const validate = validator ( schema )
18
19
const stringify = build ( schema )
@@ -30,7 +31,7 @@ test('render a date in a string when format is date-format as ISOString', (t) =>
30
31
type : 'string' ,
31
32
format : 'date-time'
32
33
}
33
- const toStringify = new Date ( )
34
+ const toStringify = new Date ( 1674263005800 )
34
35
35
36
const validate = validator ( schema )
36
37
const stringify = build ( schema )
@@ -49,7 +50,7 @@ test('render a nullable date in a string when format is date-format as ISOString
49
50
format : 'date-time' ,
50
51
nullable : true
51
52
}
52
- const toStringify = new Date ( )
53
+ const toStringify = new Date ( 1674263005800 )
53
54
54
55
const validate = validator ( schema )
55
56
const stringify = build ( schema )
@@ -67,13 +68,13 @@ test('render a date in a string when format is date as YYYY-MM-DD', (t) => {
67
68
type : 'string' ,
68
69
format : 'date'
69
70
}
70
- const toStringify = new Date ( )
71
+ const toStringify = new Date ( 1674263005800 )
71
72
72
73
const validate = validator ( schema )
73
74
const stringify = build ( schema )
74
75
const output = stringify ( toStringify )
75
76
76
- t . equal ( output , `" ${ DateTime . fromJSDate ( toStringify ) . toISODate ( ) } "` )
77
+ t . equal ( output , '"2023-01-21"' )
77
78
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
78
79
} )
79
80
@@ -86,13 +87,13 @@ test('render a nullable date in a string when format is date as YYYY-MM-DD', (t)
86
87
format : 'date' ,
87
88
nullable : true
88
89
}
89
- const toStringify = new Date ( )
90
+ const toStringify = new Date ( 1674263005800 )
90
91
91
92
const validate = validator ( schema )
92
93
const stringify = build ( schema )
93
94
const output = stringify ( toStringify )
94
95
95
- t . equal ( output , `" ${ DateTime . fromJSDate ( toStringify ) . toISODate ( ) } "` )
96
+ t . equal ( output , '"2023-01-21"' )
96
97
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
97
98
} )
98
99
@@ -110,7 +111,7 @@ test('verify padding for rendered date in a string when format is date', (t) =>
110
111
const stringify = build ( schema )
111
112
const output = stringify ( toStringify )
112
113
113
- t . equal ( output , `" ${ DateTime . fromJSDate ( toStringify ) . toISODate ( ) } "` )
114
+ t . equal ( output , '"2020-01-01"' )
114
115
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
115
116
} )
116
117
@@ -122,7 +123,7 @@ test('render a date in a string when format is time as kk:mm:ss', (t) => {
122
123
type : 'string' ,
123
124
format : 'time'
124
125
}
125
- const toStringify = new Date ( )
126
+ const toStringify = new Date ( 1674263005800 )
126
127
127
128
const validate = validator ( schema )
128
129
const stringify = build ( schema )
@@ -131,7 +132,7 @@ test('render a date in a string when format is time as kk:mm:ss', (t) => {
131
132
validate ( JSON . parse ( output ) )
132
133
t . equal ( validate . errors , null )
133
134
134
- t . equal ( output , `" ${ DateTime . fromJSDate ( toStringify ) . toFormat ( 'HH:mm:ss' ) } "` )
135
+ t . equal ( output , '"01:03:25"' )
135
136
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
136
137
} )
137
138
@@ -144,7 +145,7 @@ test('render a nullable date in a string when format is time as kk:mm:ss', (t) =
144
145
format : 'time' ,
145
146
nullable : true
146
147
}
147
- const toStringify = new Date ( )
148
+ const toStringify = new Date ( 1674263005800 )
148
149
149
150
const validate = validator ( schema )
150
151
const stringify = build ( schema )
@@ -153,7 +154,7 @@ test('render a nullable date in a string when format is time as kk:mm:ss', (t) =
153
154
validate ( JSON . parse ( output ) )
154
155
t . equal ( validate . errors , null )
155
156
156
- t . equal ( output , `" ${ DateTime . fromJSDate ( toStringify ) . toFormat ( 'HH:mm:ss' ) } "` )
157
+ t . equal ( output , '"01:03:25"' )
157
158
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
158
159
} )
159
160
@@ -165,7 +166,7 @@ test('render a midnight time', (t) => {
165
166
type : 'string' ,
166
167
format : 'time'
167
168
}
168
- const midnight = new Date ( new Date ( ) . setHours ( 24 ) )
169
+ const midnight = new Date ( new Date ( 1674263005800 ) . setHours ( 24 ) )
169
170
170
171
const validate = validator ( schema )
171
172
const stringify = build ( schema )
@@ -174,7 +175,7 @@ test('render a midnight time', (t) => {
174
175
validate ( JSON . parse ( output ) )
175
176
t . equal ( validate . errors , null )
176
177
177
- t . equal ( output , `" ${ DateTime . fromJSDate ( midnight ) . toFormat ( 'HH:mm:ss' ) } "` )
178
+ t . equal ( output , '"00:03:25"' )
178
179
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
179
180
} )
180
181
@@ -195,7 +196,7 @@ test('verify padding for rendered date in a string when format is time', (t) =>
195
196
validate ( JSON . parse ( output ) )
196
197
t . equal ( validate . errors , null )
197
198
198
- t . equal ( output , `" ${ DateTime . fromJSDate ( toStringify ) . toFormat ( 'HH:mm:ss' ) } "` )
199
+ t . equal ( output , '"01:01:01"' )
199
200
t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
200
201
} )
201
202
@@ -212,7 +213,7 @@ test('render a nested object in a string when type is date-format as ISOString',
212
213
}
213
214
}
214
215
}
215
- const toStringify = { date : new Date ( ) }
216
+ const toStringify = { date : new Date ( 1674263005800 ) }
216
217
217
218
const validate = validator ( schema )
218
219
const stringify = build ( schema )
@@ -384,11 +385,11 @@ test('serializing null value', t => {
384
385
]
385
386
}
386
387
387
- const date = new Date ( )
388
+ const date = new Date ( 1674263005800 )
388
389
const input = { updatedAt : date }
389
390
const { output } = serialize ( schema , input )
390
391
391
- t . equal ( output , JSON . stringify ( { updatedAt : DateTime . fromJSDate ( date ) . toFormat ( 'HH:mm:ss' ) } ) )
392
+ t . equal ( output , JSON . stringify ( { updatedAt : '01:03:25' } ) )
392
393
} )
393
394
394
395
t . test ( 'format::time, Date object' , t => {
@@ -403,10 +404,10 @@ test('serializing null value', t => {
403
404
]
404
405
}
405
406
406
- const date = new Date ( )
407
+ const date = new Date ( 1674263005800 )
407
408
const { output } = serialize ( schema , date )
408
409
409
- t . equal ( output , `" ${ DateTime . fromJSDate ( date ) . toFormat ( 'HH:mm:ss' ) } "` )
410
+ t . equal ( output , '"01:03:25"' )
410
411
} )
411
412
412
413
t . test ( 'format::time, Date object' , t => {
@@ -497,7 +498,7 @@ test('Validate Date object as string type', (t) => {
497
498
{ type : 'string' }
498
499
]
499
500
}
500
- const toStringify = new Date ( )
501
+ const toStringify = new Date ( 1674263005800 )
501
502
502
503
const stringify = build ( schema )
503
504
const output = stringify ( toStringify )
@@ -520,10 +521,10 @@ test('nullable date', (t) => {
520
521
521
522
const stringify = build ( schema )
522
523
523
- const data = new Date ( )
524
+ const data = new Date ( 1674263005800 )
524
525
const result = stringify ( data )
525
526
526
- t . same ( result , `" ${ DateTime . fromJSDate ( data ) . toISODate ( ) } "` )
527
+ t . same ( result , '"2023-01-21"' )
527
528
} )
528
529
529
530
test ( 'non-date format should not affect data serialization (issue #491)' , ( t ) => {
0 commit comments