/ Javascript

Running Jest tests with Flow typed Javascript files

This will be a simple tutorial about running Jest tests with Flow typed Javascript files in NodeJS project.

Problem

Recently I've started adding Flow types to my existing NodeJS project which uses Jest testing framework. It turns out immidiately that there is no "out of the box" support for Flow in Jest. This is strange for me because both are Facebook products, so I was hoping for seamless integration.

Solution

There is a quite simple solution which is based on two Babel originated packages: babel-jest and babel-plugin-transform-flow-strip-types.
Install them with:

yarn add --dev babel-jest babel-plugin-transform-flow-strip-types

Then adjust configuration inside .babelrc file as described on babel-plugin-transform-flow-strip-types NPM repository page:

{
  "plugins": ["transform-flow-strip-types"]
}

Now after executing yarn jest all my Flow type hinted JS files works as expected.

No jest.config.js modification required!

Issues

While trying to get Jest tests to work with Flow I've got one problem - a dead end. It was jest-flow-transform package. Despite declaring approperiate configuration inside jest.config.js file in transform section I was constantly getting a message:

TypeError: ...mockResolvedValue is not a function

I didn't find any solution for that and move on to babel-jest which is described above in a solution section.

Create React App (CRA) Jest tests

If you are using Create React App and Jest tests reports a lot of Flow bugs just install jest directly with:

yarn flow-typed install [email protected]

You should replace 20.0.4 with whichever Jest version is currently used by CRA (check it with yarn list | grep jest). Unfortunatelly, there seems to be no way to automatically install Flow definitions for Jest. See CRA #3904 for more info.

Running Jest tests with Flow typed Javascript files
Share this