Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite): add fix for vitest configuration #14404

Merged
merged 1 commit into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion e2e/vite/src/vite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ describe('Vite Plugin', () => {
it('should collect coverage', () => {
runCLI(`generate @nrwl/react:lib ${lib} --unitTestRunner=vitest`);
updateFile(`libs/${lib}/vite.config.ts`, () => {
return `import { defineConfig } from 'vite';
return `/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`@nrwl/vite:configuration library mode should add config for building library 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down Expand Up @@ -58,6 +59,7 @@ import { join } from 'path';

exports[`@nrwl/vite:configuration library mode should set up non buildable library correctly 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down Expand Up @@ -373,6 +375,7 @@ exports[`@nrwl/vite:configuration transform React app to use Vite by providing c

exports[`@nrwl/vite:configuration transform React app to use Vite should create vite.config file at the root of the app 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down Expand Up @@ -536,6 +539,7 @@ exports[`@nrwl/vite:configuration transform React app to use Vite should transfo

exports[`@nrwl/vite:configuration transform Web app to use Vite should create vite.config file at the root of the app 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';

import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down Expand Up @@ -688,6 +692,7 @@ exports[`@nrwl/vite:configuration transform Web app to use Vite should transform

exports[`@nrwl/vite:configuration vitest should create a vitest configuration if "includeVitest" is true 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`vitest generator insourceTests should add the insourceSource option in the vite config 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down Expand Up @@ -46,6 +47,7 @@ exports[`vitest generator insourceTests should add the insourceSource option in

exports[`vitest generator vite.config should create correct vite.config.ts file for apps 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down Expand Up @@ -88,6 +90,7 @@ exports[`vitest generator vite.config should create correct vite.config.ts file

exports[`vitest generator vite.config should create correct vite.config.ts file for non buildable libs 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`ensureBuildOptionsInViteConfig should add build and test options if def
"import dts from 'vite-plugin-dts';
import { join } from 'path';

/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down Expand Up @@ -203,6 +204,7 @@ import { defineConfig } from 'vite';

exports[`ensureBuildOptionsInViteConfig should add build options if it is using conditional config - do nothing for test 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
export default defineConfig(({ command, mode, ssrBuild }) => {
if (command === 'serve') {
Expand Down Expand Up @@ -269,6 +271,7 @@ exports[`ensureBuildOptionsInViteConfig should not do anything if cannot underst

exports[`ensureBuildOptionsInViteConfig should not do anything if project has everything setup already 1`] = `
"
/// <reference types=\\"vitest\\" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/utils/generator-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ export function createOrEditViteConfig(
}

viteConfigContent = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
${reactPluginImportLine}
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/src/utils/test-files/test-vite-configs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const noBuildOptions = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand All @@ -24,6 +25,7 @@ export const noBuildOptions = `
`;

export const someBuildOptions = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down Expand Up @@ -53,6 +55,7 @@ export const someBuildOptions = `
`;

export const noContentDefineConfig = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand All @@ -61,6 +64,7 @@ export const noContentDefineConfig = `
`;

export const conditionalConfig = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
export default defineConfig(({ command, mode, ssrBuild }) => {
if (command === 'serve') {
Expand All @@ -78,6 +82,7 @@ export const conditionalConfig = `
`;

export const configNoDefineConfig = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand All @@ -93,6 +98,7 @@ export const configNoDefineConfig = `
`;

export const noBuildOptionsHasTestOption = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand All @@ -118,6 +124,7 @@ export const noBuildOptionsHasTestOption = `
`;

export const someBuildOptionsSomeTestOption = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand All @@ -142,6 +149,7 @@ export const someBuildOptionsSomeTestOption = `
`;

export const hasEverything = `
/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/utils/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export function mockViteReactAppGenerator(tree: Tree): Tree {

tree.write(
`apps/${appName}/vite.config.ts`,
`import { defineConfig } from 'vite';
` /// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';

Expand Down Expand Up @@ -533,7 +534,8 @@ export function mockReactLibNonBuildableVitestRunnerGenerator(

tree.write(
`libs/${libName}/vite.config.ts`,
`import { defineConfig } from 'vite';
`/// <reference types="vitest" />
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import viteTsConfigPaths from 'vite-tsconfig-paths';

Expand Down