From 71f276e5880f65d5e7b1843400c1b799ebc4235a Mon Sep 17 00:00:00 2001 From: Valentin Cocaud Date: Fri, 17 May 2024 20:16:35 +0200 Subject: [PATCH] [integration/bun] add tests for shared context (#3170) Co-authored-by: github-actions[bot] --- .../bun/__integration-tests__/bun.spec.ts | 100 +++++++++++++----- pnpm-lock.yaml | 73 +++++++++++++ 2 files changed, 146 insertions(+), 27 deletions(-) diff --git a/examples/bun/__integration-tests__/bun.spec.ts b/examples/bun/__integration-tests__/bun.spec.ts index 386b46f14..6f63f6cff 100644 --- a/examples/bun/__integration-tests__/bun.spec.ts +++ b/examples/bun/__integration-tests__/bun.spec.ts @@ -1,6 +1,6 @@ import { Server } from 'bun'; -import { describe, expect, it } from 'bun:test'; -import { createSchema, createYoga } from 'graphql-yoga'; +import { afterEach, beforeEach, describe, expect, it } from 'bun:test'; +import { createSchema, createYoga, Plugin } from 'graphql-yoga'; describe('Bun integration', () => { const yoga = createYoga({ @@ -20,52 +20,98 @@ describe('Bun integration', () => { let server: Server; let url: string; - function beforeEach() { + beforeEach(() => { + console.log('Starting server'); server = Bun.serve({ fetch: yoga, port: 3000, }); url = `http://${server.hostname}:${server.port}${yoga.graphqlEndpoint}`; - } + }); - function afterEach() { + afterEach(async () => { server.stop(); - } + console.log('Server stopped'); + }); it('shows GraphiQL', async () => { - beforeEach(); - try { - const response = await fetch(url, { - method: 'GET', - headers: { - Accept: 'text/html', - }, - }); - expect(response.status).toBe(200); - expect(response.headers.get('content-type')).toBe('text/html'); - const htmlContents = await response.text(); - expect(htmlContents.includes('GraphiQL')).toBe(true); - } finally { - afterEach(); - } + const response = await fetch(url, { + method: 'GET', + headers: { + Accept: 'text/html', + }, + }); + expect(response.status).toBe(200); + expect(response.headers.get('content-type')).toBe('text/html'); + const htmlContents = await response.text(); + expect(htmlContents.includes('GraphiQL')).toBe(true); }); it('accepts a query', async () => { - beforeEach(); - try { + const response = await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + query: `{ greetings }`, + }), + }); + const result = await response.json(); + expect(result.data.greetings).toBe('Hello Bun!'); + }); + + it('should have a different context for each request', async () => { + const contexts: unknown[] = []; + const yoga = createYoga({ + schema: createSchema({ + typeDefs: /* GraphQL */ ` + type Query { + greetings: String + getContext: String + } + `, + resolvers: { + Query: { + greetings: () => 'Hello Bun!', + getContext: (_, __, ctx) => ctx['test'], + }, + }, + }), + plugins: [ + { + onExecute: ({ args }) => { + contexts.push(args.contextValue); + }, + } as Plugin, + ], + }); + + server = Bun.serve({ + fetch: yoga, + port: 3001, + }); + url = `http://${server.hostname}:${server.port}${yoga.graphqlEndpoint}`; + + async function makeTestRequest() { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ - query: `{ greetings }`, + query: `{ getContext }`, }), }); - const result = await response.json(); - expect(result.data.greetings).toBe('Hello Bun!'); + await response.json(); + } + + try { + await makeTestRequest(); + await makeTestRequest(); + expect(contexts[0]).not.toBe(contexts[1]); } finally { - afterEach(); + server.stop(); } }); }); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e25673afd..a07cad7aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -338,6 +338,25 @@ importers: specifier: ^0.9.17 version: 0.9.17 + examples/bun-yoga-ws: + dependencies: + bun-types: + specifier: ^1.0.0 + version: 1.0.0 + graphql: + specifier: 16.6.0 + version: 16.6.0 + graphql-ws: + specifier: ^5.14.1 + version: 5.14.1(graphql@16.6.0) + graphql-yoga: + specifier: ^4.0.5 + version: 4.0.5(graphql@16.6.0) + devDependencies: + '@whatwg-node/fetch': + specifier: ^0.9.0 + version: 0.9.17 + examples/cloudflare-advanced: dependencies: '@cloudflare/workers-types': @@ -7541,6 +7560,31 @@ packages: dependencies: graphql: 16.6.0 + /@graphql-yoga/logger@1.0.0: + resolution: {integrity: sha512-JYoxwnPggH2BfO+dWlWZkDeFhyFZqaTRGLvFhy+Pjp2UxitEW6nDrw+pEDw/K9tJwMjIFMmTT9VfTqrnESmBHg==} + engines: {node: '>=16.0.0'} + dependencies: + tslib: 2.6.2 + dev: false + + /@graphql-yoga/subscription@4.0.0: + resolution: {integrity: sha512-0qsN/BPPZNMoC2CZ8i+P6PgiJyHh1H35aKDt37qARBDaIOKDQuvEOq7+4txUKElcmXi7DYFo109FkhSQoEajrg==} + engines: {node: '>=16.0.0'} + dependencies: + '@graphql-yoga/typed-event-target': 2.0.0 + '@repeaterjs/repeater': 3.0.4 + '@whatwg-node/events': 0.1.0 + tslib: 2.6.2 + dev: false + + /@graphql-yoga/typed-event-target@2.0.0: + resolution: {integrity: sha512-oA/VGxGmaSDym1glOHrltw43qZsFwLLjBwvh57B79UKX/vo3+UQcRgOyE44c5RP7DCYjkrC2tuArZmb6jCzysw==} + engines: {node: '>=16.0.0'} + dependencies: + '@repeaterjs/repeater': 3.0.4 + tslib: 2.6.2 + dev: false + /@grpc/grpc-js@1.8.11: resolution: {integrity: sha512-f/xC+6Z2QKsRJ+VSSFlt4hA5KSRm+PKvMWV8kMPkMgGlFidR6PeIkXrOasIY2roe+WROM6GFQLlgDKfeEZo2YQ==} engines: {node: ^8.13.0 || >=10.10.0} @@ -20854,6 +20898,35 @@ packages: dependencies: graphql: 16.6.0 + /graphql-ws@5.14.1(graphql@16.6.0): + resolution: {integrity: sha512-aqkls1espsygP1PfkAuuLIV96IbztQ6EaADse97pw8wRIMT3+AL/OYfS8V2iCRkc0gzckitoDRGCQEdnySggiA==} + engines: {node: '>=10'} + peerDependencies: + graphql: '>=0.11 <=16' + dependencies: + graphql: 16.6.0 + dev: false + + /graphql-yoga@4.0.5(graphql@16.6.0): + resolution: {integrity: sha512-vIbJU9QX5RP4PoxbMCHcfOlt/3EsC/0uLdAOlKaiUvlwJDTFCaIHo2X10vL4i/27Gw8g90ECIwm2YbmeLDwcqg==} + engines: {node: '>=16.0.0'} + peerDependencies: + graphql: ^15.2.0 || ^16.0.0 + dependencies: + '@envelop/core': 4.0.0 + '@graphql-tools/executor': 1.2.5(graphql@16.6.0) + '@graphql-tools/schema': 10.0.0(graphql@16.6.0) + '@graphql-tools/utils': 10.1.1(graphql@16.6.0) + '@graphql-yoga/logger': 1.0.0 + '@graphql-yoga/subscription': 4.0.0 + '@whatwg-node/fetch': 0.9.17 + '@whatwg-node/server': 0.9.33 + dset: 3.1.2 + graphql: 16.6.0 + lru-cache: 10.0.0 + tslib: 2.6.2 + dev: false + /graphql@16.6.0: resolution: {integrity: sha512-KPIBPDlW7NxrbT/eh4qPXz5FiFdL5UbaA0XUNz2Rp3Z3hqBSkbj0GVjwFDztsWVauZUWsbKHgMg++sk8UX0bkw==} engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0}