|
1 |
| -import { spawn } from 'child_process' |
2 | 1 | import { fetch } from '@whatwg-node/fetch'
|
3 | 2 |
|
4 |
| -describe('Bun integration', () => { |
5 |
| - let bunProcess: ReturnType<typeof spawn> |
6 |
| - let serverUrl: string |
7 |
| - |
8 |
| - beforeAll(async () => { |
9 |
| - // Start Bun |
10 |
| - bunProcess = spawn('yarn', ['workspace', 'example-bun', 'start']) |
| 3 | +import { createYoga, createSchema } from 'graphql-yoga' |
11 | 4 |
|
12 |
| - serverUrl = await new Promise((resolve, reject) => { |
13 |
| - bunProcess.stderr?.on('data', (chunk) => { |
14 |
| - const chunkString = chunk.toString('utf-8') |
15 |
| - console.error(chunk.toString('utf-8')) |
16 |
| - if (chunkString.includes('Command failed')) { |
17 |
| - reject(new Error('Bun failed to start')) |
18 |
| - } |
19 |
| - }) |
| 5 | +import { describe, it, expect } from 'bun:test' |
20 | 6 |
|
21 |
| - bunProcess.stdout?.on('data', (chunk) => { |
22 |
| - const chunkString = chunk.toString('utf-8') |
23 |
| - console.log(chunk.toString('utf-8')) |
24 |
| - if (chunkString.includes('Server is running on')) { |
25 |
| - resolve(chunkString.split('Server is running on ')[1]) |
| 7 | +describe('Bun integration', () => { |
| 8 | + const yoga = createYoga({ |
| 9 | + schema: createSchema({ |
| 10 | + typeDefs: /* GraphQL */ ` |
| 11 | + type Query { |
| 12 | + greetings: String |
26 | 13 | }
|
27 |
| - }) |
28 |
| - }) |
29 |
| - }) |
30 |
| - |
31 |
| - afterAll(() => { |
32 |
| - bunProcess.kill() |
| 14 | + `, |
| 15 | + resolvers: { |
| 16 | + Query: { |
| 17 | + greetings: () => 'Hello Bun!', |
| 18 | + }, |
| 19 | + }, |
| 20 | + }), |
33 | 21 | })
|
34 | 22 |
|
35 | 23 | it('shows GraphiQL', async () => {
|
36 |
| - const response = await fetch(serverUrl, { |
37 |
| - method: 'GET', |
38 |
| - headers: { |
39 |
| - Accept: 'text/html', |
| 24 | + const server = Bun.serve(yoga) |
| 25 | + const response = await fetch( |
| 26 | + new URL(yoga.graphqlEndpoint, server.hostname).toString(), |
| 27 | + { |
| 28 | + method: 'GET', |
| 29 | + headers: { |
| 30 | + Accept: 'text/html', |
| 31 | + }, |
40 | 32 | },
|
41 |
| - }) |
42 |
| - expect(response.status).toEqual(200) |
43 |
| - expect(response.headers.get('content-type')).toEqual('text/html') |
| 33 | + ) |
| 34 | + expect(response.status).toBe(200) |
| 35 | + expect(response.headers.get('content-type')).toBe('text/html') |
44 | 36 | const htmlContents = await response.text()
|
45 |
| - expect(htmlContents).toContain('Yoga GraphiQL') |
| 37 | + expect(htmlContents.includes('GraphiQL')).toBe(true) |
| 38 | + server.stop() |
46 | 39 | })
|
47 | 40 |
|
48 | 41 | it('accepts a query', async () => {
|
49 |
| - const response = await fetch(serverUrl, { |
50 |
| - method: 'POST', |
51 |
| - headers: { |
52 |
| - 'Content-Type': 'application/json', |
| 42 | + const server = Bun.serve(yoga) |
| 43 | + const response = await fetch( |
| 44 | + new URL(yoga.graphqlEndpoint, server.hostname).toString(), |
| 45 | + { |
| 46 | + method: 'POST', |
| 47 | + headers: { |
| 48 | + 'Content-Type': 'application/json', |
| 49 | + }, |
| 50 | + body: JSON.stringify({ |
| 51 | + query: `{ greetings }`, |
| 52 | + }), |
53 | 53 | },
|
54 |
| - body: JSON.stringify({ |
55 |
| - query: `{ greetings }`, |
56 |
| - }), |
57 |
| - }) |
| 54 | + ) |
58 | 55 | const result = await response.json()
|
59 |
| - expect(result).toEqual({ |
60 |
| - data: { |
61 |
| - greetings: 'Hello Bun!', |
62 |
| - }, |
63 |
| - }) |
| 56 | + expect(result.data.greetings).toBe('Hello Bun!') |
| 57 | + server.stop() |
64 | 58 | })
|
65 | 59 | })
|
0 commit comments