简体   繁体   English

babel,jest:TypeError:(0,_connector2.default)不是函数

[英]babel, jest: TypeError: (0 , _connector2.default) is not a function

My jest test errors out but my app transpiles (webpack) and runs without error. 我的开玩笑测试错误,但我的应用程序转换(webpack)并运行没有错误。 I did recently switch to babel-preset-env but I still get the same error. 我最近切换到babel-preset-env,但我仍然得到同样的错误。

relevant package.json: 相关的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 .babelrc

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

src/js/modules/ui/components/UIObject/index.jsx up to line 17 src / js / modules / ui / components / 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 ../../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;

console output: 控制台输出:

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

For reference: 以供参考:

github issue github问题

You have a circular dependency in your code. 您的代码中存在循环依赖关系。 You can see it in the stacktrace: 你可以在stacktrace中看到它:

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)

From the bottom to the top: 从下到上:

  1. connector.js:2:39 this file is loading, and runs connector.js:2:39这个文件正在加载,并运行

     import { actions } from './reducers/main'; 
  2. More files load 加载更多文件

  3. More files load 加载更多文件
  4. UIObject/index.jsx:17:109 this file is loading, then runs UIObject/index.jsx:17:109此文件正在加载,然后运行

     import connector from '../../connector'; 

But this cycle is not going to work. 但这个循环不会起作用。 connector.js stopped running on step 3, before it got to the line connector.js在到达该行之前在步骤3上停止运行

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

meaning that when it runs the import in step 6 , connector doesn't exist yet. 意味着当它在步骤6运行导入时, connector尚不存在。

In a real ES6 environment, this code would actually throw an exception like if you did 在真正的ES6环境中,这段代码实际上会抛出异常,就像你做的那样

connector()
let connector = ...

but Babel doesn't handle those cases currently, so the value just shows up as undefined . 但是Babel目前没有处理这些情况,因此该值只显示为undefined

You'll have to rework your code such that this cyclic dependency is not present. 您必须重新编写代码,以便不存在此循环依赖关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM