graphql-server-test 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/esm/server.js +17 -4
  2. package/package.json +4 -4
  3. package/server.js +17 -4
package/esm/server.js CHANGED
@@ -2,11 +2,12 @@ import { Server } from '@constructive-io/graphql-server';
2
2
  import { createServer } from 'http';
3
3
  /**
4
4
  * Find an available port starting from the given port
5
+ * Uses 127.0.0.1 explicitly to avoid IPv6/IPv4 mismatch issues with supertest
5
6
  */
6
- const findAvailablePort = async (startPort) => {
7
+ const findAvailablePort = async (startPort, host = '127.0.0.1') => {
7
8
  return new Promise((resolve, reject) => {
8
9
  const server = createServer();
9
- server.listen(startPort, () => {
10
+ server.listen(startPort, host, () => {
10
11
  const address = server.address();
11
12
  const port = typeof address === 'object' && address ? address.port : startPort;
12
13
  server.close(() => resolve(port));
@@ -28,9 +29,11 @@ const findAvailablePort = async (startPort) => {
28
29
  * which includes all the standard middleware (CORS, authentication, GraphQL, etc.)
29
30
  */
30
31
  export const createTestServer = async (opts, serverOpts = {}) => {
31
- const host = serverOpts.host ?? 'localhost';
32
+ // Use 127.0.0.1 by default to avoid IPv6/IPv4 mismatch issues with supertest
33
+ // On some systems, 'localhost' resolves to ::1 (IPv6) but supertest connects to 127.0.0.1 (IPv4)
34
+ const host = serverOpts.host ?? '127.0.0.1';
32
35
  const requestedPort = serverOpts.port ?? 0;
33
- const port = requestedPort === 0 ? await findAvailablePort(5555) : requestedPort;
36
+ const port = requestedPort === 0 ? await findAvailablePort(5555, host) : requestedPort;
34
37
  // Merge server options into the PgpmOptions
35
38
  const serverConfig = {
36
39
  ...opts,
@@ -44,6 +47,16 @@ export const createTestServer = async (opts, serverOpts = {}) => {
44
47
  const server = new Server(serverConfig);
45
48
  // Start listening and get the HTTP server
46
49
  const httpServer = server.listen();
50
+ // Wait for the server to actually be listening before getting the address
51
+ // The listen() call is async - it returns immediately but the server isn't ready yet
52
+ await new Promise((resolve) => {
53
+ if (httpServer.listening) {
54
+ resolve();
55
+ }
56
+ else {
57
+ httpServer.once('listening', () => resolve());
58
+ }
59
+ });
47
60
  const actualPort = httpServer.address().port;
48
61
  const stop = async () => {
49
62
  await server.close({ closeCaches: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "graphql-server-test",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "Constructive GraphQL Server Testing with SuperTest HTTP requests",
6
6
  "main": "index.js",
@@ -35,8 +35,8 @@
35
35
  "makage": "^0.1.10"
36
36
  },
37
37
  "dependencies": {
38
- "@constructive-io/graphql-env": "^2.9.0",
39
- "@constructive-io/graphql-server": "^2.19.3",
38
+ "@constructive-io/graphql-env": "^2.9.1",
39
+ "@constructive-io/graphql-server": "^2.19.5",
40
40
  "@constructive-io/graphql-types": "^2.13.0",
41
41
  "@pgpmjs/types": "^2.15.0",
42
42
  "express": "^5.2.1",
@@ -57,5 +57,5 @@
57
57
  "e2e",
58
58
  "server"
59
59
  ],
60
- "gitHead": "06cc06455356911bf9a24245849138892267b87d"
60
+ "gitHead": "13c6cd90521b5521a7219ce270e1b4dd979652cf"
61
61
  }
package/server.js CHANGED
@@ -5,11 +5,12 @@ const graphql_server_1 = require("@constructive-io/graphql-server");
5
5
  const http_1 = require("http");
6
6
  /**
7
7
  * Find an available port starting from the given port
8
+ * Uses 127.0.0.1 explicitly to avoid IPv6/IPv4 mismatch issues with supertest
8
9
  */
9
- const findAvailablePort = async (startPort) => {
10
+ const findAvailablePort = async (startPort, host = '127.0.0.1') => {
10
11
  return new Promise((resolve, reject) => {
11
12
  const server = (0, http_1.createServer)();
12
- server.listen(startPort, () => {
13
+ server.listen(startPort, host, () => {
13
14
  const address = server.address();
14
15
  const port = typeof address === 'object' && address ? address.port : startPort;
15
16
  server.close(() => resolve(port));
@@ -31,9 +32,11 @@ const findAvailablePort = async (startPort) => {
31
32
  * which includes all the standard middleware (CORS, authentication, GraphQL, etc.)
32
33
  */
33
34
  const createTestServer = async (opts, serverOpts = {}) => {
34
- const host = serverOpts.host ?? 'localhost';
35
+ // Use 127.0.0.1 by default to avoid IPv6/IPv4 mismatch issues with supertest
36
+ // On some systems, 'localhost' resolves to ::1 (IPv6) but supertest connects to 127.0.0.1 (IPv4)
37
+ const host = serverOpts.host ?? '127.0.0.1';
35
38
  const requestedPort = serverOpts.port ?? 0;
36
- const port = requestedPort === 0 ? await findAvailablePort(5555) : requestedPort;
39
+ const port = requestedPort === 0 ? await findAvailablePort(5555, host) : requestedPort;
37
40
  // Merge server options into the PgpmOptions
38
41
  const serverConfig = {
39
42
  ...opts,
@@ -47,6 +50,16 @@ const createTestServer = async (opts, serverOpts = {}) => {
47
50
  const server = new graphql_server_1.Server(serverConfig);
48
51
  // Start listening and get the HTTP server
49
52
  const httpServer = server.listen();
53
+ // Wait for the server to actually be listening before getting the address
54
+ // The listen() call is async - it returns immediately but the server isn't ready yet
55
+ await new Promise((resolve) => {
56
+ if (httpServer.listening) {
57
+ resolve();
58
+ }
59
+ else {
60
+ httpServer.once('listening', () => resolve());
61
+ }
62
+ });
50
63
  const actualPort = httpServer.address().port;
51
64
  const stop = async () => {
52
65
  await server.close({ closeCaches: true });