Skip to content

Commit c9985a0

Browse files
Copilotcharmander
andauthored
Assert exact serialized value and match warning by code
Co-authored-by: charmander <1889843+charmander@users.noreply.github.com>
1 parent e1042da commit c9985a0

2 files changed

Lines changed: 8 additions & 17 deletions

File tree

packages/pg/lib/utils.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict'
22

33
const defaults = require('./defaults')
4-
const nodeUtils = require('util')
4+
const { deprecate, types } = require('util')
55

6-
const { isDate } = require('util/types')
6+
const { isDate } = types
77

8-
const invalidDateDeprecationNotice = nodeUtils.deprecate(
8+
const invalidDateDeprecationNotice = deprecate(
99
() => {},
10-
'Sending an invalid date to Postgres is deprecated and will throw an error in the next major version of pg. Ensure any Date object passed as a query parameter is valid.'
10+
'Sending an invalid date to Postgres is deprecated and will throw an error in the next major version of pg. Ensure any Date object passed as a query parameter is valid.',
11+
'PG_INVALID_DATE'
1112
)
1213

1314
function escapeElement(elementRepresentation) {

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,20 @@ test('prepareValues: 1 BC date prepared properly', function () {
9292
test('prepareValues: invalid date emits deprecation warning', function () {
9393
const date = new Date(undefined)
9494

95-
// util.deprecate (used internally) only ever emits its warning once per process,
96-
// so build a promise that resolves as soon as we observe it, and race it against
97-
// a short timeout so the test fails fast instead of hanging if it's not emitted.
98-
const warningPromise = new Promise((resolve, reject) => {
95+
const warningPromise = new Promise((resolve) => {
9996
const onWarning = (warning) => {
100-
if (warning.name === 'DeprecationWarning' && /invalid date/i.test(warning.message)) {
101-
clearTimeout(timer)
97+
if (warning.code === 'PG_INVALID_DATE') {
10298
process.removeListener('warning', onWarning)
10399
resolve(warning)
104100
}
105101
}
106102
process.on('warning', onWarning)
107-
108-
const timer = setTimeout(() => {
109-
process.removeListener('warning', onWarning)
110-
reject(new Error('Expected a DeprecationWarning about invalid dates but none was emitted'))
111-
}, 500)
112103
})
113104

114105
const out = utils.prepareValue(date)
115106

116107
// still serializes (for backwards compatibility) but warns that this is deprecated
117-
assert.strictEqual(typeof out, 'string')
118-
assert.ok(out.includes('NaN'))
108+
assert.strictEqual(out, '0NaN-NaN-NaNTNaN:NaN:NaN.NaN+NaN:NaN')
119109

120110
return warningPromise
121111
})

0 commit comments

Comments
 (0)