From 45ee157a2d2fcb7d04f91f55fd7d5f288b5b0a88 Mon Sep 17 00:00:00 2001 From: estrada9166 Date: Thu, 18 Nov 2021 14:55:55 -0500 Subject: [PATCH] Refactor handler --- .../server/__snapshots__/run_plugins_spec.js | 3 --- .../server/lib/plugins/child/run_plugins.js | 6 ----- .../lib/util/run_require_async_child.js | 26 +++++++++++++------ .../unit/plugins/child/run_plugins_spec.js | 9 ------- 4 files changed, 18 insertions(+), 26 deletions(-) delete mode 100644 packages/server/__snapshots__/run_plugins_spec.js diff --git a/packages/server/__snapshots__/run_plugins_spec.js b/packages/server/__snapshots__/run_plugins_spec.js deleted file mode 100644 index 17b05c9ab8ee..000000000000 --- a/packages/server/__snapshots__/run_plugins_spec.js +++ /dev/null @@ -1,3 +0,0 @@ -exports['lib/plugins/child/run_plugins sends error message if setupNodeEvents is not a function 1'] = ` -plugins-file -` diff --git a/packages/server/lib/plugins/child/run_plugins.js b/packages/server/lib/plugins/child/run_plugins.js index b9703b1f292d..007125ec8e47 100644 --- a/packages/server/lib/plugins/child/run_plugins.js +++ b/packages/server/lib/plugins/child/run_plugins.js @@ -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') diff --git a/packages/server/lib/util/run_require_async_child.js b/packages/server/lib/util/run_require_async_child.js index 99553e839caf..2c8f7bece745 100644 --- a/packages/server/lib/util/run_require_async_child.js +++ b/packages/server/lib/util/run_require_async_child.js @@ -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) @@ -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') diff --git a/packages/server/test/unit/plugins/child/run_plugins_spec.js b/packages/server/test/unit/plugins/child/run_plugins_spec.js index eedb94641e5c..c0e6277e9b91 100644 --- a/packages/server/test/unit/plugins/child/run_plugins_spec.js +++ b/packages/server/test/unit/plugins/child/run_plugins_spec.js @@ -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`) @@ -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()