Skip to content

Commit

Permalink
Refactor handler
Browse files Browse the repository at this point in the history
  • Loading branch information
estrada9166 committed Nov 18, 2021
1 parent 46c4b34 commit 45ee157
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
3 changes: 0 additions & 3 deletions packages/server/__snapshots__/run_plugins_spec.js

This file was deleted.

6 changes: 0 additions & 6 deletions packages/server/lib/plugins/child/run_plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,6 @@ class RunPlugins {
}

runSetupNodeEvents (setupNodeEvents) {
if (setupNodeEvents && typeof setupNodeEvents !== 'function') {
this.ipc.send('load:error:plugins', 'SETUP_NODE_EVENTS_IS_NOT_FUNCTION', this.requiredFile, setupNodeEvents)
}

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

debug('project root:', this.projectRoot)
if (!this.projectRoot) {
throw new Error('Unexpected: projectRoot should be a string')
Expand Down
26 changes: 18 additions & 8 deletions packages/server/lib/util/run_require_async_child.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ function run (ipc, requiredFile, projectRoot) {
return false
})

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

return
}

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

ipc.on('load', () => {
try {
debug('try loading', requiredFile)
Expand All @@ -61,19 +71,19 @@ function run (ipc, requiredFile, projectRoot) {

areSetupNodeEventsLoaded = true
if (testingType === 'component') {
runPlugins.runSetupNodeEvents(result.component?.setupNodeEvents)
const setupNodeEvents = validateOrDefaultSetupNodeEvents(result.component?.setupNodeEvents)

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

return config
}

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

runPlugins.runSetupNodeEvents(setupNodeEvents)
} else {
// Notify the plugins init that there's no plugins to resolve
ipc.send('empty:plugins')
Expand Down
9 changes: 0 additions & 9 deletions packages/server/test/unit/plugins/child/run_plugins_spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require('../../../spec_helper')

const _ = require('lodash')
const snapshot = require('snap-shot-it')
const Promise = require('bluebird')

const preprocessor = require(`${root}../../lib/plugins/child/preprocessor`)
Expand Down Expand Up @@ -42,14 +41,6 @@ describe('lib/plugins/child/run_plugins', () => {
mockery.deregisterMock('@cypress/webpack-batteries-included-preprocessor')
})

it('sends error message if setupNodeEvents is not a function', function () {
runPlugins.runSetupNodeEvents('plugins-file')

expect(this.ipc.send).to.be.calledWith('load:error:plugins', 'SETUP_NODE_EVENTS_IS_NOT_FUNCTION', 'cypress.config.js')

return snapshot(this.ipc.send.lastCall.args[3].split('\n')[0])
})

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

0 comments on commit 45ee157

Please sign in to comment.