0

I have Customer.js.flow type file.

When I run jest, it fails with this error:

Customer.js.flow:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export type Customer = {
                                                                                         ^^^^^^
SyntaxError: Unexpected token export

  at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)

Even if I explicitly added:

"transform": {
  "^.+\\.js.flow$": "babel-jest",
  "^.+\\.jsx?$": "babel-jest"
},

and when I change Customer.js.flow to Customer.js I don't have the issue anymore

Mayas
  • 1,434
  • 5
  • 16
  • 25

1 Answers1

0

The problem is that when using transform in your jest settings, it will overwrite the default. From the docs:

Note: if you are using the babel-jest transformer and want to use an additional code preprocessor, keep in mind that when "transform" is overwritten in any way the babel-jest is not loaded automatically anymore. If you want to use it to compile JavaScript code it has to be explicitly defined. See babel-jest plugin

So you need to as the matcher for .js files as well.

"transform": {
  "^.+\\.js\\.flow$": "babel-jest",
  "^.+\\.jsx?$": "babel-jest"
},

I'm not that good as regex but this of cause can be simplified into one statement.

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
  • Updated my question, it wasn't this. I turns out solution is here: https://github.com/facebook/jest/issues/2152#issuecomment-262485964 So I wrote my own custom-transformer here: https://github.com/MayasHaddad/flow-jest-transformer – Mayas Apr 14 '17 at 19:17