在react-native子目录中运行Jest测试

时间:2018-07-09 10:28:26

标签: javascript reactjs react-native npm jestjs

我正在为react-native构建一个npm软件包,并试图包括Jest测试,但是似乎无法弄清楚我项目的文件结构,这导致了软件包冲突。

这是我的项目的文件夹结构(为简化我的问题已被简化):

 "
 /App
   /react-native-images-collage <- This is the package I am working on
      index.js
      package.json
      .git
 App.js <- This renders the 'react-native-images-collage' component 
 index.js <- Registers app component using AppRegistry
 package.json
 "

/react-native-images-collage目录已同时提交给GitHub和npmjs。这是/react-native-images-collage目录中的packjage.json:

{
  "name": "react-native-images-collage",
  "version": "3.0.3",
  "description": "Customizable image grid component for React Native",
  "repository": {
    "type": "git",
    "url":
      "git+https://github.com/LukeBrandonFarrell/react-native-images-collage.git"
  },
  "main": "src/index.js",
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:coverage": "jest --coverage",
    "flow": "flow",
    "flow-stop": "flow stop",
    "build": "flow-remove-types src/ -d lib/"
  },
  "peerDependencies": {
    "react": "^16.1.0",
    "react-native": "^0.54.0"
  },
  "author": "Luke Brandon Farrell",
  "license": "MIT",
  "readme": "README.md",
  "devDependencies": {
    "babel-jest": "23.2.0",
    "babel-preset-react-native": "4.0.0",
    "jest": "^23.3.0",
    "babel-eslint": "^8.2.5",
    "enzyme": "^3.3.0",
    "eslint": "^5.1.0",
    "eslint-plugin-react": "^7.10.0",
    "flow-bin": "^0.76.0",
    "husky": "^1.0.0-rc.13",
    "react-test-renderer": "^16.4.1"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm test",
      "pre-push": "npm test"
    }
  },
  "jest": {
    "preset": "react-native",
    "collectCoverage": true,
    "collectCoverageFrom": ["src/**/*.{js,jsx}"]
  },
  "dependencies": {}
}

所以这一切都很好,但是它要求我同时添加react-native和react作为开发依赖项,以便Jest运行。

/react-native-images-collage/package.json +

"devDependencies": {
   "react": "^16.1.0",
   "react-native": "^0.54.0"
   ...

现在npm test将在/react-native-images-collage目录中工作。

这是问题所在。当我在/App目录中运行“ react-native run-ios”以手动渲染和测试组件时,发生冲突,因为/App/package.json包括react和react-native作为依赖项,{{ 1}}作为开发依赖项。

如果我既要在设备上渲染组件以进行测试,又要使用Jest测试组件代码,我将无法删除。

可能我没有遵循健康的工作流程来构建npm组件?有什么选择?任何建议将不胜感激。

0 个答案:

没有答案