diff --git a/e2e/vite/src/vite-legacy.test.ts b/e2e/vite/src/vite-legacy.test.ts index 9711662ab3..25bcfb47c0 100644 --- a/e2e/vite/src/vite-legacy.test.ts +++ b/e2e/vite/src/vite-legacy.test.ts @@ -87,6 +87,18 @@ describe('Vite Plugin', () => { const result = runCLI(`test ${myApp}`); expect(result).toContain('Successfully ran target test'); }, 200_000); + + it('should generate a coverage file specified by the executor', async () => { + updateJson(`${myApp}/project.json`, (json) => { + json.targets.test.options.reportsDirectory = '../coverage/test-dir'; + return json; + }); + + const result = runCLI(`test ${myApp} --coverage`); + + checkFilesExist(`coverage/test-dir/index.html`); + expect(result).toContain('Coverage report'); + }, 200_000); }); }); diff --git a/packages/vite/src/executors/test/lib/utils.ts b/packages/vite/src/executors/test/lib/utils.ts index 3b36c1f113..9cd94b6547 100644 --- a/packages/vite/src/executors/test/lib/utils.ts +++ b/packages/vite/src/executors/test/lib/utils.ts @@ -67,15 +67,22 @@ export async function getOptions( options: { watch, ...normalizedExtraArgs }, } = parseCLI(['vitest', ...getOptionsAsArgv(options)]); + const { reportsDirectory, coverage, ...restNormalizedArgs } = + normalizedExtraArgs as Record; + const settings = { // Explicitly set watch mode to false if not provided otherwise vitest // will enable watch mode by default for non CI environments watch: watch ?? false, - ...normalizedExtraArgs, + ...restNormalizedArgs, // This should not be needed as it's going to be set in vite.config.ts // but leaving it here in case someone did not migrate correctly root: resolved.config.root ?? root, config: viteConfigPath, + coverage: { + ...(coverage ?? {}), + ...(reportsDirectory && { reportsDirectory }), + }, }; return mergeConfig(resolved?.config?.['test'] ?? {}, settings);