反应16不适用于酶

时间:2018-01-04 16:57:51

标签: node.js reactjs enzyme

我使用React和酶库进行测试。 我迁移到React 16并尝试运行我的测试。

这是我的适配器文件:

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

这是我的package.json文件:

{
  "name": "some-app",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "NODE_ENV=test NODE_PATH=src/ jest --verbose",
    "lint": "eslint .",
    "lint:fix": "npm run lint -- --fix",
    "precommit": "npm run lint -s"
  },
  "dependencies": {
    "react": "^16.2.0",
    "react-datepicker": "0.55.0",
    "react-dom": "^16.2.0",
    "react-fontawesome": "1.6.1",
    "react-google-maps": "8.3.0",
    "react-modal": "2.3.2",
    "react-paginate": "^5.0.0",
    "react-redux": "5.0.6",
    "react-router-dom": "4.2.2",
    "react-router-redux": "4.0.8",
    "react-scripts": "1.0.12",
    "react-tabs": "2.0.0",
    "react-toastify": "2.1.6",
    "recompose": "0.25.0",
    "redux": "3.7.2",
    "redux-form": "7.0.3",
    "redux-thunk": "2.2.0"
  },
  "devDependencies": {
    "babel-cli": "6.26.0",
    "babel-plugin-transform-es2015-modules-commonjs": "6.26.0",
    "babel-preset-stage-0": "6.24.1",
    "enzyme": "^2.9.1",
    "enzyme-adapter-react-16": "^1.1.1",
    "husky": "0.14.3",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^22.0.4",
    "jest-cli": "^22.0.4",
    "jest-css-modules": "1.1.0",
    "react-test-renderer": "15.6.1",
    "redux-devtools-extension": "2.13.2"
  },
  "jest": {
    "testURL": "http://localhost/",
    "transform": {
      ".*": "<rootDir>/node_modules/jest-css-modules"
    },
    "setupTestFrameworkScriptFile": "<rootDir>/src/enzyme-setup-file.js",
    "moduleNameMapper": {
      "\\.(css|less)$": "identity-obj-proxy"
    }
  }
}

当我尝试运行npm test时,出现错误:react-addons-test-utils is an implicit dependency in order to support react@0.13-14. Please add the appropriate version to your devDependencies. See https://github.com/airbnb/enzyme#installation

我尝试设置另一个版本的react-addons-test-utils,但它没有帮助。我尝试导入时出现问题:

import Enzyme from 'enzyme';

有人知道怎么解决吗?

2 个答案:

答案 0 :(得分:3)

您正在使用酶版本2.x。要使用React16,请迁移到酶版本3.x

答案 1 :(得分:0)

以下是关于如何使用酶与反应16的enzyme docs