-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
e2ePluginSetup.ts
362 lines (291 loc) · 11.9 KB
/
e2ePluginSetup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import path from 'path'
import execa from 'execa'
import type { CyTaskResult, RemoteGraphQLInterceptor, ResetOptionsResult, WithCtxInjected, WithCtxOptions } from './support/e2eSupport'
import { fixtureDirs } from '@tooling/system-tests'
// import type { CloudExecuteRemote } from '@packages/data-context/src/sources'
import { makeGraphQLServer } from '@packages/graphql/src/makeGraphQLServer'
import { clearCtx, DataContext, globalPubSub, setCtx } from '@packages/data-context'
import * as inspector from 'inspector'
import sinonChai from '@cypress/sinon-chai'
import sinon from 'sinon'
import fs from 'fs-extra'
import { buildSchema, execute, GraphQLError, parse } from 'graphql'
import { Response } from 'cross-fetch'
import { CloudRunQuery } from '../support/mock-graphql/stubgql-CloudTypes'
import { getOperationName } from '@urql/core'
import pDefer from 'p-defer'
const pkg = require('@packages/root')
interface InternalOpenProjectArgs {
argv: string[]
projectName: string
}
interface InternalAddProjectOpts {
projectName: string
open?: boolean
}
const cloudSchema = buildSchema(fs.readFileSync(path.join(__dirname, '../../../graphql/schemas/cloud.graphql'), 'utf8'))
// require'd so we don't conflict with globals loaded in @packages/types
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
const chaiSubset = require('chai-subset')
const { expect } = chai
chai.use(chaiAsPromised)
chai.use(chaiSubset)
chai.use(sinonChai)
export async function e2ePluginSetup (on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions) {
process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF = 'true'
delete process.env.CYPRESS_INTERNAL_GRAPHQL_PORT
delete process.env.CYPRESS_INTERNAL_VITE_DEV
delete process.env.CYPRESS_INTERNAL_VITE_APP_PORT
delete process.env.CYPRESS_INTERNAL_VITE_LAUNCHPAD_PORT
// Set this to a dedicate port so we can debug the state of the tests
process.env.CYPRESS_INTERNAL_GRAPHQL_PORT = '5555'
on('task', await makeE2ETasks())
return {
...config,
env: {
e2e_isDebugging: Boolean(inspector.url()),
},
}
}
export type E2ETaskMap = ReturnType<typeof makeE2ETasks> extends Promise<infer U> ? U : never
interface FixturesShape {
scaffold (): void
scaffoldProject (project: string): Promise<void>
clearFixtureNodeModules (project: string): void
scaffoldWatch (): void
remove (): void
removeProject (name): void
projectPath (name): string
projectFixturePath(name): string
get (fixture, encoding?: BufferEncoding): string
path (fixture): string
}
async function makeE2ETasks () {
// require'd from @packages/server & @tooling/system-tests so we don't import
// types which would pollute strict type checking
const argUtils = require('@packages/server/lib/util/args')
const { makeDataContext } = require('@packages/server/lib/makeDataContext')
const Fixtures = require('@tooling/system-tests') as FixturesShape
const { scaffoldCommonNodeModules, scaffoldProjectNodeModules } = require('@tooling/system-tests/lib/dep-installer')
const cli = require('../../../../cli/lib/cli')
const cliOpen = require('../../../../cli/lib/exec/open')
// Remove all the fixtures when the plugin starts
Fixtures.remove()
// const tmpDir = path.join(__dirname, '.projects')
// await util.promisify(rimraf)(tmpDir)
// Fixtures.setTmpDir(tmpDir)
interface WithCtxObj {
fn: string
options: WithCtxOptions
// TODO(tim): add an API for intercepting this
// interceptCloudExecute: (config: CloudExecuteRemote) => {}
}
let ctx: DataContext
let testState: Record<string, any> = {}
let remoteGraphQLIntercept: RemoteGraphQLInterceptor | undefined
let scaffoldedProjects = new Set<string>()
const cachedCwd = process.cwd()
clearCtx()
ctx = setCtx(makeDataContext({ mode: 'open', modeOptions: { cwd: process.cwd() } }))
const launchpadPort = await makeGraphQLServer()
const __internal_scaffoldProject = async (projectName: string, isRetry = false): Promise<string> => {
if (fs.existsSync(Fixtures.projectPath(projectName))) {
Fixtures.removeProject(projectName)
}
Fixtures.clearFixtureNodeModules(projectName)
await Fixtures.scaffoldProject(projectName)
await scaffoldCommonNodeModules()
try {
await scaffoldProjectNodeModules(projectName)
} catch (e) {
if (isRetry) {
throw e
}
// If we have an error, it's likely that we don't have a lockfile, or it's out of date.
// Let's run a quick "yarn" in the directory, kill the node_modules, and try again
await execa('yarn', { cwd: Fixtures.projectFixturePath(projectName), stdio: 'inherit', shell: true })
await fs.remove(path.join(Fixtures.projectFixturePath(projectName), 'node_modules'))
return await __internal_scaffoldProject(projectName, true)
}
scaffoldedProjects.add(projectName)
return Fixtures.projectPath(projectName)
}
return {
/**
* Called before all tests, cleans up any scaffolded projects and returns the global "launchpadPort".
* The same GraphQL server is used for all integration tests, so we can
* go to http://localhost:5555/graphql and debug the internal state of the application
*/
async __internal__before () {
Fixtures.remove()
scaffoldedProjects = new Set()
process.chdir(cachedCwd)
return { launchpadPort }
},
/**
* Force a reset to the correct CWD after all tests have completed, just incase this
* was modified by any code under test.
*/
__internal__after () {
process.chdir(cachedCwd)
return null
},
/**
* Called before each test to do global setup/cleanup
*/
async __internal__beforeEach () {
testState = {}
await globalPubSub.emitThen('test:cleanup')
await ctx.actions.app.removeAppDataDir()
await ctx.actions.app.ensureAppDataDirExists()
await ctx.reinitializeCypress()
sinon.reset()
sinon.restore()
remoteGraphQLIntercept = undefined
const fetchApi = ctx.util.fetch
// Stub all of the things that we can't actually execute (electron/os operations),
// so we can verify they were called
sinon.stub(ctx.actions.electron, 'openExternal')
sinon.stub(ctx.actions.electron, 'showItemInFolder')
const operationCount: Record<string, number> = {}
sinon.stub(ctx.util, 'fetch').callsFake(async (url: RequestInfo | URL, init?: RequestInit) => {
if (String(url).endsWith('/test-runner-graphql')) {
const { query, variables } = JSON.parse(String(init?.body))
const document = parse(query)
const operationName = getOperationName(document)
operationCount[operationName ?? 'unknown'] = operationCount[operationName ?? 'unknown'] ?? 0
let result = await execute({
operationName,
document,
variableValues: variables,
schema: cloudSchema,
rootValue: CloudRunQuery,
contextValue: {
__server__: ctx,
},
})
operationCount[operationName ?? 'unknown']++
if (remoteGraphQLIntercept) {
try {
result = await remoteGraphQLIntercept({
operationName,
variables,
document,
query,
result,
callCount: operationCount[operationName ?? 'unknown'],
}, testState)
} catch (e) {
const err = e as Error
result = { data: null, extensions: [], errors: [new GraphQLError(err.message, undefined, undefined, undefined, undefined, err)] }
}
}
return new Response(JSON.stringify(result), { status: 200 })
}
if (String(url) === 'https://download.cypress.io/desktop.json') {
return new Response(JSON.stringify({
name: 'Cypress',
version: pkg.version,
}), { status: 200 })
}
if (String(url) === 'https://registry.npmjs.org/cypress') {
return new Response(JSON.stringify({
'time': {
[pkg.version]: '2022-02-10T01:07:37.369Z',
},
}), { status: 200 })
}
return fetchApi(url, init)
})
return null
},
__internal_remoteGraphQLIntercept (fn: string) {
remoteGraphQLIntercept = new Function('console', 'obj', 'testState', `return (${fn})(obj, testState)`).bind(null, console) as RemoteGraphQLInterceptor
return null
},
async __internal_addProject (opts: InternalAddProjectOpts) {
if (!scaffoldedProjects.has(opts.projectName)) {
await __internal_scaffoldProject(opts.projectName)
}
await ctx.actions.project.addProject({ path: Fixtures.projectPath(opts.projectName), open: opts.open })
return Fixtures.projectPath(opts.projectName)
},
__internal_scaffoldProject,
async __internal_openGlobal (argv: string[] = []): Promise<ResetOptionsResult> {
const openArgv = ['--global', ...argv]
// Runs the launchArgs through the whole pipeline for the CLI open process,
// which probably needs a bit of refactoring / consolidating
const cliOptions = await cli.parseOpenCommand(['open', ...openArgv])
const processedArgv = cliOpen.processOpenOptions(cliOptions)
const modeOptions = argUtils.toObject(processedArgv)
// Reset the state of the context
await ctx.reinitializeCypress(modeOptions)
// Handle any pre-loading that should occur based on the launch arg settings
await ctx.initializeMode()
return {
modeOptions,
e2eServerPort: ctx.appServerPort,
}
},
async __internal_openProject ({ argv, projectName }: InternalOpenProjectArgs): Promise<ResetOptionsResult> {
if (!scaffoldedProjects.has(projectName)) {
throw new Error(`${projectName} has not been scaffolded. Be sure to call cy.scaffoldProject('${projectName}') in the test, a before, or beforeEach hook`)
}
const openArgv = [...argv, '--project', Fixtures.projectPath(projectName), '--port', '4455']
// Runs the launchArgs through the whole pipeline for the CLI open process,
// which probably needs a bit of refactoring / consolidating
const cliOptions = await cli.parseOpenCommand(['open', ...openArgv])
const processedArgv = cliOpen.processOpenOptions(cliOptions)
const modeOptions = argUtils.toObject(processedArgv)
// Reset the state of the context
await ctx.reinitializeCypress(modeOptions)
// Handle any pre-loading that should occur based on the launch arg settings
await ctx.initializeMode()
return {
modeOptions,
e2eServerPort: ctx.appServerPort,
}
},
async __internal_withCtx (obj: WithCtxObj): Promise<CyTaskResult<any>> {
const options: WithCtxInjected = {
...obj.options,
testState,
require,
process,
sinon,
pDefer,
projectDir (projectName) {
if (!fixtureDirs.includes(projectName)) {
throw new Error(`${projectName} is not a fixture project`)
}
return Fixtures.projectPath(projectName)
},
}
let i = 0
let lastErr: Error | undefined
const retries = obj.options.retry ? obj.options.retryCount ?? 5 : 0
while (i++ <= retries) {
try {
const value = await Promise.resolve(new Function('ctx', 'options', 'chai', 'expect', 'sinon', `
return (${obj.fn})(ctx, options, chai, expect, sinon)
`).call(undefined, ctx, options, chai, expect, sinon))
return { value }
} catch (e: any) {
if (i <= retries) {
await new Promise((resolve) => setTimeout(resolve, obj.options.retryDelay ?? 1000))
}
lastErr = e
}
}
lastErr = lastErr || new Error('Error in withCtx')
return {
error: {
stack: lastErr.stack,
message: lastErr.message,
name: lastErr.name,
},
}
},
}
}