Skip to content

Commit

Permalink
Refactor error, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
estrada9166 committed Nov 18, 2021
1 parent 45ee157 commit c2ba257
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/frontend-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"windi": "yarn windicss-analysis",
"cypress:open": "cross-env TZ=America/New_York node ../../scripts/cypress open --component --project ${PWD}",
"cypress:run:e2e": "echo 'no e2e in frontend-shared'",
"cypress:open:ct": "cross-env TZ=America/New_York node ../../scripts/cypress open --component --project ${PWD}",
"cypress:run:ct": "cross-env TZ=America/New_York node ../../scripts/cypress run --component --project ${PWD}",
"dev": "vite --open",
"generate-shiki-theme": "node ./script/generate-shiki-theme.js"
Expand Down
6 changes: 6 additions & 0 deletions packages/server/lib/plugins/child/run_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class RunPlugins {
return
}

if (event === 'dev-server:start' && registeredEventsByName[event]) {
this.ipc.send('load:error:plugins', 'SETUP_NODE_EVENTS_DO_NOT_SUPPORT_DEV_SERVER', this.requiredFile)

return
}

if (event === 'task') {
const existingEventId = registeredEventsByName[event]

Expand Down
18 changes: 13 additions & 5 deletions packages/server/lib/util/run_require_async_child.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ function run (ipc, requiredFile, projectRoot) {
return false
})

const validateOrDefaultSetupNodeEvents = (setupNodeEvents) => {
const isValidSetupNodeEvents = (setupNodeEvents) => {
if (setupNodeEvents && typeof setupNodeEvents !== 'function') {
ipc.send('load:error:plugins', 'SETUP_NODE_EVENTS_IS_NOT_FUNCTION', requiredFile, setupNodeEvents)

return
return false
}

return setupNodeEvents ?? ((on, config) => {})
return true
}

ipc.on('load', () => {
Expand All @@ -71,17 +71,25 @@ function run (ipc, requiredFile, projectRoot) {

areSetupNodeEventsLoaded = true
if (testingType === 'component') {
const setupNodeEvents = validateOrDefaultSetupNodeEvents(result.component?.setupNodeEvents)
if (!isValidSetupNodeEvents(result.component?.setupNodeEvents)) {
return
}

runPlugins.runSetupNodeEvents((on, config) => {
if (result.component?.devServer) {
on('dev-server:start', (options) => result.component.devServer(options, result.component?.devServerConfig))
}

const setupNodeEvents = result.component?.setupNodeEvents ?? ((on, config) => {})

return setupNodeEvents(on, config)
})
} else if (testingType === 'e2e') {
const setupNodeEvents = validateOrDefaultSetupNodeEvents(result.e2e?.setupNodeEvents)
if (!isValidSetupNodeEvents(result.e2e?.setupNodeEvents)) {
return
}

const setupNodeEvents = result.e2e?.setupNodeEvents ?? ((on, config) => {})

runPlugins.runSetupNodeEvents(setupNodeEvents)
} else {
Expand Down
29 changes: 29 additions & 0 deletions packages/server/test/unit/plugins/child/run_plugins_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,35 @@ describe('lib/plugins/child/run_plugins', () => {
mockery.deregisterMock('@cypress/webpack-batteries-included-preprocessor')
})

it('sends error message if setupNodeEvents is not a function', function () {
const config = { projectRoot: '/project/root' }

const setupNodeEventsFn = (on, config) => {
on('dev-server:start', (options) => {})
on('after:screenshot', () => {})
on('task', {})

return config
}

const foo = ((on, config) => {
on('dev-server:start', (options) => {})

return setupNodeEventsFn(on, config)
})

runPlugins.runSetupNodeEvents(foo)

this.ipc.on.withArgs('load:plugins').yield(config)

return Promise
.delay(10)
.then(() => {
expect(this.ipc.send).to.be.calledWith('loaded:plugins', config)
expect(this.ipc.send).to.be.calledWith('load:error:plugins', 'SETUP_NODE_EVENTS_DO_NOT_SUPPORT_DEV_SERVER', 'cypress.config.js')
})
})

describe('on \'load\' message', () => {
it('sends loaded event with registrations', function () {
const pluginsDeferred = deferred()
Expand Down

0 comments on commit c2ba257

Please sign in to comment.