0

Jest + SuperTest tests pass locally, but GitHub action CI has connect ECONNREFUSED error. This seems to be an error caused by not connecting to the server.

When testing locally, I always tested after connecting to the server through npm run start:dev. So, I want to be able to run the server in the CI stage to conduct tests, and then shut down the server.

However, when I type the command npm run start:dev && npm run test, CI doesn't finish for tens of minutes after the server starts.

How can I make my tests run as successfully in the CI stage as I did locally? I need help. The execution environment is node js.

The code to create a request variable and get the API call result is as follows.

const request = supertest("http://127.0.0.1:55503");

const response = await request.get("/test/a");

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
shinbian11
  • 41
  • 5
  • 1
    Run server in background, wait for it to start, and then run tests e.g. `npm run start:dev & && sleep 2s`. – Azeem Jun 16 '23 at 06:29
  • @Azeem What does the & after `npm run start:dev` mean? – shinbian11 Jun 16 '23 at 06:36
  • To run it in the background. See https://stackoverflow.com/questions/44222883/run-a-shell-script-and-immediately-background-it-however-keep-the-ability-to-in. You can find multiple threads on this on SO. – Azeem Jun 16 '23 at 06:40
  • 1
    Does this answer your question? [Run a shell script and immediately background it, however keep the ability to inspect its output](https://stackoverflow.com/questions/44222883/run-a-shell-script-and-immediately-background-it-however-keep-the-ability-to-in) – Azeem Jun 16 '23 at 06:41
  • 1
    @Azeem yes this is the answer i want. thank you. I did a test in the ci step via npm run start:dev & => sleep 2s => npm test. But I have one more question. I need to turn off the server after npm test. What command should I use at this time? Locally, you can use ctrl+c to turn it off, but I don't know what command to enter on ci. – shinbian11 Jun 16 '23 at 13:10
  • You need to kill its process. See https://unix.stackexchange.com/questions/104821/how-to-terminate-a-background-process – Azeem Jun 16 '23 at 13:29
  • @Azeem thank you very much. I have one last question, can I ask you? What happens when I don't shut down the server running in the background for testing in a CI situation? I wonder if github actions will turn it off automatically at some point, or if it will be forced to close when ci is finished. – shinbian11 Jun 17 '23 at 01:39
  • 1
    Each GHA workflow job runs in a different runner. As soon as a job is complete, its runner goes down i.e. your server will no longer be running. – Azeem Jun 17 '23 at 07:00
  • @Azeem To be honest, I'm asking because I made the mistake of not running the command to turn off the server after testing. I'm glad the server shuts down automatically. Thanks for your kind comments! – shinbian11 Jun 17 '23 at 13:20
  • @Azeem I faced another problem. Can you provide additional help? I've created a new question on this link below. It seems that there is a problem in the process of connecting the database. => https://stackoverflow.com/questions/76499472/jest-supertest-tests-has-error-connect-econnrefused-in-github-actions-ci – shinbian11 Jun 18 '23 at 07:37

1 Answers1

0
  • I had a very similar issue recently and I found out I was making a call to a 3rd party api in my test, to solve the issue I had to mock the api call using jest's mock functionalities. If you are not familiar with mocking third party api calls check out Link to jest docs.
cognito
  • 1
  • 1