1
+ import path from 'path'
2
+ import type { RemoteGraphQLInterceptor , WithCtxInjected , WithCtxOptions } from './support/e2eSupport'
3
+ import { e2eProjectDirs } from './support/e2eProjectDirs'
4
+ import type { CloudExecuteRemote } from '@packages/data-context/src/sources'
1
5
import type { DataContext } from '@packages/data-context'
2
6
import * as inspector from 'inspector'
3
7
import sinonChai from '@cypress/sinon-chai'
4
8
import sinon from 'sinon'
5
9
import rimraf from 'rimraf'
6
10
import util from 'util'
11
+ import fs from 'fs'
12
+ import { buildSchema , execute , parse } from 'graphql'
13
+ import { Response } from 'cross-fetch'
14
+
15
+ import { CloudRunQuery } from '../support/mock-graphql/stubgql-CloudTypes'
16
+ import { getOperationName } from '@urql/core'
17
+
18
+ const cloudSchema = buildSchema ( fs . readFileSync ( path . join ( __dirname , '../../../graphql/schemas/cloud.graphql' ) , 'utf8' ) )
7
19
8
20
// require'd so we don't conflict with globals loaded in @packages/types
9
21
const chai = require ( 'chai' )
10
22
const chaiAsPromised = require ( 'chai-as-promised' )
11
23
const chaiSubset = require ( 'chai-subset' )
24
+
12
25
const { expect } = chai
13
26
14
27
chai . use ( chaiAsPromised )
15
28
chai . use ( chaiSubset )
16
29
chai . use ( sinonChai )
17
30
18
- import path from 'path'
19
- import type { WithCtxInjected , WithCtxOptions } from './support/e2eSupport'
20
- import { e2eProjectDirs } from './support/e2eProjectDirs'
21
-
22
31
export async function e2ePluginSetup ( projectRoot : string , on : Cypress . PluginEvents , config : Cypress . PluginConfigOptions ) {
23
32
process . env . CYPRESS_INTERNAL_E2E_TESTING_SELF = 'true'
24
33
// require'd so we don't import the types from @packages/server which would
@@ -35,14 +44,22 @@ export async function e2ePluginSetup (projectRoot: string, on: Cypress.PluginEve
35
44
fn : string
36
45
options : WithCtxOptions
37
46
activeTestId : string
47
+ // TODO(tim): add an API for intercepting this
48
+ interceptCloudExecute : ( config : CloudExecuteRemote ) => { }
38
49
}
39
50
40
51
let ctx : DataContext
41
52
let serverPortPromise : Promise < number >
42
53
let currentTestId : string | undefined
43
54
let testState : Record < string , any > = { }
55
+ let remoteGraphQLIntercept : RemoteGraphQLInterceptor | undefined
44
56
45
57
on ( 'task' , {
58
+ remoteGraphQLIntercept ( fn : string ) {
59
+ remoteGraphQLIntercept = new Function ( 'console' , 'obj' , `return (${ fn } )(obj)` ) . bind ( null , console ) as RemoteGraphQLInterceptor
60
+
61
+ return null
62
+ } ,
46
63
setupE2E ( projectName : string ) {
47
64
Fixtures . scaffoldProject ( projectName )
48
65
@@ -53,11 +70,49 @@ export async function e2ePluginSetup (projectRoot: string, on: Cypress.PluginEve
53
70
if ( obj . activeTestId !== currentTestId ) {
54
71
await ctx ?. destroy ( )
55
72
currentTestId = obj . activeTestId
73
+ remoteGraphQLIntercept = undefined
56
74
testState = { } ;
57
75
( { serverPortPromise, ctx } = runInternalServer ( {
58
76
projectRoot : null ,
59
77
} ) as { ctx : DataContext , serverPortPromise : Promise < number > } )
60
78
79
+ const fetchApi = ctx . util . fetch
80
+
81
+ sinon . stub ( ctx . util , 'fetch' ) . get ( ( ) => {
82
+ return async ( url : RequestInfo , init ?: RequestInit ) => {
83
+ if ( ! String ( url ) . endsWith ( '/test-runner-graphql' ) ) {
84
+ return fetchApi ( url , init )
85
+ }
86
+
87
+ const { query, variables } = JSON . parse ( String ( init ?. body ) )
88
+ const document = parse ( query )
89
+ const operationName = getOperationName ( document )
90
+
91
+ let result = await execute ( {
92
+ operationName,
93
+ document,
94
+ variableValues : variables ,
95
+ schema : cloudSchema ,
96
+ rootValue : CloudRunQuery ,
97
+ contextValue : {
98
+ __server__ : ctx ,
99
+ } ,
100
+ } )
101
+
102
+ if ( remoteGraphQLIntercept ) {
103
+ result = remoteGraphQLIntercept ( {
104
+ operationName,
105
+ variables,
106
+ document,
107
+ query,
108
+ result,
109
+ } )
110
+ }
111
+
112
+ return new Response ( JSON . stringify ( result ) , { status : 200 } )
113
+ }
114
+ } )
115
+
61
116
await serverPortPromise
62
117
}
63
118
0 commit comments