Create-react-app: How can i run tests before commit using lint-staged and react-scripts?

Created on 23 May 2017  路  5Comments  路  Source: facebook/create-react-app

Curently have

  "devDependencies": {
    "husky": "^0.13.3",
    "lint-staged": "^3.4.2",
    "prettier": "^1.3.1",
    "react-scripts": "1.0.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject",
    "precommit": "lint-staged"
  },
  "lint-staged": {
    "src/**/*.js": [
      "prettier",
      "git add"
    ]
  }
}

Most helpful comment

@spacecrio this is how I implement it

"scripts": {
  "precommit": "lint-staged",
  "test:staged": "cross-env CI=true react-scripts test --env=jsdom --findRelatedTests"
}

then

"lint-staged": {
  "src/**/*.js": [
    "test:staged",
    "git add"
  ]
}

for the env variable, use cross-env.. it is cross platform

hope it helps!

All 5 comments

"precommit": "lint-staged && set CI=true&&npm test" work, but it looks like hack and it platform specific

@spacecrio this is how I implement it

"scripts": {
  "precommit": "lint-staged",
  "test:staged": "cross-env CI=true react-scripts test --env=jsdom --findRelatedTests"
}

then

"lint-staged": {
  "src/**/*.js": [
    "test:staged",
    "git add"
  ]
}

for the env variable, use cross-env.. it is cross platform

hope it helps!

@luftywiranda13 its work, thank you

But it would be great to be able to just run all tests like run "$ yarn jest" in the project created by the creat-reaction-app without env CI=true

without passing CI=true jest will automatically run in watch mode by default.
so it's not suitable for testing staged files.

IMO until this time, doing like what I wrote in the comment before is perfectly fine.
Also by passing --findRelatedTestswill make things faster because jest (under the hood) will find related tests and run selected tests based on files which are currently in staging area.

Thx for this guys, very helpful.

Was this page helpful?
0 / 5 - 0 ratings