bahmutov / start-server-and-test

Starts server, waits for URL, then runs test command; when the tests end, shuts down server
MIT License
1.53k stars 93 forks source link

start-server-and-test is incompatible with husky #285

Open mvasin opened 3 years ago

mvasin commented 3 years ago

Is this a bug report or a feature request?

If this is a bug report, please provide as much info as possible

I test a create-react-app-based app.

With this code in husky.config.js

module.exports = {
  hooks: {
    'pre-push': 'yarn start-server-and-test start http-get://localhost:3000 cy:run'
  }
}

I expect to run Cypress tests on git push.

Error: server closed unexpectedly at ChildProcess.onClose (/Users/foo/project/node_modules/start-server-and-test/src/index.js:69:14) at ChildProcess.emit (events.js:314:20) at maybeClose (internal/child_process.js:1047:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5) error Command failed with exit code 1.



Fun fact: `'pre-push': 'sleep 10'` does wait for 10 seconds before pushing to a git server. And `time yarn start-server-and-test start http-get://localhost:3000 cy:run` also confirms that the command works on its own and doesn't return to the terminal until the tests are done. But husky and start-server-and-test together somehow don't work. I also tried `'pre-push': 'bash -c "yarn start-server-and-test start http-get://localhost:3000 cy:run"'` - that changed nothing.
bcamarneiro commented 3 years ago

Did you find any solution for this?

mvasin commented 3 years ago

@bcamarneiro unfortunately not.

marek-siemieniuk-morawski commented 3 years ago

Hey, I had the same problem and I solved it. I'm not sure what's the root issue, but in my package.json there was:

"husky": {
    "hooks": {
      "pre-push": "yarn run test:cy -- --env configFile=local"
    }
},

and after removing -- --env configFile=local it started working.

Full package.json content:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {},
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "cy:run": "cypress run",
    "test:cy": "start-server-and-test start http://localhost:3000 cy:run"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ]
  },
  "husky": {
    "hooks": {
      "pre-push": "yarn run test:cy"
    }
  },
  "devDependencies": {
    "@types/fs-extra": "^9.0.6",
    "start-server-and-test": "^1.11.7"
  }
}

hopefully I helped