2017-04-13 65 views
0

我有Customer.js.flow类型的文件。babel-jest转换非.js | .jsx扩展

当我运行的笑话,它失败,此错误:

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) 

即使我明确地说:

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

,当我改变Customer.js.flowCustomer.js我没有问题了

回答

0

问题是,当在您的笑话设置中使用transform时,它会覆盖默认值。 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

所以,你需要的匹配为.js的文件。

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

我不像正则表达式那样好,但是这个原因可以简化成一个语句。

+0

更新我的问题,这不是这个。我原来的解决方案是在这里:https://github.com/facebook/jest/issues/2152#issuecomment-262485964 所以我写了我自己的自定义转换器:https://github.com/MayasHaddad/flow-jest -变压器 – Mayas