[Jest] Automate your migration to Jest using codemods

Jest is a fantastic testing library, but maybe you've been putting off the switch because migrating all of your existing tests from another library seems like a daunting task. With jest-codemods, you can automate most, if not all, of that migration! The jest-codemods CLI is a wrapper around jscodeshift that is specifically focused on helping you migrate from several popular test runner and assertion library combinations to Jest. In this lesson, we'll walk through converting a small sampling of tests created with Mocha, Sinon and Chai over to Jest. We'll even take a look at a couple edge cases where the conversion requires some manual intervention so you can be prepared for those in your own project.

 

Source code

 

Install:

npm i -D jest

npm i -g jest-codemons

 

Run:

jest-codemods  # default path is under src

 

 

There is a waring:

"

jest-codemods warning: (src/index.spec.js) Usage of package "sinon" might be incompatible with Jest

"

复制代码
  describe('once', () => {
    test('Only invokes the function once', () => {
      const fn = sinon.stub()
      const onceFn = once(fn)
      onceFn()
      onceFn()
      onceFn()
      onceFn()
      sinon.assert.calledOnce(fn)
    })
  })
复制代码

 

Change to:

复制代码
  describe('once', () => {
    test('Only invokes the function once', () => {
      const fn = jest.fn();
      const onceFn = once(fn)
      onceFn()
      onceFn()
      onceFn()
      onceFn()
      expect(fn).toHaveBeenCalledTimes(1);
    })
  })
复制代码

 

 

One failed test:

expect(result).to.eq(3)

 

Change to:

expect(result).toBe(3)

 

posted @   Zhentiw  阅读(336)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-08-22 [D3] Reuse Transitions in D3 v4
2016-08-22 [React] Styling React Components With Aphrodite
2016-08-22 [Canvas] Make Canvas Responsive to Pixel Ratio
2016-08-22 [RxJS] Introduction to RxJS Marble Testing
2015-08-22 [Node.js] Web Scraping with Pagination and Advanced Selectors
2014-08-22 [jQuery] Custom event trigger
2014-08-22 [jQuery] $.map, $.each, detach() , $.getJSOIN()
点击右上角即可分享
微信分享提示