-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Date detection doesn't work for Dates from other realms #441
Comments
I reproduce your setup in demo/issues-esm/lib/441.js. The result corresponds to the expectations unless there is something that I did not understand from your description. |
Interesting. I wonder if this is specific to running inside of Jest. That's where I found this issue in testing. Let me see if I can repro stand alone. |
Ok, confirmed. Looks to be an issue with running in Test /* eslint-disable mocha/no-mocha-arrows */
const { stringify } = require("csv-stringify/sync");
const { Database } = require("duckdb-async");
const assert = require("node:assert");
test("441", async () => {
const db = await Database.create(":memory:");
const t = await db.all("SELECT DATE '2022-01-01' as dt");
// expect: 1640995200000
console.warn(stringify(t));
// actual: """2022-01-01T00:00:00.000Z"""
// expect: 1640995200000
console.warn(stringify(t, { cast: { date: (x) => x.getTime().toString() } }));
// actual: """2022-01-01T00:00:00.000Z"""
// expect: 1640995200000
console.warn(stringify(t, { cast: { object: (x) => x.getTime().toString() } }));
// Validate that duckdb returns as an instance of JS date
assert(t[0].dt instanceof Date);
// Validate that date objects are handled by `cast.date`
assert.equal(stringify(t, { cast: { date: () => "ok" } }).trim(), "ok");
// First assertion raised in the issue
assert.equal(stringify(t), 1640995200000);
// Second assertion raised in the issue
assert.equal(
stringify(t, { cast: { date: (x) => x.getTime().toString() } }).trim(),
1640995200000,
);
}); Output
|
You could also remove duckdb from your test to simplify the code and make sure does not influence the behavior. |
Duckdb is required for the repro. If I update to the following everything passes: const t = [{ dt: new Date("2022-01-01") }];
// expect: 1640995200000
console.warn(stringify(t));
// actual: """2022-01-01T00:00:00.000Z"""
// expect: 1640995200000
console.warn(stringify(t, { cast: { date: (x) => x.getTime().toString() } }));
... |
Describe the bug
await db.all("SELECT DATE '2022-01-01' as dt")
Expected:
Actual:
To Reproduce
Additional context
Some discussion in this stack overflow answer: https://stackoverflow.com/questions/643782/how-to-check-whether-an-object-is-a-date
Looks like the issue is that
instanceof Date
only works within the same frame/realm.date-fns handles it this way.
The text was updated successfully, but these errors were encountered: