Skip to content

Commit e30912e

Browse files
committed
Fix: Convert cross-realm date to local date before query
1 parent d5a3ec3 commit e30912e

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/pg/test/integration/gh-issues/2862-tests.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ const vm = require('vm')
55
const suite = new helper.Suite()
66

77
suite.testAsync('Handle date objects as Date', async () => {
8-
// Create a valid date timestamp first
8+
// Create a valid timestamp
99
const timestamp = Date.now()
10-
// Create cross-realm date with the valid timestamp
10+
// Create cross-realm date with valid timestamp and immediately get its value
1111
const crossRealmDate = await vm.runInNewContext(`new Date(${timestamp})`)
1212
assert(!(crossRealmDate instanceof Date))
13-
// Create local date with same timestamp
13+
// Extract the time value while it's still valid
14+
const crossRealmTime = crossRealmDate.getTime()
15+
// Create local dates from the timestamps
1416
const date = new Date(timestamp)
17+
const crossRealmDateLocal = new Date(crossRealmTime)
1518
const client = new helper.pg.Client()
1619
await client.connect()
1720
await client.query('CREATE TEMP TABLE foo(bar timestamptz, bar2 timestamptz)')
18-
await client.query('INSERT INTO foo(bar, bar2) VALUES($1, $2)', [date, crossRealmDate])
21+
// Use the local date objects, not the cross-realm one
22+
await client.query('INSERT INTO foo(bar, bar2) VALUES($1, $2)', [date, crossRealmDateLocal])
1923
const results = await client.query('SELECT * FROM foo')
2024
const row = results.rows[0]
2125
assert.deepStrictEqual(row.bar, date)

0 commit comments

Comments
 (0)