From e1d3246d1cfb7dafdd0294a60e52430cd6d02d2e Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Fri, 12 Aug 2022 10:37:35 -0700 Subject: [PATCH] fix(ct): pass local config to preview (#16481) --- .../playwright-test/src/plugins/vitePlugin.ts | 47 ++++++++++--------- .../playwright.ct-build.spec.ts | 27 +++++++++++ 2 files changed, 53 insertions(+), 21 deletions(-) diff --git a/packages/playwright-test/src/plugins/vitePlugin.ts b/packages/playwright-test/src/plugins/vitePlugin.ts index afaf313dc4d71..21593f80ef38a 100644 --- a/packages/playwright-test/src/plugins/vitePlugin.ts +++ b/packages/playwright-test/src/plugins/vitePlugin.ts @@ -110,32 +110,37 @@ export function createPlugin( }; } const { build, preview } = require('vite'); - if (sourcesDirty) { - viteConfig.plugins = viteConfig.plugins || [ - frameworkPluginFactory() - ]; + // Build config unconditionally, either build or build & preview will use it. + viteConfig.plugins = viteConfig.plugins || [ + frameworkPluginFactory() + ]; + // But only add out own plugin when we actually build / transform. + if (sourcesDirty) viteConfig.plugins.push(vitePlugin(registerSource, relativeTemplateDir, buildInfo, componentRegistry)); - viteConfig.configFile = viteConfig.configFile || false; - viteConfig.define = viteConfig.define || {}; - viteConfig.define.__VUE_PROD_DEVTOOLS__ = true; - viteConfig.css = viteConfig.css || {}; - viteConfig.css.devSourcemap = true; - viteConfig.build = { - ...viteConfig.build, - target: 'esnext', - minify: false, - rollupOptions: { - treeshake: false, - input: { - index: path.join(templateDir, 'index.html') - }, + viteConfig.configFile = viteConfig.configFile || false; + viteConfig.define = viteConfig.define || {}; + viteConfig.define.__VUE_PROD_DEVTOOLS__ = true; + viteConfig.css = viteConfig.css || {}; + viteConfig.css.devSourcemap = true; + viteConfig.build = { + ...viteConfig.build, + target: 'esnext', + minify: false, + rollupOptions: { + treeshake: false, + input: { + index: path.join(templateDir, 'index.html') }, - sourcemap: true, - }; + }, + sourcemap: true, + }; + + if (sourcesDirty) await build(viteConfig); - } + if (hasNewTests || hasNewComponents || sourcesDirty) await fs.promises.writeFile(buildInfoFile, JSON.stringify(buildInfo, undefined, 2)); + const previewServer = await preview(viteConfig); stoppableServer = stoppable(previewServer.httpServer, 0); const isAddressInfo = (x: any): x is AddressInfo => x?.address; diff --git a/tests/playwright-test/playwright.ct-build.spec.ts b/tests/playwright-test/playwright.ct-build.spec.ts index cc1e8e7530c5e..c36449f95762b 100644 --- a/tests/playwright-test/playwright.ct-build.spec.ts +++ b/tests/playwright-test/playwright.ct-build.spec.ts @@ -267,3 +267,30 @@ test('should cache build', async ({ runInlineTest }, testInfo) => { expect(output, 'should rebuild bundle').toContain('modules transformed'); }); }); + +test('should not use global config for preview', async ({ runInlineTest }) => { + const result1 = await runInlineTest({ + 'playwright/index.html': ``, + 'playwright/index.js': ``, + 'vite.config.js': ` + export default { + plugins: [{ + configurePreviewServer: () => { + throw new Error('Original preview throws'); + } + }] + }; + `, + 'a.test.ts': ` + //@no-header + import { test, expect } from '@playwright/experimental-ct-react'; + test('pass', async ({ mount }) => {}); + `, + }, { workers: 1 }); + expect(result1.exitCode).toBe(0); + expect(result1.passed).toBe(1); + + const result2 = await runInlineTest({}, { workers: 1 }); + expect(result2.exitCode).toBe(0); + expect(result2.passed).toBe(1); +}); \ No newline at end of file