Skip to content

Commit

Permalink
[integration/bun] add tests for shared context (#3170)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
EmrysMyrddin and github-actions[bot] committed May 17, 2024
1 parent 38b2c21 commit 71f276e
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 27 deletions.
100 changes: 73 additions & 27 deletions examples/bun/__integration-tests__/bun.spec.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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();
}
});
});
73 changes: 73 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 71f276e

Please sign in to comment.