Skip to content

Commit

Permalink
cherry-pick(#16481): fix(ct): pass local config to preview
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelfeldman committed Aug 15, 2022
1 parent 10fcbe3 commit 817a29b
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
47 changes: 26 additions & 21 deletions packages/playwright-test/src/plugins/vitePlugin.ts
Expand Up @@ -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;
Expand Down
27 changes: 27 additions & 0 deletions tests/playwright-test/playwright.ct-build.spec.ts
Expand Up @@ -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': `<script type="module" src="/playwright/index.js"></script>`,
'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);
});

0 comments on commit 817a29b

Please sign in to comment.