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.

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:

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.