2016-11-10 106 views
6

當我嘗試使用笑話測試我的反應本機應用程序時,出現奇怪的導入錯誤。我有babel-jest,babel-preset-react-native,jest,jest-react-native和react-test-render都安裝了,但是當我運行npm測試時得到這個錯誤信息。意外的導入令牌 - 測試React Native與Jest

●測試套件運行失敗

/Users/maftalion/www/stars20/kiosk/node_modules/native-base/index.js:4 
import Drawer from './Components/vendor/react-native-drawer'; 
^^^^^^ 
**SyntaxError: Unexpected token import** 

    at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:316:10) 
    at Object.<anonymous> (src/routes/Identification.js:3:17) 
    at Object.<anonymous> (src/routes/router.js:4:21) 

測試套件:1失敗,1過去了,2個總 測試:1後,1總 花絮:1後,1周總 時間:1.011s

回答

5

想通了,基本上拋出了在transformIgnorePatterns中使用es6語法的任何節點模塊。

「transformIgnorePatterns」:[ 「node_modules /(?反應天然|天然基|反應克隆引用元素)」 ],

+2

有關此解決方法的更多上下文:https://github.com/GeekyAnts/NativeBase/issues/396#issuecomment -270116326 – dadads

1

嘗試增加transformIgnorePatterns到您的package.json:

{ 
    "name": "MyAwesomeApp", 
    ... 
    "jest": { 
    "transformIgnorePatterns": ["/node_modules/"] 
    } 
} 

對我玩笑開箱的然而:

$ react-native init MyAwesomeApp 
$ cd MyAwesomeApp 
$ npm test 
... 
Tests:  2 passed 

我使用本機作出反應v0.37.0。

+0

是的,當我創建一個新的RN應用程序jest也工作得很好,不知道爲什麼它給我的問題。 transformIgnorePatterns使我的測試都失敗 –

+0

將它設置爲「/ node_modules /」將不起作用,因爲它指示Jest不能傳輸所有第三方庫。這也是默認的配置值,所以它不會改變任何東西。 請參閱https://facebook.github.io/jest/docs/configuration.html#transformignorepatterns-array-string – dadads

0

我發現前面給出的答案沒有解決Unexpected token import錯誤在測試中本身,如果它們是用ES6編寫的(例如ignite new MyProject之後由Ignite CLI創建的樣板測試)。

我終於設法通過向test任務package.json刪除ES6相關的錯誤:

"test": "NODE_ENV=test jest --no-cache" 

我使用RN 0.45.1,節點7.10.1(和8.1.2),紗0.24.6和玩笑20.0.4

PS:不要總是在控制檯中看到這個,但在VS代碼v1.13.1設置--no-cache有所作爲。