From f5085108e2e6c0fa5f6db64a1eb1cb06d565da4d Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Thu, 26 Dec 2024 12:42:36 +0300 Subject: [PATCH] Improve integration tests --- .github/workflows/ci.yml | 3 -- examples/bun-yoga-ws/package.json | 1 + .../bun/__integration-tests__/bun.spec.ts | 8 ++--- examples/bun/package.json | 4 ++- examples/bun/src/index.ts | 5 ++- examples/egg/.gitignore | 4 +-- .../{egg.test.ts => egg.spec.ts} | 0 .../__integration-tests__/graphql-ws.spec.ts | 36 ++++++++++++++----- examples/graphql-ws/package.json | 1 + .../hapi/__integration-tests__/hapi.spec.ts | 19 +++++++--- .../__integration-tests__/sveltekit.spec.ts | 6 ---- jest.config.js | 2 ++ pnpm-lock.yaml | 3 ++ 13 files changed, 59 insertions(+), 33 deletions(-) rename examples/egg/__integration-tests__/{egg.test.ts => egg.spec.ts} (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aa0352c6f2..2fa44c6ca0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,9 +25,6 @@ jobs: with: node-version-file: '.node-version' - - name: Setup Bun Runtime - uses: antongolub/action-setup-bun@v1 - - name: Setup Deno Runtime uses: denoland/setup-deno@v1 with: diff --git a/examples/bun-yoga-ws/package.json b/examples/bun-yoga-ws/package.json index aada96ffab..ed0b8809bc 100644 --- a/examples/bun-yoga-ws/package.json +++ b/examples/bun-yoga-ws/package.json @@ -7,6 +7,7 @@ "test": "bun test" }, "dependencies": { + "bun": "^1.1.42", "bun-types": "^1.0.0", "graphql": "16.10.0", "graphql-ws": "^5.14.1", diff --git a/examples/bun/__integration-tests__/bun.spec.ts b/examples/bun/__integration-tests__/bun.spec.ts index 6f63f6cffc..1ee92b62c1 100644 --- a/examples/bun/__integration-tests__/bun.spec.ts +++ b/examples/bun/__integration-tests__/bun.spec.ts @@ -21,18 +21,14 @@ describe('Bun integration', () => { let server: Server; let url: string; beforeEach(() => { - console.log('Starting server'); server = Bun.serve({ fetch: yoga, - port: 3000, + port: 0, }); url = `http://${server.hostname}:${server.port}${yoga.graphqlEndpoint}`; }); - afterEach(async () => { - server.stop(); - console.log('Server stopped'); - }); + afterEach(() => server.stop()); it('shows GraphiQL', async () => { const response = await fetch(url, { diff --git a/examples/bun/package.json b/examples/bun/package.json index b23b51bf35..204c318d02 100644 --- a/examples/bun/package.json +++ b/examples/bun/package.json @@ -7,11 +7,13 @@ "test": "bun test" }, "dependencies": { - "bun-types": "^1.0.0", + "bun": "^1.1.42", + "bun-types": "^1.1.42", "graphql": "16.10.0", "graphql-yoga": "5.3.1" }, "devDependencies": { + "@types/bun": "^1.1.14", "@whatwg-node/fetch": "^0.10.1", "typescript": "^5.4.5" } diff --git a/examples/bun/src/index.ts b/examples/bun/src/index.ts index 54ec3a29aa..c0b15364ed 100644 --- a/examples/bun/src/index.ts +++ b/examples/bun/src/index.ts @@ -15,7 +15,10 @@ const yoga = createYoga({ }), }); -const server = Bun.serve(yoga); +const server = Bun.serve({ + fetch: yoga, + port: 4000, +}); console.info( `Server is running on http://${server.hostname}:${server.port}${yoga.graphqlEndpoint}`, diff --git a/examples/egg/.gitignore b/examples/egg/.gitignore index ea21e550e1..e81df5da9c 100644 --- a/examples/egg/.gitignore +++ b/examples/egg/.gitignore @@ -1,4 +1,4 @@ run logs -app/*.js -config/*.js \ No newline at end of file +app/**/*.js +config/**/*.js \ No newline at end of file diff --git a/examples/egg/__integration-tests__/egg.test.ts b/examples/egg/__integration-tests__/egg.spec.ts similarity index 100% rename from examples/egg/__integration-tests__/egg.test.ts rename to examples/egg/__integration-tests__/egg.spec.ts diff --git a/examples/graphql-ws/__integration-tests__/graphql-ws.spec.ts b/examples/graphql-ws/__integration-tests__/graphql-ws.spec.ts index 1b5af1acb5..e9bf676ad7 100644 --- a/examples/graphql-ws/__integration-tests__/graphql-ws.spec.ts +++ b/examples/graphql-ws/__integration-tests__/graphql-ws.spec.ts @@ -1,4 +1,5 @@ import { createServer } from 'node:http'; +import { AddressInfo } from 'node:net'; import { createClient } from 'graphql-ws'; import { useServer } from 'graphql-ws/lib/use/ws'; import { createSchema, createYoga } from 'graphql-yoga'; @@ -7,13 +8,28 @@ import { buildApp } from '../src/app.js'; describe('graphql-ws example integration', () => { const app = buildApp(); - beforeAll(() => app.start(4000)); + function findAvailablePort() { + return new Promise((resolve, reject) => { + const server = createServer(); + server.listen(0, () => { + const { port } = server.address() as AddressInfo; + server.close(() => resolve(port)); + }); + server.once('error', reject); + }); + } + let url: string; + beforeAll(async () => { + const port = await findAvailablePort(); + url = `ws://localhost:${port}/graphql`; + return app.start(port); + }); afterAll(() => app.stop()); it('should execute query', async () => { const client = createClient({ webSocketImpl: WebSocket, - url: 'ws://localhost:4000/graphql', + url, retryAttempts: 0, // fail right away }); @@ -30,13 +46,13 @@ describe('graphql-ws example integration', () => { ); }); - expect(onNext).toBeCalledWith({ data: { hello: 'world' } }); + expect(onNext).toHaveBeenCalledWith({ data: { hello: 'world' } }); }); it('should execute mutation', async () => { const client = createClient({ webSocketImpl: WebSocket, - url: 'ws://localhost:4000/graphql', + url, retryAttempts: 0, // fail right away }); @@ -59,7 +75,7 @@ describe('graphql-ws example integration', () => { it('should subscribe', async () => { const client = createClient({ webSocketImpl: WebSocket, - url: 'ws://localhost:4000/graphql', + url, retryAttempts: 0, // fail right away }); @@ -131,17 +147,19 @@ describe('graphql-ws example integration', () => { }), ); + const port = await findAvailablePort(); + await new Promise((resolve, reject) => { server.on('error', err => reject(err)); server.on('listening', () => resolve()); - server.listen(4001); + server.listen(port); }); // const client = createClient({ webSocketImpl: WebSocket, - url: 'ws://localhost:4001/graphql', + url: `ws://localhost:${port}/graphql`, retryAttempts: 0, // fail right away }); @@ -158,8 +176,8 @@ describe('graphql-ws example integration', () => { ); }); - expect(onNext).toBeCalledTimes(1); - expect(onNext).toBeCalledWith({ data: { hello: 'world' } }); + expect(onNext).toHaveBeenCalledTimes(1); + expect(onNext).toHaveBeenCalledWith({ data: { hello: 'world' } }); // diff --git a/examples/graphql-ws/package.json b/examples/graphql-ws/package.json index 2505dbbdbf..9de05c6a4b 100644 --- a/examples/graphql-ws/package.json +++ b/examples/graphql-ws/package.json @@ -14,6 +14,7 @@ "ws": "8.18.0" }, "devDependencies": { + "@types/ws": "^8.5.13", "cross-env": "7.0.3", "ts-node": "10.9.2", "ts-node-dev": "2.0.0", diff --git a/examples/hapi/__integration-tests__/hapi.spec.ts b/examples/hapi/__integration-tests__/hapi.spec.ts index 25082744be..bd01b9ee09 100644 --- a/examples/hapi/__integration-tests__/hapi.spec.ts +++ b/examples/hapi/__integration-tests__/hapi.spec.ts @@ -2,12 +2,21 @@ import { fetch } from '@whatwg-node/fetch'; import { startApp } from '../src/app.js'; describe('hapi example integration', () => { - const port = 4000; - let stop = () => { - // noop - }; + let port: number; + function findAvailablePort() { + return new Promise((resolve, reject) => { + const server = require('http').createServer(); + server.listen(0, () => { + const { port } = server.address(); + server.close(() => resolve(port)); + }); + server.once('error', reject); + }); + } + let stop: VoidFunction; beforeAll(async () => { - stop = await startApp(4000); + port = await findAvailablePort(); + stop = await startApp(port); }); afterAll(() => stop()); diff --git a/examples/sveltekit/__integration-tests__/sveltekit.spec.ts b/examples/sveltekit/__integration-tests__/sveltekit.spec.ts index 38aa723dc6..3774df9718 100644 --- a/examples/sveltekit/__integration-tests__/sveltekit.spec.ts +++ b/examples/sveltekit/__integration-tests__/sveltekit.spec.ts @@ -19,12 +19,6 @@ const timings = { }; describe('SvelteKit integration', () => { - if (process.env.LEAKS_TEST) { - it('dummy', () => { - return; - }); - return; - } beforeAll(async () => { const tslibDirPath = join(__dirname, '../node_modules/tslib'); const tslibFilePath = join(tslibDirPath, 'tslib.js'); diff --git a/jest.config.js b/jest.config.js index 98ae2631bb..dc1a4c7ed2 100644 --- a/jest.config.js +++ b/jest.config.js @@ -37,6 +37,8 @@ if (process.env.LEAKS_TEST === 'true') { '!**/uwebsockets.test.ts', '!**/apollo-client.test.ts', '!**/browser.spec.ts', + '!**/egg.spec.ts', + '!**/sveltekit.spec.ts', ); } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d37cb8f0ff..954d40a784 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -750,6 +750,9 @@ importers: specifier: 8.18.0 version: 8.18.0 devDependencies: + '@types/ws': + specifier: ^8.5.13 + version: 8.5.13 cross-env: specifier: 7.0.3 version: 7.0.3