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