start-server-and-test icon indicating copy to clipboard operation
start-server-and-test copied to clipboard

Timed out waiting for a url

Open RosyCloudsLee opened this issue 1 year ago • 5 comments

Environment: start-server-and-test:2.0.3 node: 20.9.0 pnpm: 8.9.0 npm: 10.1.0 os: ubuntu 20.04

In package.json, "test:e2e": "start-server-and-test preview http://localhost:4173 'cypress run --e2e --browser firefox'"

Run pnpm test:e2e,show

1: starting server using command "npm run preview"
and when url "[ 'http://localhost:4173' ]" is responding with HTTP status code 200
running tests using command "cypress run --e2e --browser firefox"

 [email protected] preview
 vite preview

  ➜  Local:   http://localhost:4173/

Error: Timed out waiting for: http://localhost:4173
    at /home/xx/Documents/work/xx-yy/node_modules/.pnpm/[email protected][email protected]/node_modules/wait-on/lib/wait-on.js:131:31
    at doInnerSub (/home/xx/Documents/work/xx-yy/node_modules/.pnpm/[email protected]/node_modules/rxjs/dist/cjs/internal/operators/mergeInternals.js:22:31)

When stopping vite preview, visit http://localhost:4173 successfully. Use npx cypress run --e2e --browser firefox ok

RosyCloudsLee avatar Nov 28 '23 07:11 RosyCloudsLee

I've got the same issue. Switching back to v2.0.1 makes it working again.

Robbson avatar Nov 30 '23 17:11 Robbson

I switched back to v2.0.1. It also has this problem

RosyCloudsLee avatar Dec 01 '23 06:12 RosyCloudsLee

I can do this. First, check start-server-and-test version is >=2.0.3 Second, set vite.config.js :

server: {
    host: '0.0.0.0'
  },
  preview: {
    host: '0.0.0.0',
    port: 4173
  }

chenweiyi avatar Dec 14 '23 05:12 chenweiyi

I have the same issue (tested on Linux). Version 2.0.2 works, version 2.0.3 does not. The only change that happened in this version was:

https://github.com/bahmutov/start-server-and-test/compare/v2.0.2...v2.0.3

I'll check if this is a known issue with wait-on.

rkrisztian avatar Jan 10 '24 10:01 rkrisztian

I couldn't tie this problem to either the wait-on or axios version change. I think it's related to how start-server-and-test uses wait-on, although I'm not sure. Here's some details.

Test 1: Testing wait-on alone in an empty project using a test script:

$ npm i [email protected] && npm list axios && node test.js 
(...)
[email protected] tempProjects/waiton2
└─┬ [email protected]
  └── [email protected]

waiting for 1 resources: http://127.0.0.1:5173
making HTTP(S) head request to  url:http://127.0.0.1:5173 ...
  HTTP(S) result for http://127.0.0.1:5173: {
  status: 200,
  statusText: 'OK',
  headers: {
    'access-control-allow-origin': '*',
    'content-type': 'text/html',
    'cache-control': 'no-cache',
    etag: 'W/"4f2-OGT49f3kLhD7R/gJSN/I4kYS59M"',
    date: 'Wed, 10 Jan 2024 12:35:08 GMT',
    connection: 'close'
  },
  data: ''
}
wait-on(439093) complete
waitOn finished successfully


$ npm i [email protected] && npm list axios && node test.js 
(...)
[email protected] tempProjects/waiton2
└─┬ [email protected]
  └── [email protected]

waiting for 1 resources: http://127.0.0.1:5173
making HTTP(S) head request to  url:http://127.0.0.1:5173 ...
  HTTP(S) result for http://127.0.0.1:5173: {
  status: 200,
  statusText: 'OK',
  headers: Object [AxiosHeaders] {
    'access-control-allow-origin': '*',
    'content-type': 'text/html',
    'cache-control': 'no-cache',
    etag: 'W/"4f2-OGT49f3kLhD7R/gJSN/I4kYS59M"',
    date: 'Wed, 10 Jan 2024 12:34:58 GMT',
    connection: 'close'
  },
  data: ''
}
wait-on(439031) complete
waitOn finished successfully

