Skip to content
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

test(calendar): refactor demo to show selected date in UTC, ensure consistent snapshot #2972

Closed

Conversation

Summer-Shen
Copy link
Contributor

Types of changes

  • New feature
  • Bug fix
  • Enhancement
  • Component style change
  • Typescript definition change
  • Documentation change
  • Coding style change
  • Refactoring
  • Test cases
  • Continuous integration
  • Breaking change
  • Others

Background and context

#2971

Solution

The Calendar demo test fails because if we use new Date('YYYY-MM-DD') to instantiate the Date object, we get a UTC time set at '00:00:00'. The object will give us the corresponding time in our system time zone when accessed. By using toISOString, we get the same representation wherever we run the tests.

Calendar demo 不通过的原因是,使用 new Date('YYYY-MM-DD') 实例化 Date 对象时,系统会自动设置一个时刻为 '00:00:00' 的 UTC 时间,并在用户访问时,返回系统时区的对应时间。通过使用 toISOString,无论在哪里运行测试,都能得到相同的表示。

How is the change tested?

Changelog

Component Changelog(CN) Changelog(EN) Related issues
calendar test(calendar): 重构 demo,以 UTC 显示所选日期,确保快照一致 test(calendar): refactor demo to show selected date in UTC, ensure consistent snapshot Close #2971

Checklist:

  • Test suite passes (npm run test)
  • Provide changelog for relevant changes (e.g. bug fixes and new features) if applicable.
  • Changes are submitted to the appropriate branch (e.g. features should be submitted to feature branch and others
    should be submitted to main branch)

Other information

Reference 参考

Copy link

codesandbox bot commented Feb 15, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

select: {{value}}
<a-space direction="vertical" fill>
<a-calendar v-model="value" />
<div>Selected date in UTC: {{ value.toISOString() }}</div>
Copy link
Member

@oljc oljc Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Summer-Shen 这样是可以保证这一处的快照一致,但没有根本上解决时间相关的测试问题。 在 Demo 中使用toISOString() 可能会增加用户理解成本。建议arco-vue-scripts 中优化 Jest 配置,统一处理日期时间模拟,以确保测试结果在不同环境下的一致性和可靠性。😊👍

Copy link
Contributor Author

@Summer-Shen Summer-Shen Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LIjiAngChen8 尝试了一些方案:

但是在我的 Windows 机器上似乎都不 work,我倾向于还是暂时使用之前修改的 toISOString,不知道您这边有什么想法,感谢!

Copy link
Contributor Author

@Summer-Shen Summer-Shen Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LIjiAngChen8 我还尝试了一下给这个测试用例单独加 MockDate,但是始终无法绕开不同机器显示时区不同的问题;如果配置 Jest,在 Windows 上想要暂时取代系统时区也非常困难。

Copy link
Contributor Author

@Summer-Shen Summer-Shen Feb 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我改了下 npm run test 的 script,加入了一个环境变量,已经实现了每个用户都能看到 local time,但测试发生在新加坡时区。不过现在 yarn.lock 这边好像有点问题,不太清楚原因。可能和 yarn 的 版本 有关,参考 #2841

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我本地重新 yarn install 之后,首先在 init 和测试过程中需要额外安装 @babel/helper-environment-visitor 和 @babel/helper-string-parser 才可以运行测试。

测试后,许多 snapshot 好像都和之前不是完全一样,有细微的差别。我应该更新这些 snapshot 吗?

Test Suites: 36 failed, 84 passed, 120 total
Tests:       187 failed, 501 passed, 688 total
Snapshots:   189 failed, 375 passed, 564 total

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LIjiAngChen8 尝试了一些方案:

但是在我的 Windows 机器上似乎都不 work,我倾向于还是暂时使用之前修改的 ,不知道您这边有什么想法,感谢!toISOString

变更 scripts 没生效的话有尝试重新 build 吗。jest 测试变动关联的内容比较多,因此建议将此项工作交由 maintainer 进行变动。
📌 按计划年后可能会对该仓库的 ci 进行一次完善优化。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LIjiAngChen8

变更 scripts 没生效的话有尝试重新 build 吗

没有;向 packages/arco-vue-scripts/src/configs/jest.config.ts 的 globals 增加 TZ 的方案应该在 Windows 上不可行,后续 向 package.json 引入 globalSetup 值得一试。

jest 测试变动关联的内容比较多,因此建议将此项工作交由 maintainer 进行变动。

了解,也感谢您对这个 PR 的关心和帮助🤝!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calendar demo snapshot fails, but is false positive
2 participants