$u value is removed when modifying a time zone-aware dayjs object
- Dominant language
- JavaScript
- Stars
- 48.7k
- Forks
- 2.5k
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug**
Dayjs objects created by modifying an existing time zone-aware object using `.hour()` (or any other setter) do not have the `$u` field. This causes Jest's `toEqual` assertion to fail even though both objects point to the same moment in time and have the same time zone offset.
```typescript
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import customParseFormat from "dayjs/plugin/customParseFormat";
dayjs.extend(utc);
dayjs.extend(timezone);
// Uncomment to make the test pass
// dayjs.extend(customParseFormat);
describe("Dayjs", () => {
it("$u information is lost after setting time", () => {
const someDate = dayjs().tz('Europe/London');
// Use the setter to create a new dayjs object with the same hour
// Before running the test set the value to the current time in London
const someDateWithHourSpecifiedUsingSetter = someDate.hour(20);
expect(someDate).toEqual(someDateWithHourSpecifiedUsingSetter);
});
});
```
Result:
**Expected behavior**
The test should pass no matter if `dayjs` was extended with `customParseFormat`.
**Information**
- Day.js Version: v1.11.3
- OS: macOS
- ~Browser:~ Node: v16
- Time zone: Europe/Berlin (UTC+2)
Contributor guide
Research direction
Start with the timezone and utc plugins and trace how the `.hour()` setter creates a new object from `dayjs().tz('Europe/London')`. Reproduce the Jest `toEqual` case with and without `customParseFormat`; done means setter-created objects retain the `$u` information and the assertion passes in both configurations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100