Dev Collection
HomeGitHubAI Web
  • ReadMe
  • How to use patch-package
  • Add GCC for node-canvas in Centos7.x
  • Create Server-Sent Events in Nextjs
  • TimeZone in Jest
  • Common Git Commit Message Types
  • Node v8 memory leak
  • minIO + picGO self-hosted image hosting service
  • Preventing SSH remote brute-force attacks
  • Ubuntu安装Netflix-Proxy
  • SNIProxy+Dnsmasq实现Netflix-Proxy
  • Simultaneously Pushing a Repository to Two Different Remotes: GitHub and GitLab
  • Typescript中的类型谓词
  • Mac Sonoma 无法安装 Charles 的解决办法
  • Git 多账号管理:在不同文件夹区分Git账号
  • localStorage VS IndexedDB
  • Chrome Extension 开发指南
Powered by GitBook
On this page

TimeZone in Jest

PreviousCreate Server-Sent Events in NextjsNextCommon Git Commit Message Types

Last updated 1 year ago

jest 和 node 相同,在运行时没有时区的概念,导致某些场景的下和业务实际的场景不同。

要解决这个问题有两种处理方法:

在 jest 启动的时候设置环境变量 TZ

TZ=UTC jest --config=jest.config.js

或者在 jest.config.js 中配置 globalSetup

let config = {
    ... ...
    // A path to a module which exports an async function that is triggered once before all test suites
    globalSetup: "<rootDir>/global-setup.js",
    ... ...
}
module.exports = config

在根目录新建一个 global-setup.js

module.exports = async () => {
    process.env.TZ = "Asia/Shanghai";
};

这里的 UTC 和 Asia/Shanghai 需要遵循 UTC 标准写法,

最后我们可以在控制台打印看到,已经根据对应的时区显示了

点此查看规则