node.js - 更新到 npm 3.3.12 后使用 Jest 进行单元测试时出错

标签 node.js reactjs npm jestjs

我最近将我的项目更新到了 Node 5.1.0、npm 3.3.12 并更新了我的所有依赖项...现在我正在与一堆错误作斗争!

最初我在这个问题上遇到了同样的错误:https://github.com/facebook/jest/issues/554 ,这个问题很容易解决。

但是现在我得到了这个:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components).

Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

我的 package.json 中有这个:

"jest": {
    "collectCoverage": true,
    "testRunner": "./node_modules/jest-cli/src/testRunners/jasmine/jasmine2",
    "scriptPreprocessor": "./node_modules/babel-jest",
    "unmockedModulePathPatterns": [
        "./node_modules/es6-promise",
        "./node_modules/events",
        "./node_modules/fbjs",
        "./node_modules/flux",
        "./node_modules/history",
        "./node_modules/immutable",
        "./node_modules/lodash",
        "./node_modules/moment",
        "./node_modules/react",
        "./node_modules/react-addons-pure-render-mixin",
        "./node_modules/react-addons-test-utils",
        "./node_modules/react-d3",
        "./node_modules/react-dom",
        "./node_modules/react-modal",
        "./node_modules/react-router",
        "./node_modules/whatwg-fetch"
    ]
  }

我的测试的相关部分是:

var ImageGallery = require('../../../app/components/image-gallery/image-gallery');
imageGalleryComponent = TestUtils.renderIntoDocument(
    <ImageGallery
        items={items}
        onSlide={onSlideMock}
        lazyLoad={false}
        showBullets={true}
        showThumbnails={true}
        showNav={true}
        slideInterval={4000}/>
);

有人遇到过这个问题吗?有什么解决办法的线索吗?

最佳答案

在我的代码中,我将 babel 与“imports”一起使用,所以问题是在测试中我也需要使用“imports”,或者只是在 require 语句的末尾添加一个“.default”,例如这个:

var ImageGallery = require('../../../app/components/image-gallery/image-gallery').default;

关于node.js - 更新到 npm 3.3.12 后使用 Jest 进行单元测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33976917/

相关文章:

reactjs - 如何在 React 和 MUI 中使用 useStyles() 覆盖标签

javascript - Globbing 无法与带有 jsx 文件的 gulp eslint 一起使用

javascript - React + TS - 通过重定向传递参数

javascript - 在 Node.js 中输入 pincode 值时需要获取国家、州、城市和地区

npm - 最小化漏洞

javascript - Node.js NPM 和 Typescript 无法从测试应用程序访问类函数

git - 可以从 node.js 脚本中压缩 git 提交吗?

javascript - Bluebird 的 Promise.settle 未使用正确的值解析

node.js - npm:如何管理依赖项?

node.js - 在 root 模式下设置 Node-red 密码