w3resource

Migrating to Jest


Sometimes you might have started a project without having Jest as you preferred testing tool, what do you do start your project afresh? That is obviously not what you intend to do, this tutorial will guide you on how to migrate an existing codebase to use Jest, enjoy!

If you would like to try out Jest with an existing codebase, there are so many ways to convert to Jest:

  • In the case where you are using Jasmine, or you are using a Jasmine like API (for instance Mocha), in most cases Jest should be compatible and easy to migrate to.
  • In the case where you are using AVA, Expect.js (by Automattic), Jasmine, Mocha, proxyquire, Should.js or Tape you can migrate automatically with Jest Codemods (see the explanation and usage below).
  • If you prefer chai, you might choose to upgrade to Jest and continue using chai. However, we will recommend that you try out Jest's assertions and their failure messages. Jest Codemods can help you migrate from chai (see instructions below).

jest-codemods

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

If you need to transform your existing tests using jest-codemods, this instruction set will get you started:

Usage (CLI)

If you want to use the interactive CLI, you need to run

npx jest-codemods

If npx is not installed on your environment, install the jest-codemods command globally by running npm install -g jest-codemods.

For more options

npx jest-codemods --help

Codemods for migrating test files to Jest.

Usage

npx jest-codemods <path> [options]

    path	        Files or directory to transform. Can be a glob like src/**.test.js

Options

--force, -f	this will bypass Git safety checks and forcibly run codemods
      --dry, -d		this will do a dry run (no changes are made to files)

To transform all test files that are in a directory run jest-codemods . in your terminal.

Observe the console output for errors, manual intervention and tweaks may be required.

Usage (jscodeshift)

If you want to make the process as simple as possible, it is recommended that the jest-codemods CLI that wraps the jscodeshift executable. But you can run the transformations directly using jscodeshift also.

npm install -g jscodeshift```
```npm install jest-codemods```
```jscodeshift -t node_modules/jest-codemods/dist/transformers/ava.js test-folder```
 ```jscodeshift -t node_modules/jest-codemods/dist/transformers/chai-assert.js test-folder```
 ```jscodeshift -t node_modules/jest-codemods/dist/transformers/chai-should.js test-folder```
```jscodeshift -t node_modules/jest-codemods/dist/transformers/expect-js.js test-folder```
```jscodeshift -t node_modules/jest-codemods/dist/transformers/expect.js test-folder```
 ```jscodeshift -t node_modules/jest-codemods/dist/transformers/jasmine-globals.js test-folder```
```jscodeshift -t node_modules/jest-codemods/dist/transformers/jasmine-this.js test-folder```
```jscodeshift -t node_modules/jest-codemods/dist/transformers/mocha.js test-folder```
```jscodeshift -t node_modules/jest-codemods/dist/transformers/should.js test-folder```
```jscodeshift -t node_modules/jest-codemods/dist/transformers/tape.js test-folder

Test environment: Jest on Node.js or other

In the case where you are using Jest as your test runner and executing tests on Node.js, you may want to use the default option when prompted. In this case, jest-codemods will assume that global values such as expect and jest are provided and will not have to require() them explicitly.

If you are using a different test runner however or you are executing Jest tests in a browser, you may have to choose the option with explicit require() calls.

In the second case, after you run jest-codemods, you might have to install a few dependencies:

yarn add --dev expect jest-mock
npm install --save-dev expect jest-mock


Inviting useful, relevant, well-written and unique guest posts