My test script was the following. I tried to approximate as much as I could to what start-server-and-test does:

const waitOn = require('wait-on');

const url = 'http://127.0.0.1:5173';

// Options for wait-on
const options = {
  resources: [url],
  delay: 1000, // initial delay in ms, default 0
  timeout: 10000, // timeout in ms, default Infinity
  interval: 2000,
  window: 1000,
  log: true, // Enables logging
  verbose: true,
  headers: {
    Accept: 'text/html, application/json, text/plain, */*',
  },
  validateStatus: (status) => (status >= 200 && status < 300) || status === 304,
};

// Wait for the resource to become available
waitOn(options, (err) => {
  if (err) {
    console.log('error waiting for url', url);
    console.log(err.message);
    return;
  }
  console.log('waitOn finished successfully');
});

Test 2: However, when I used start-server-and-test, I had different results in a freshly-created Vite project (created with command npm create vite@latest -- --template vanilla):

$ npm i -D [email protected] && npm list axios
(...)
[email protected] tempProjects/waiton
└─┬ [email protected]
  └─┬ [email protected]
    └── [email protected]


$ DEBUG=start-server-and-test:* npm test

> [email protected] test
> start-server-and-test preview 5173 'echo successful'

1: starting server using command "npm run preview"
and when url "[ 'http://127.0.0.1:5173' ]" is responding with HTTP status code 200
running tests using command "echo successful"

waiting for 1 resources: http://127.0.0.1:5173
making HTTP(S) head request to  url:http://127.0.0.1:5173 ...
  HTTP(S) error for http://127.0.0.1:5173 Error: connect ECONNREFUSED 127.0.0.1:5173

> [email protected] preview
> vite preview --port 5173

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
making HTTP(S) head request to  url:http://127.0.0.1:5173 ...
  HTTP(S) result for http://127.0.0.1:5173: {
  status: 200,
  statusText: 'OK',
  headers: {
    'access-control-allow-origin': '*',
    'content-type': 'text/html',
    'cache-control': 'no-cache',
    etag: 'W/"1c6-uOcU/FEPRFIQaiTkxuyeY9g0IwA"',
    date: 'Wed, 10 Jan 2024 12:47:08 GMT',
    connection: 'close'
  },
  data: ''
}
wait-on(440429) complete
successful


$ npm i -D [email protected] && npm list axios
(...)
[email protected] tempProjects/waiton
└─┬ [email protected]
  └─┬ [email protected]
    └── [email protected]

$ DEBUG=start-server-and-test:* npm test

> [email protected] test
> start-server-and-test preview 5173 'echo successful'

1: starting server using command "npm run preview"
and when url "[ 'http://127.0.0.1:5173' ]" is responding with HTTP status code 200
running tests using command "echo successful"

waiting for 1 resources: http://127.0.0.1:5173
making HTTP(S) head request to  url:http://127.0.0.1:5173 ...
  HTTP(S) error for http://127.0.0.1:5173 AxiosError: Request failed with status code 504

> [email protected] preview
> vite preview --port 5173

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
making HTTP(S) head request to  url:http://127.0.0.1:5173 ...
  HTTP(S) error for http://127.0.0.1:5173 AxiosError: Request failed with status code 504
making HTTP(S) head request to  url:http://127.0.0.1:5173 ...
  HTTP(S) error for http://127.0.0.1:5173 AxiosError: Request failed with status code 504
making HTTP(S) head request to  url:http://127.0.0.1:5173 ...
^C

rkrisztian avatar Jan 10 '24 12:01 rkrisztian

start-server-and-test: 2.0.3 node: v20.11.0 pnpm: 8.15.1 npm: 10.2.4 vite:^5.0.11 os: ubuntu 20.04

Run pnpm test:e2e ok

RosyCloudsLee avatar Jun 17 '24 03:06 RosyCloudsLee

For anyone who ends up here when trying to use this with Vite serve: I struggled with this too and had an interesting observation: it works for me when I specify the port in this format localhost:3000 but it times out for all other notations (e.g. 3000, :3000, http://localhost:3000 all fail, even though the docs say it should be equivalent)

kfrederix avatar Jul 17 '24 07:07 kfrederix