настройка lint-staged для шутки

Надо попробовать настроить хаски с lint-stage. Изначально я пытался настроить следующим образом, но это не сработало.

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

затем я после некоторого поиска изменил свои настройки на следующие, но снова сообщил об ошибке diff

"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. Я делаю что-то не так здесь?

мой пакет.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"
    ]
  }
}

person Carlos    schedule 19.03.2019    source источник


Ответы (2)


Ваш шаблон lint-staged glob настроен для работы только с *.js файлами в корневом каталоге. Вы должны изменить его на **/*.js, чтобы включить ВСЕ *.js файлы в ваш проект.

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

Использование приложения Create React означает, что вам нужно протестировать его с помощью реагирующих сценариев, потому что у них есть конфигурация babel, которой не хватает только шутке.

Существует обходной путь, чтобы установить husky lint-staged с помощью теста реагирования-скриптов и избежать извлечения и настройки собственного вавилона и шутки.

Используйте пакет cross-env, который позволит нам настроить переменную среды CI в любой среде, в которой мы сейчас находимся.

Установить с помощью:

yarn add --dev cross-env

or

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.

Надеюсь, поможет ;).

person Eliecer Chicott    schedule 02.02.2020
comment
Я получил это решение благодаря этой средней статье. повышение уровня. gitconnected.com/ - person Eliecer Chicott; 02.02.2020