From 0618ba4ee4d91e86d576a9c4a4473d1c508bfb4b Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Wed, 6 Dec 2023 15:53:27 -0500 Subject: [PATCH] fix(testing): recalculate cypress targets when cypress config changes (#20593) --- packages/cypress/src/plugins/plugin.ts | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/cypress/src/plugins/plugin.ts b/packages/cypress/src/plugins/plugin.ts index 14f60ebeb1532..0a97ae00a126a 100644 --- a/packages/cypress/src/plugins/plugin.ts +++ b/packages/cypress/src/plugins/plugin.ts @@ -263,15 +263,15 @@ function getCypressConfig( if (tsConfigPath) { const unregisterTsProject = registerTsProject(tsConfigPath); try { - module = require(resolvedPath); + module = load(resolvedPath); } finally { unregisterTsProject(); } } else { - module = require(resolvedPath); + module = load(resolvedPath); } } else { - module = require(resolvedPath); + module = load(resolvedPath); } return module.default ?? module; } @@ -297,3 +297,18 @@ function getInputs( }, ]; } + +/** + * Load the module after ensuring that the require cache is cleared. + */ +function load(path: string): any { + // Clear cache if the path is in the cache + if (require.cache[path]) { + for (const k of Object.keys(require.cache)) { + delete require.cache[k]; + } + } + + // Then require + return require(path); +}