2017-09-01 60 views
0

我的笑話測試錯誤,但我的應用程序transpiles(webpack)並運行沒有錯誤。我最近切換到了babel-preset-env,但我仍然得到相同的錯誤。 Stackoverflow不斷顯示「看起來你的文章主要是代碼;請添加一些更多細節。」babel,jest:TypeError:(0,_connector2.default)不是函數

相關的package.json:

"jest": "^20.0.4", 
"jest-fetch-mock": "^1.0.8", 
"babel-core": "^6.22.1", 
"babel-jest": "^20.0.3", 
"babel-preset-es2015": "^6.22.0", 
"babel-preset-stage-0": "^6.22.0", 

.babelrc

{ 
    "presets": ["es2015", "stage-0"], 
    "plugins": [ 
    ["inferno", {"imports": true}], 
    ["transform-es2015-classes", {"loose": true}] 
    ], 
    "env": { 
    "test": { 
     "presets": ["es2015", "stage-0"] 
    } 
    } 
} 

SRC/JS /模塊/ UI /組件/ UIObject類/ index.jsx高達線17

import TwitterComponent from './twitter'; 
import AdComponent, { networks } from './ad'; 
import RssComponent from './rss'; 
import WwwComponent from './www'; 
import connector from '../../connector'; 
const types = ['twitter', 'ads', 'rss', 'www']; 
const getTitleByType = (type, data) => { 
    const titles = { 
     twitter: data['data-widget-id'], 
     ad: `${networks[data.network]} (${data.key})`, 
     ads: `${networks[data.network]} (${data.key})`, 
     rss: data.url, 
     www: data.url 
    }; 
    return titles[type]; 
}; 
const Ad = connector(AdComponent, ['noSubmit', 'ads']); 
... 

../../connector.js

import { connect } from 'inferno-redux'; 
import { actions } from './reducers/main'; 
import { bindActionCreators } from 'redux'; 
import Immutable from 'seamless-immutable'; 
const connector = (component, keys = null) => { 
    const mapDispatchToProps = dispatch => { 
     return bindActionCreators(Object.assign({}, actions), dispatch); 
    }; 
    const mapStateToProps = state => { 
     const _state = {}; 
     const stateKeys = Object.keys(Immutable.asMutable(state.main)); 
     for (let key of stateKeys) { 
      if ((Array.isArray(keys) && keys.includes(key)) || keys === null) { 
       _state[key] = Immutable.getIn(state.main, [key]); 
      } 
     } 
     return _state; 
    }; 
    return connect(mapStateToProps, mapDispatchToProps)(component); 
}; 
export { connector }; 
export default connector; 

控制檯輸出:

FAIL src/__tests__/modules/ui/components/Main/index.spec.jsx 
    ● Test suite failed to run 

    TypeError: (0 , _connector2.default) is not a function 

     at Object.<anonymous> (src/js/modules/ui/components/UIObject/index.jsx:17:109) 
     at Object.<anonymous> (src/js/modules/ui/components/UIObjectsComponent/index.jsx:2:43) 
     at Object.<anonymous> (src/js/modules/ui/reducers/main.js:367:305) 
     at Object.<anonymous> (src/js/modules/ui/connector.js:2:39) 
     at Object.<anonymous> (src/js/modules/ui/components/Main/index.jsx:3:44) 
     at Object.<anonymous> (src/__tests__/modules/ui/components/Main/index.spec.jsx:3:13) 
      at <anonymous> 
     at process._tickCallback (internal/process/next_tick.js:188:7) 

Test Suites: 1 failed, 1 total 
Tests:  0 total 
Snapshots: 0 total 
Time:  14.381s 

參考:

github issue

回答

1

你在你的代碼循環依賴。你可以看到它的堆棧跟蹤:

at Object.<anonymous> (src/js/modules/ui/components/UIObject/index.jsx:17:109) 
at Object.<anonymous> (src/js/modules/ui/components/UIObjectsComponent/index.jsx:2:43) 
at Object.<anonymous> (src/js/modules/ui/reducers/main.js:367:305) 
at Object.<anonymous> (src/js/modules/ui/connector.js:2:39) 
at Object.<anonymous> (src/js/modules/ui/components/Main/index.jsx:3:44) 
at Object.<anonymous> (src/__tests__/modules/ui/components/Main/index.spec.jsx:3:13) 

從底部到頂部:

  • connector.js:2:39該文件被加載並運行

    import { actions } from './reducers/main'; 
    
  • 更多的文件負載

  • 更多的文件負載
  • UIObject/index.jsx:17:109此文件加載,然後運行

    import connector from '../../connector'; 
    
  • 但這種循環行不通。 connector.js停在第3步中運行,纔到了線

    const connector = (component, keys = null) => { 
    

    這意味着當它運行在步驟6進口,connector還不存在。

    在一個真實的環境ES6,這段代碼實際上拋出一個異常,就像如果你沒有

    connector() 
    let connector = ... 
    

    但巴貝爾目前不處理的情況下,這樣的價值只是顯示爲undefined

    您將不得不重做您的代碼,以免出現這種循環依賴關係。

    +0

    就是這樣!謝謝! – jduhls

    相關問題