使用Jest测试时导入失败

时间:2018-06-15 11:16:08

标签: javascript reactjs testing jestjs

所以我有React组件在浏览器中运行良好,但在测试时无法呈现。我正在使用Jest,有问题的组件是React Sortable的包装器。在尝试启动测试时,我得到了#34; _sortablejs2.default.create不是一个函数"错误,我发现这是由Sortable.js文件中的导入(即要求('反应'))返回' undefined'。为什么会这样?其他测试我工作得很好。

测试本身:

let renderer = TestRenderer.create(
      <DragAndDropSelector
        selected={[]}
        choices={[]}
        selectedChange={ (a, b, c) => {return a} }
      />
  );
  expect(renderer.toJSON()).toMatchSnapshot();
});

我的包json:

{
"name": "<Redacted/>",
"version": "0.1.0",
"private": true,
"dependencies": {
    "axios": "0.17.1",
    "babel-eslint": "8.2.3",
    "faker": "4.1.0",
    "file-saver": "1.3.8",
    "history": "4.7.2",
    "html-to-react": "1.3.3",
    "immutability-helper": "2.6.6",
    "lodash": "4.17.5",
    "moment": "2.21.0",
    "npm": "6.1.0",
    "npm-run-all": "4.1.2",
    "react": "16.2.0",
    "react-cookie": "2.1.4",
    "react-datepicker": "1.4.1",
    "react-dates": "16.6.1",
    "react-dom": "16.2.0",
    "react-redux": "5.0.7",
    "react-redux-toastr": "^7.3.0",
    "react-router": "4.2.0",
    "react-router-dom": "4.2.2",
    "react-scripts": "1.1.0",
    "react-sortablejs": "^1.3.6",
    "react-test-renderer": "16.2.0",
    "redux": "3.7.2",
    "redux-dehydrate": "0.0.2",
    "redux-logger": "3.0.6",
    "redux-persist": "5.6.5",
    "redux-thunk": "2.2.0",
    "semantic-ui": "2.3.1",
    "semantic-ui-react": "0.78.2",
    "sortablejs": "^1.7.0"
},
"scripts": {
    "build-css": "less-watch-compiler --run-once src src",
    "watch-css": "npm run build-css && less-watch-compiler src src",
    "start-js": "PORT=3000 react-scripts start --host 0.0.0.0",
    "start": "npm-run-all -p watch-css start-js",
    "build-js": "react-scripts build",
    "build": "npm-run-all build-css build-js",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
},
"devDependencies": {
    "less-watch-compiler": "1.11.0",
    "redux-mock-store": "1.5.1",
    "serve": "6.5.3"
},
"homepage__doc": "See VERSION in config/config.js, version should be the same",
"homepage": "/2.1/"

}

我很难过,所以任何帮助都会受到赞赏:)

编辑:根据请求,更详细的错误消息:

TypeError: _sortablejs2.default.create is not a function    
at Sortable.componentDidMount (/home/path/node_modules/react-sortablejs/lib/Sortable.js:120:50)
at commitLifeCycles (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:4553:24)
at commitAllLifeCycles (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:5729:9)
at Object.invokeGuardedCallback$1 (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:1559:10)
at invokeGuardedCallback (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:1506:29)
at commitRoot (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:5833:9)
at performWorkOnRoot (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6800:42)
at performWork (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6750:7)
at requestWork (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6661:7)
at scheduleWorkImpl (/home/path/node_modules/react-test-renderer/cjs/react-test-renderer.development.js:6515:11)

更多信息: 在react-sortablejs / lib / Sortable.js文件中,在行

var _sortablejs = require('sortablejs');

要求返回undefined。但是,我安装了sortablejs,我可以在节点模块中亲眼看到它。我甚至检查过它是在浏览器中渲染时输入的,但在运行测试时却没有。我很难过,所以任何帮助都会非常感激:)

0 个答案:

没有答案