Skip to content

Commit 765bab2

Browse files
committed
Fix: Remove semicolons and add tests
1 parent be504a6 commit 765bab2

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

packages/pg/lib/utils.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ function dateToString(date) {
9797
// Invalid dates can occur from new Date(undefined), new Date(NaN), etc.
9898
// See https://github.com/brianc/node-postgres/issues/3318
9999
if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
100-
throw new TypeError('Cannot serialize invalid date');
100+
throw new TypeError('Cannot serialize invalid date')
101101
}
102-
103102
let offset = -date.getTimezoneOffset()
104103
let year = date.getFullYear()
105104
const isBCYear = year < 1
@@ -137,7 +136,7 @@ function dateToStringUTC(date) {
137136
// Invalid dates can occur from new Date(undefined), new Date(NaN), etc.
138137
// See https://github.com/brianc/node-postgres/issues/3318
139138
if (!date || !(date instanceof Date) || isNaN(date.getTime())) {
140-
throw new TypeError('Cannot serialize invalid date');
139+
throw new TypeError('Cannot serialize invalid date')
141140
}
142141

143142
let year = date.getUTCFullYear()

packages/pg/test/unit/utils-tests.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,28 @@ testEscapeIdentifier(
314314
'hello \\ \' " world',
315315
'"hello \\ \' "" world"'
316316
)
317+
318+
test('prepareValue: invalid date from undefined throws error', function () {
319+
assert.throws(() => {
320+
utils.prepareValue(new Date(undefined))
321+
}, /Cannot serialize invalid date/)
322+
})
323+
324+
test('prepareValue: invalid date from NaN throws error', function () {
325+
assert.throws(() => {
326+
utils.prepareValue(new Date(NaN))
327+
}, /Cannot serialize invalid date/)
328+
})
329+
330+
test('prepareValue: invalid date from invalid string throws error', function () {
331+
assert.throws(() => {
332+
utils.prepareValue(new Date('invalid'))
333+
}, /Cannot serialize invalid date/)
334+
})
335+
336+
test('prepareValue: valid date still works correctly', function () {
337+
const date = new Date('2024-01-01T12:00:00Z')
338+
const result = utils.prepareValue(date)
339+
assert(typeof result === 'string')
340+
assert(result.includes('2024'))
341+
})

0 commit comments

Comments
 (0)