Skip to main content
Version: 29.7

迁移到 Jest

如果你想使用现有代码库尝试 Jest,可以通过多种方法转换为 Jest:

¥If you'd like to try out Jest with an existing codebase, there are a number of ways to convert to Jest:

  • 如果你使用 Jasmine 或类似 Jasmine 的 API(例如 Mocha),Jest 应该大部分兼容,这使得迁移变得不那么复杂。

    ¥If you are using Jasmine, or a Jasmine like API (for example Mocha), Jest should be mostly compatible, which makes it less complicated to migrate to.

  • 如果你使用 AVA、Expect.js(Automattic 出品)、Jasmine、Mocha、proxyquire、Should.js 或 Tape,你可以使用 Jest Codemods 自动迁移(见下文)。

    ¥If you are using AVA, Expect.js (by Automattic), Jasmine, Mocha, proxyquire, Should.js or Tape you can automatically migrate with Jest Codemods (see below).

  • 如果你喜欢 chai,你可以升级到 Jest 并继续使用 chai。但是,我们建议尝试 Jest 的断言及其失败消息。Jest Codemods 可以从 chai 迁移(见下文)。

    ¥If you like chai, you can upgrade to Jest and continue using chai. However, we recommend trying out Jest's assertions and their failure messages. Jest Codemods can migrate from chai (see below).

jest-codemods

如果你使用的是 AVAExpect.js(由 Automattic 提供)JasmineMochaproxyquireShould.jsTapeSinon,则可以使用第三方 jest-codemods 来完成大部分脏迁移工作。它使用 jscodeshift 在你的代码库上运行代码转换。

¥If you are using AVA, Chai, Expect.js (by Automattic), Jasmine, Mocha, proxyquire, Should.js, Tape, or Sinon you can use the third-party jest-codemods to do most of the dirty migration work. It runs a code transformation on your codebase using jscodeshift.

要转换现有测试,请导航到包含测试的项目并运行:

¥To transform your existing tests, navigate to the project containing the tests and run:

npx jest-codemods

更多信息请参见 https://github.com/skovhus/jest-codemods

¥More information can be found at https://github.com/skovhus/jest-codemods.