Files
cypress/packages/server/lib/socket-e2e.ts
Lachlan Miller 72b8a65e78 feat: Cypress Studio for Cypress 10 (#23544)
* chore: wire up Cypress Studio  (#23413)

* wip

* wip

* wip - spike

* more wip [skip ci]

* update style

* fix ts

* move types around

* extract types

* lint

* fixing tests

* fix component test

* skip some tests

* do not error on experimentalStudio flag

* add studio controls placeholder

* fixing tests

* revert

* revert changes

* rename store

* rename method

* remove comment

* refactor

* correctly feature flag studio

* simplify code

* simplify code

* lift check into useEventManager

* correctly hide create studio prompt based on flag;

* remove superfulous css

* rename variables

* fix bugs

* wip

* unskip tests

* unskip more tests

* fix a bug in the assertion API

* fix bug in assertions [skip ci]

* wip - bugs [skip ci]

* feat: add experimentalStudio flag back (#23506)

Co-authored-by: astone123 <adams@cypress.io>

* chore: Add Studio UI to Cypress 10 (#23537)

* wip

* wip

* wip - spike

* more wip [skip ci]

* update style

* fix ts

* move types around

* extract types

* lint

* fixing tests

* fix component test

* skip some tests

* do not error on experimentalStudio flag

* add studio controls placeholder

* fixing tests

* revert

* revert changes

* rename store

* rename method

* remove comment

* refactor

* correctly feature flag studio

* chore: wip add barebones studio modals

* simplify code

* simplify code

* lift check into useEventManager

* correctly hide create studio prompt based on flag;

* remove superfulous css

* chore: style studio toolbar

* chore: misc feedback

* chore: remove studio store prop

* chore: studio URL prompt and other changes

* update component

* chore: UI styling and remove studio init modal

* chore: revert unnecessary changes

* chore: fix types

* chore: fix some tests, minor refactor (#23545)

* fix test

* fix test

* add noHelp link to StandardModal

Co-authored-by: Lachlan Miller <lachlan.miller.1990@outlook.com>

* test: studio e2e tests (#23546)

* add basic e2e test

* add some e2e tests for studio and a note on limitations

* additional spec

* add more tests, refactor helper

* fix bug in studio

* remove test code

* chore: UI feedback

* fix race condition

* update tests

* rename test

* improve types in reporter

* remove dead code

* improve tests

* merge tests into one spec

* chore: Cap instruction modal width; exit studio mode when new spec is chosen

* chore: Only render studio error when test has failed; add test for studioEnabled

* correctly check if command is studio or not

* improve specs and hopefully reduce flake

* communicate studio state from app->reporter

* receive studio save state validity from app

* fix test

* improve test coverage

* fix external link

Co-authored-by: astone123 <adams@cypress.io>
2022-08-30 07:45:06 +10:00

139 lines
4.3 KiB
TypeScript

import Debug from 'debug'
import preprocessor from './plugins/preprocessor'
import { SocketBase } from './socket-base'
import { fs } from './util/fs'
import type { DestroyableHttpServer } from './util/server_destroy'
import * as studio from './studio'
import type { FoundSpec } from '@packages/types'
const debug = Debug('cypress:server:socket-e2e')
const isSpecialSpec = (name) => {
return name.endsWith('__all')
}
export class SocketE2E extends SocketBase {
private testFilePath: string | null
constructor (config: Record<string, any>) {
super(config)
this.testFilePath = null
this.onTestFileChange = this.onTestFileChange.bind(this)
this.onStudioTestFileChange = this.onStudioTestFileChange.bind(this)
this.removeOnStudioTestFileChange = this.removeOnStudioTestFileChange.bind(this)
if (config.watchForFileChanges) {
preprocessor.emitter.on('file:updated', this.onTestFileChange)
}
}
onStudioTestFileChange (filePath) {
// wait for the studio test file to be written to disk, then reload the test
// and remove the listener (since this handler is only invoked when watchForFileChanges is false)
return this.onTestFileChange(filePath).then(() => {
this.removeOnStudioTestFileChange()
})
}
removeOnStudioTestFileChange () {
return preprocessor.emitter.off('file:updated', this.onStudioTestFileChange)
}
onTestFileChange = (filePath) => {
debug('test file changed %o', filePath)
return fs.statAsync(filePath)
.then(() => {
return this._io?.emit('watched:file:changed')
}).catch(() => {
return debug('could not find test file that changed %o', filePath)
})
}
watchTestFileByPath (config, specConfig: FoundSpec) {
debug('watching spec with config %o', specConfig)
// previously we have assumed that we pass integration spec path with "integration/" prefix
// now we pass spec config object that tells what kind of spec it is, has relative path already
// so the only special handling remains for special paths like "integration/__all"
// bail if this is special path like "__all"
// maybe the client should not ask to watch non-spec files?
if (isSpecialSpec(specConfig.relative)) {
return
}
if (specConfig.relative.startsWith('/')) {
specConfig.relative = specConfig.relative.slice(1)
}
// bail if we're already watching this exact file
if (specConfig.relative === this.testFilePath) {
return
}
// remove the existing file by its path
if (this.testFilePath) {
preprocessor.removeFile(this.testFilePath, config)
}
// store this location
this.testFilePath = specConfig.relative
debug('will watch test file path %o', specConfig.relative)
return preprocessor.getFile(specConfig.relative, config)
// ignore errors b/c we're just setting up the watching. errors
// are handled by the spec controller
.catch(() => {})
}
startListening (server: DestroyableHttpServer, automation, config, options) {
return super.startListening(server, automation, config, options, {
onSocketConnection: (socket) => {
socket.on('watch:test:file', (specInfo: FoundSpec, cb = function () { }) => {
debug('watch:test:file %o', specInfo)
this.watchTestFileByPath(config, specInfo)
// callback is only for testing purposes
return cb()
})
socket.on('studio:save', (saveInfo, cb) => {
// even if the user has turned off file watching
// we want to force a reload on save
if (!config.watchForFileChanges) {
preprocessor.emitter.on('file:updated', this.onStudioTestFileChange)
}
studio.save(saveInfo)
.then((err) => {
cb(err)
// onStudioTestFileChange will remove itself after being called
// but if there's an error, it never gets called so we manually remove it
if (err && !config.watchForFileChanges) {
this.removeOnStudioTestFileChange()
}
})
.catch(() => {})
})
socket.on('studio:get:commands:text', (commands, cb) => {
const commandsText = studio.getCommandsText(commands)
cb(commandsText)
})
},
})
}
close () {
preprocessor.emitter.removeListener('file:updated', this.onTestFileChange)
return super.close()
}
}