2017-04-13 83 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