|
| 1 | +import { spawn } from 'child_process' |
| 2 | +import puppeteer from 'puppeteer' |
| 3 | + |
| 4 | +let browser: puppeteer.Browser |
| 5 | +let page: puppeteer.Page |
| 6 | +let bunProcess: ReturnType<typeof spawn> |
| 7 | + |
| 8 | +const timings = { |
| 9 | + waitForSelector: 999, |
| 10 | + waitForResponse: 1999, |
| 11 | +} |
| 12 | + |
| 13 | +jest.setTimeout(20000) |
| 14 | + |
| 15 | +let toSkip = false |
| 16 | + |
| 17 | +describe('Bun integration', () => { |
| 18 | + let serverUrl: string |
| 19 | + beforeAll(async () => { |
| 20 | + if (process.versions.node.startsWith('14')) { |
| 21 | + toSkip = true |
| 22 | + return |
| 23 | + } |
| 24 | + // Start Bun |
| 25 | + bunProcess = spawn('yarn', ['workspace', 'example-bun', 'start']) |
| 26 | + |
| 27 | + serverUrl = await new Promise((resolve, reject) => { |
| 28 | + bunProcess.stderr?.on('data', (chunk) => { |
| 29 | + const chunkString = chunk.toString('utf-8') |
| 30 | + console.error(chunk.toString('utf-8')) |
| 31 | + if (chunkString.includes('Command failed')) { |
| 32 | + reject(new Error('Bun failed to start')) |
| 33 | + } |
| 34 | + }) |
| 35 | + |
| 36 | + bunProcess.stdout?.on('data', (chunk) => { |
| 37 | + const chunkString = chunk.toString('utf-8') |
| 38 | + console.log(chunk.toString('utf-8')) |
| 39 | + if (chunkString.includes('Server is running on')) { |
| 40 | + resolve(chunkString.split('Server is running on ')[1]) |
| 41 | + } |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + // Launch puppeteer |
| 46 | + browser = await puppeteer.launch({ |
| 47 | + // If you wanna run tests with open browser |
| 48 | + // set your PUPPETEER_HEADLESS env to "false" |
| 49 | + headless: process.env.PUPPETEER_HEADLESS !== 'false', |
| 50 | + args: ['--incognito'], |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + beforeEach(async () => { |
| 55 | + if (toSkip) { |
| 56 | + return |
| 57 | + } |
| 58 | + if (page !== undefined) { |
| 59 | + await page.close() |
| 60 | + } |
| 61 | + const context = await browser.createIncognitoBrowserContext() |
| 62 | + page = await context.newPage() |
| 63 | + }) |
| 64 | + |
| 65 | + afterAll(async () => { |
| 66 | + if (toSkip) { |
| 67 | + return |
| 68 | + } |
| 69 | + await browser.close() |
| 70 | + bunProcess.kill() |
| 71 | + }) |
| 72 | + |
| 73 | + it('go to GraphiQL page', async () => { |
| 74 | + if (toSkip) { |
| 75 | + return |
| 76 | + } |
| 77 | + // Go the the right route |
| 78 | + const body = await page.goto( |
| 79 | + `${serverUrl}?query=query+Hello+%7B%0A%09greetings%0A%7D`, |
| 80 | + ) |
| 81 | + |
| 82 | + let strIntro = '' |
| 83 | + try { |
| 84 | + // A-1/ Wait for the introspection query result getting our type "greetings" |
| 85 | + const resIntro = await page.waitForResponse( |
| 86 | + (res) => res.url().endsWith('/graphql'), |
| 87 | + { |
| 88 | + timeout: timings.waitForResponse, |
| 89 | + }, |
| 90 | + ) |
| 91 | + const jsonIntro = await resIntro.json() |
| 92 | + strIntro = JSON.stringify(jsonIntro, null, 0) |
| 93 | + } catch (error) { |
| 94 | + // We had an issue grabbing the introspection query result! |
| 95 | + // let's see what is in the html with the finafinally |
| 96 | + } finally { |
| 97 | + const bodyContent = await body?.text() |
| 98 | + // B/ Check that GraphiQL is showing |
| 99 | + expect(bodyContent).toContain(`Yoga GraphiQL`) |
| 100 | + } |
| 101 | + |
| 102 | + // A-2/ Finish the test after the body check |
| 103 | + expect(strIntro).toContain(`"name":"greetings"`) |
| 104 | + |
| 105 | + // C/ Tigger the default request and wait for the response |
| 106 | + const [res] = await Promise.all([ |
| 107 | + page.waitForResponse((res) => res.url().endsWith('/graphql'), { |
| 108 | + timeout: timings.waitForResponse, |
| 109 | + }), |
| 110 | + page.click(`button[class="execute-button"]`), |
| 111 | + ]) |
| 112 | + |
| 113 | + const json = await res.json() |
| 114 | + const str = JSON.stringify(json, null, 0) |
| 115 | + expect(str).toContain(`{"data":{"greetings":"Hello Bun!"}}`) |
| 116 | + }) |
| 117 | +}) |
0 commit comments