From 27c425dda318c316f751a000b77b39fe727999d5 Mon Sep 17 00:00:00 2001 From: Matt Schile Date: Fri, 2 Dec 2022 13:32:03 -0700 Subject: [PATCH] fix(breaking): exclude node_modules from cross-origin-callback-loader (#24952) * fix: exclude node_modules from cross-origin-callback-loader * Update system-tests/test/cy_origin_error_spec.ts Co-authored-by: Chris Breiding * Update system-tests/projects/origin-dependencies/.gitignore Co-authored-by: Ryan Manuel Co-authored-by: Chris Breiding Co-authored-by: Matt Henkes Co-authored-by: Ryan Manuel --- npm/webpack-preprocessor/index.ts | 3 ++- packages/driver/src/cypress/error_messages.ts | 4 +++- .../projects/origin-dependencies/.gitignore | 1 + .../projects/origin-dependencies/cypress.config.js | 5 +++++ .../cypress/e2e/cross_origin.cy.js | 3 +++ .../origin-dependencies/cypress/support/e2e.js | 1 + .../node_modules/custom-origin-command/index.js | 5 +++++ system-tests/test/cy_origin_error_spec.ts | 14 ++++++++++++++ 8 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 system-tests/projects/origin-dependencies/.gitignore create mode 100644 system-tests/projects/origin-dependencies/cypress.config.js create mode 100644 system-tests/projects/origin-dependencies/cypress/e2e/cross_origin.cy.js create mode 100644 system-tests/projects/origin-dependencies/cypress/support/e2e.js create mode 100644 system-tests/projects/origin-dependencies/node_modules/custom-origin-command/index.js diff --git a/npm/webpack-preprocessor/index.ts b/npm/webpack-preprocessor/index.ts index 3b93af15b35b..c61f9bf12291 100644 --- a/npm/webpack-preprocessor/index.ts +++ b/npm/webpack-preprocessor/index.ts @@ -222,7 +222,7 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F }) .tap((opts) => { if (opts.devtool === false) { - // disable any overrides if we've explictly turned off sourcemaps + // disable any overrides if we've explicitly turned off sourcemaps overrideSourceMaps(false, options.typescript) return @@ -248,6 +248,7 @@ const preprocessor: WebpackPreprocessor = (options: PreprocessorOptions = {}): F // so that it's working with plain javascript webpackOptions.module.rules.unshift({ test: /\.(js|ts|jsx|tsx)$/, + exclude: /node_modules/, use: [{ loader: require.resolve('@cypress/webpack-preprocessor/dist/lib/cross-origin-callback-loader.js'), options: { diff --git a/packages/driver/src/cypress/error_messages.ts b/packages/driver/src/cypress/error_messages.ts index 5340ae35757d..78d34e344eb3 100644 --- a/packages/driver/src/cypress/error_messages.ts +++ b/packages/driver/src/cypress/error_messages.ts @@ -1228,7 +1228,9 @@ export default { Variables must either be defined within the ${cmd('origin')} command or passed in using the args option. - Using \`require()\` or \`import()\` to include dependencies requires enabling the \`experimentalOriginDependencies\` flag and using the latest version of \`@cypress/webpack-preprocessor\`.`, + Using \`require()\` or \`import()\` to include dependencies requires enabling the \`experimentalOriginDependencies\` flag and using the latest version of \`@cypress/webpack-preprocessor\`. + + Note: Using \`require()\` or \`import()\` within ${cmd('origin')} from a \`node_modules\` plugin is not currently supported.`, }, callback_mixes_sync_and_async: { message: stripIndent`\ diff --git a/system-tests/projects/origin-dependencies/.gitignore b/system-tests/projects/origin-dependencies/.gitignore new file mode 100644 index 000000000000..cf4bab9ddde9 --- /dev/null +++ b/system-tests/projects/origin-dependencies/.gitignore @@ -0,0 +1 @@ +!node_modules diff --git a/system-tests/projects/origin-dependencies/cypress.config.js b/system-tests/projects/origin-dependencies/cypress.config.js new file mode 100644 index 000000000000..091d7c0451d5 --- /dev/null +++ b/system-tests/projects/origin-dependencies/cypress.config.js @@ -0,0 +1,5 @@ +module.exports = { + e2e: { + setupNodeEvents (on) {}, + }, +} diff --git a/system-tests/projects/origin-dependencies/cypress/e2e/cross_origin.cy.js b/system-tests/projects/origin-dependencies/cypress/e2e/cross_origin.cy.js new file mode 100644 index 000000000000..361985aeda1a --- /dev/null +++ b/system-tests/projects/origin-dependencies/cypress/e2e/cross_origin.cy.js @@ -0,0 +1,3 @@ +it('uses custom cy.origin command', () => { + cy.customOriginCommand() +}) diff --git a/system-tests/projects/origin-dependencies/cypress/support/e2e.js b/system-tests/projects/origin-dependencies/cypress/support/e2e.js new file mode 100644 index 000000000000..3125df81f8a8 --- /dev/null +++ b/system-tests/projects/origin-dependencies/cypress/support/e2e.js @@ -0,0 +1 @@ +import 'custom-origin-command' diff --git a/system-tests/projects/origin-dependencies/node_modules/custom-origin-command/index.js b/system-tests/projects/origin-dependencies/node_modules/custom-origin-command/index.js new file mode 100644 index 000000000000..c8ea51e8c29c --- /dev/null +++ b/system-tests/projects/origin-dependencies/node_modules/custom-origin-command/index.js @@ -0,0 +1,5 @@ +Cypress.Commands.add('customOriginCommand', () => { + cy.origin('foobar.com', () => { + require('lodash') + }) +}) diff --git a/system-tests/test/cy_origin_error_spec.ts b/system-tests/test/cy_origin_error_spec.ts index 810a9eb8c44b..29a620086477 100644 --- a/system-tests/test/cy_origin_error_spec.ts +++ b/system-tests/test/cy_origin_error_spec.ts @@ -72,4 +72,18 @@ describe('e2e cy.origin errors', () => { expect(res.stdout).to.contain('Using `require()` or `import()` to include dependencies requires enabling the `experimentalOriginDependencies` flag and using the latest version of `@cypress/webpack-preprocessor`') }, }) + + systemTests.it('errors when using a plugin that has a custom command that uses cy.origin with a dependency', { + browser: '!webkit', // TODO(webkit): cy.origin is not currently supported in webkit + project: 'origin-dependencies', + spec: 'cross_origin.cy.js', + expectedExitCode: 1, + config: commonConfig, + async onRun (exec) { + const res = await exec() + + expect(res.stdout).to.contain('Using `require()` or `import()` to include dependencies requires enabling the `experimentalOriginDependencies` flag and using the latest version of `@cypress/webpack-preprocessor`') + expect(res.stdout).to.contain('Note: Using `require()` or `import()` within `cy.origin()` from a `node_modules` plugin is not currently supported.') + }, + }) })