1

我必须尝试使用​​ lint-staged 设置哈士奇。最初,我试图进行如下设置,但这不起作用。

"lint-staged": {
    "*.js": [
      "prettier --write",
      "eslint src/ --fix",
      "npm run test",
      "git add"
    ]
  }

然后我经过一番搜索后将我的设置更改为以下但那再次报告了差异错误

"lint-staged": {
    "*.js": [
      "prettier --write",
      "eslint src/ --fix",
      "jest --bail --findRelatedTests",
      "git add"
    ]
  }

错误描述

Jest encountered an unexpected token

  This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

  By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

  Here's what you can do:
   • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
   • If you need a custom transformation to specify a "transform" option in your config.
   • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

当我npm run test在终端运行时,它工作正常。那么为什么它不能与 `lint-staged. 我在这里做错了吗?

我的 package.json

{
  "name": "myproject",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.18.0",
    "react": "^16.8.4",
    "react-dom": "^16.8.4",
    "react-redux": "^6.0.1",
    "react-router-dom": "^5.0.0",
    "react-scripts": "2.1.8",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0",
    "standard-http-error": "^2.0.1",
    "styled-components": "^4.1.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-prettier": "^4.1.0",
    "eslint-plugin-import": "^2.16.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-prettier": "^3.0.1",
    "eslint-plugin-react": "^7.12.4",
    "husky": "^1.3.1",
    "install": "^0.12.2",
    "lint-staged": "^8.1.5",
    "prettier": "1.16.4"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.js": [
      "prettier --write",
      "eslint src/ --fix",
      "jest --bail --findRelatedTests",
      "git add"
    ]
  }
}
4

2 回答 2

9

您的lint-stagedglob 模式仅设置为*.js在根目录上的文件上运行。您必须将其更改**/*.js为包含项目中的所有*.js文件。

"lint-staged": {
  "**/*.js": [
    "prettier --write",
    "eslint src/ --fix",
    "jest --bail --findRelatedTests",
    "git add"
  ]
}
于 2019-05-13T06:33:51.037 回答
1

使用 Create React App 意味着您必须使用 react-scripts 进行测试,因为它们具有仅 jest 所缺少的 babel 配置。

有一种解决方法可以使用 react-scripts 测试设置 husky lint-staged 并避免弹出和配置您自己的 babel 和 jest。

使用包cross-env将允许我们在我们当前所处的任何环境中配置 CI 环境变量。

使用安装:

yarn add --dev cross-env

或者

npm install cross-env --save-dev

然后在你的 package.json 你应该有这样的东西:

{
   "scripts" {
     ...,
     "lint": "eslint src --fix"
     "test": "cross-env CI=true react-scripts test --env=jsdom",
   },
   "husky": {
     "hooks": {
       "pre-commit": "lint-staged"
     }
   },
   "lint-staged": {
     "**/*.js": [
       "yarn lint",
       "yarn test"
     ]
   },
}

如果您使用的是npm,您可以将yarn test更改为npm run test,同样适用于lint

希望能帮助到你 ;)。

于 2020-02-02T16:24:46.870 回答