From 5fea49a9803269a2a7e12909714765557eb38ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Mon, 13 May 2024 20:14:43 +0200 Subject: [PATCH] fix(vite): generate vitest cache dir scoped to each project root and normalize vite cache dir (#23330) - Update Vitest `cache.dir` to be scoped to each project root to avoid collisions (same as what's already done for Vite) - Normalize Vite `cacheDir` to avoid trailing `/.` paths for root projects - Fix Remix & Storybook Vite `cacheDir` generation ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes #21775 --- docs/shared/packages/vite/configure-vite.md | 12 +- docs/shared/recipes/add-stack/add-solid.md | 4 +- .../__snapshots__/library.spec.ts.snap | 2 +- .../__snapshots__/application.spec.ts.snap | 2 +- .../application.legacy.spec.ts.snap | 1232 +---------------- .../__snapshots__/application.spec.ts.snap | 6 +- .../application/application.legacy.spec.ts | 4 +- .../__snapshots__/library.spec.ts.snap | 4 +- .../application.impl.spec.ts.snap | 8 +- .../application/application.impl.spec.ts | 11 +- .../__snapshots__/library.impl.spec.ts.snap | 4 +- .../storybook-configuration.impl.spec.ts.snap | 70 + .../files/vite.config.ts__tpl__ | 2 +- .../storybook-configuration.impl.spec.ts | 4 +- .../storybook-configuration.impl.ts | 16 +- .../__snapshots__/configuration.spec.ts.snap | 6 +- .../vitest/__snapshots__/vitest.spec.ts.snap | 6 +- packages/vite/src/utils/generator-utils.ts | 31 +- .../__snapshots__/application.spec.ts.snap | 2 +- .../application/application.spec.ts | 2 + .../__snapshots__/library.spec.ts.snap | 4 +- .../src/generators/library/library.spec.ts | 2 + .../preset/__snapshots__/preset.spec.ts.snap | 10 +- .../src/generators/preset/preset.spec.ts | 4 - 24 files changed, 218 insertions(+), 1230 deletions(-) diff --git a/docs/shared/packages/vite/configure-vite.md b/docs/shared/packages/vite/configure-vite.md index 9b1f9a43fcbe9..fb1487a2945ef 100644 --- a/docs/shared/packages/vite/configure-vite.md +++ b/docs/shared/packages/vite/configure-vite.md @@ -88,7 +88,7 @@ If you're using `vitest`, make sure your `test` object in your `vite.config.ts` test: { globals: true, cache: { - dir: '../node_modules/.vitest', + dir: '../node_modules/.vitest/', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -124,7 +124,7 @@ export default defineConfig({ transformMixedEsModules: true, }, }, - cacheDir: '../../node_modules/.vite/my-app', + cacheDir: '../../node_modules/.vite/apps/my-app', server: { port: 4200, host: 'localhost', @@ -145,7 +145,7 @@ export default defineConfig({ }, globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/apps/my-app', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -180,7 +180,7 @@ export default defineConfig({ // Configuration for building your library. // See: https://vitejs.dev/guide/build.html#library-mode build: { - outDir: '../dist/my-lib', + outDir: '../dist/libs/my-lib', reportCompressedSize: true, commonjsOptions: { transformMixedEsModules: true, @@ -202,13 +202,13 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../node_modules/.vitest', + dir: '../node_modules/.vitest/libs/my-lib', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], reporters: ['default'], coverage: { - reportsDirectory: '../coverage/my-lib', + reportsDirectory: '../coverage/libs/my-lib', provider: 'v8', }, }, diff --git a/docs/shared/recipes/add-stack/add-solid.md b/docs/shared/recipes/add-stack/add-solid.md index 131991bc2f112..17fc9bc5827ac 100644 --- a/docs/shared/recipes/add-stack/add-solid.md +++ b/docs/shared/recipes/add-stack/add-solid.md @@ -153,7 +153,7 @@ import solidPlugin from 'vite-plugin-solid'; import viteTsConfigPaths from 'vite-tsconfig-paths'; export default defineConfig({ - cacheDir: '../../node_modules/.vite/my-solid-app', + cacheDir: '../../node_modules/.vite/apps/my-solid-app', server: { port: 3000, @@ -187,7 +187,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/apps/my-solid-app', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/js/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/js/src/generators/library/__snapshots__/library.spec.ts.snap index 993be819d5fc9..33bea35d9df15 100644 --- a/packages/js/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/js/src/generators/library/__snapshots__/library.spec.ts.snap @@ -50,7 +50,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../node_modules/.vitest', + dir: '../node_modules/.vitest/my-lib', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap b/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap index e56ad4e991aec..ed0f67a268cc6 100644 --- a/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap +++ b/packages/nuxt/src/generators/application/__snapshots__/application.spec.ts.snap @@ -106,7 +106,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../node_modules/.vitest', + dir: '../node_modules/.vitest/my-app', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/react/src/generators/application/__snapshots__/application.legacy.spec.ts.snap b/packages/react/src/generators/application/__snapshots__/application.legacy.spec.ts.snap index c0117daac9794..874086306902c 100644 --- a/packages/react/src/generators/application/__snapshots__/application.legacy.spec.ts.snap +++ b/packages/react/src/generators/application/__snapshots__/application.legacy.spec.ts.snap @@ -1,1184 +1,56 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`react app generator (legacy) should setup vite 1`] = ` -{ - "data": [ - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 47, - 47, - 47, - 32, - 60, - 114, - 101, - 102, - 101, - 114, - 101, - 110, - 99, - 101, - 32, - 116, - 121, - 112, - 101, - 115, - 61, - 39, - 118, - 105, - 116, - 101, - 115, - 116, - 39, - 32, - 47, - 62, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 105, - 109, - 112, - 111, - 114, - 116, - 32, - 123, - 32, - 100, - 101, - 102, - 105, - 110, - 101, - 67, - 111, - 110, - 102, - 105, - 103, - 32, - 125, - 32, - 102, - 114, - 111, - 109, - 32, - 39, - 118, - 105, - 116, - 101, - 39, - 59, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 105, - 109, - 112, - 111, - 114, - 116, - 32, - 114, - 101, - 97, - 99, - 116, - 32, - 102, - 114, - 111, - 109, - 32, - 39, - 64, - 118, - 105, - 116, - 101, - 106, - 115, - 47, - 112, - 108, - 117, - 103, - 105, - 110, - 45, - 114, - 101, - 97, - 99, - 116, - 39, - 59, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 105, - 109, - 112, - 111, - 114, - 116, - 32, - 123, - 32, - 110, - 120, - 86, - 105, - 116, - 101, - 84, - 115, - 80, - 97, - 116, - 104, - 115, - 32, - 125, - 32, - 102, - 114, - 111, - 109, - 32, - 39, - 64, - 110, - 120, - 47, - 118, - 105, - 116, - 101, - 47, - 112, - 108, - 117, - 103, - 105, - 110, - 115, - 47, - 110, - 120, - 45, - 116, - 115, - 99, - 111, - 110, - 102, - 105, - 103, - 45, - 112, - 97, - 116, - 104, - 115, - 46, - 112, - 108, - 117, - 103, - 105, - 110, - 39, - 59, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 101, - 120, - 112, - 111, - 114, - 116, - 32, - 100, - 101, - 102, - 97, - 117, - 108, - 116, - 32, - 100, - 101, - 102, - 105, - 110, - 101, - 67, - 111, - 110, - 102, - 105, - 103, - 40, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 114, - 111, - 111, - 116, - 58, - 32, - 95, - 95, - 100, - 105, - 114, - 110, - 97, - 109, - 101, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 99, - 97, - 99, - 104, - 101, - 68, - 105, - 114, - 58, - 32, - 39, - 46, - 46, - 47, - 110, - 111, - 100, - 101, - 95, - 109, - 111, - 100, - 117, - 108, - 101, - 115, - 47, - 46, - 118, - 105, - 116, - 101, - 47, - 109, - 121, - 45, - 118, - 105, - 116, - 101, - 45, - 97, - 112, - 112, - 39, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 115, - 101, - 114, - 118, - 101, - 114, - 58, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 112, - 111, - 114, - 116, - 58, - 32, - 52, - 50, - 48, - 48, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 104, - 111, - 115, - 116, - 58, - 32, - 39, - 108, - 111, - 99, - 97, - 108, - 104, - 111, - 115, - 116, - 39, - 44, - 10, - 32, - 32, - 32, - 32, - 125, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 112, - 114, - 101, - 118, - 105, - 101, - 119, - 58, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 112, - 111, - 114, - 116, - 58, - 32, - 52, - 51, - 48, - 48, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 104, - 111, - 115, - 116, - 58, - 32, - 39, - 108, - 111, - 99, - 97, - 108, - 104, - 111, - 115, - 116, - 39, - 44, - 10, - 32, - 32, - 32, - 32, - 125, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 112, - 108, - 117, - 103, - 105, - 110, - 115, - 58, - 32, - 91, - 114, - 101, - 97, - 99, - 116, - 40, - 41, - 44, - 10, - 110, - 120, - 86, - 105, - 116, - 101, - 84, - 115, - 80, - 97, - 116, - 104, - 115, - 40, - 41, - 93, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 47, - 47, - 32, - 85, - 110, - 99, - 111, - 109, - 109, - 101, - 110, - 116, - 32, - 116, - 104, - 105, - 115, - 32, - 105, - 102, - 32, - 121, - 111, - 117, - 32, - 97, - 114, - 101, - 32, - 117, - 115, - 105, - 110, - 103, - 32, - 119, - 111, - 114, - 107, - 101, - 114, - 115, - 46, - 32, - 10, - 32, - 32, - 32, - 32, - 47, - 47, - 32, - 119, - 111, - 114, - 107, - 101, - 114, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 47, - 47, - 32, - 32, - 112, - 108, - 117, - 103, - 105, - 110, - 115, - 58, - 32, - 91, - 32, - 110, - 120, - 86, - 105, - 116, - 101, - 84, - 115, - 80, - 97, - 116, - 104, - 115, - 40, - 41, - 32, - 93, - 44, - 10, - 32, - 32, - 32, - 32, - 47, - 47, - 32, - 125, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 98, - 117, - 105, - 108, - 100, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 111, - 117, - 116, - 68, - 105, - 114, - 58, - 32, - 39, - 46, - 46, - 47, - 100, - 105, - 115, - 116, - 47, - 109, - 121, - 45, - 118, - 105, - 116, - 101, - 45, - 97, - 112, - 112, - 39, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 114, - 101, - 112, - 111, - 114, - 116, - 67, - 111, - 109, - 112, - 114, - 101, - 115, - 115, - 101, - 100, - 83, - 105, - 122, - 101, - 58, - 32, - 116, - 114, - 117, - 101, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 99, - 111, - 109, - 109, - 111, - 110, - 106, - 115, - 79, - 112, - 116, - 105, - 111, - 110, - 115, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 116, - 114, - 97, - 110, - 115, - 102, - 111, - 114, - 109, - 77, - 105, - 120, - 101, - 100, - 69, - 115, - 77, - 111, - 100, - 117, - 108, - 101, - 115, - 58, - 32, - 116, - 114, - 117, - 101, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 125, - 44, - 10, - 32, - 32, - 32, - 32, - 125, - 44, - 10, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 32, - 116, - 101, - 115, - 116, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 103, - 108, - 111, - 98, - 97, - 108, - 115, - 58, - 32, - 116, - 114, - 117, - 101, - 44, - 10, - 32, - 32, - 32, - 32, - 99, - 97, - 99, - 104, - 101, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 100, - 105, - 114, - 58, - 32, - 39, - 46, - 46, - 47, - 110, - 111, - 100, - 101, - 95, - 109, - 111, - 100, - 117, - 108, - 101, - 115, - 47, - 46, - 118, - 105, - 116, - 101, - 115, - 116, - 39, - 10, - 32, - 32, - 32, - 32, - 125, - 44, - 10, - 32, - 32, - 32, - 32, - 101, - 110, - 118, - 105, - 114, - 111, - 110, - 109, - 101, - 110, - 116, - 58, - 32, - 39, - 106, - 115, - 100, - 111, - 109, - 39, - 44, - 10, - 32, - 32, - 32, - 32, - 105, - 110, - 99, - 108, - 117, - 100, - 101, - 58, - 32, - 91, - 39, - 115, - 114, - 99, - 47, - 42, - 42, - 47, - 42, - 46, - 123, - 116, - 101, - 115, - 116, - 44, - 115, - 112, - 101, - 99, - 125, - 46, - 123, - 106, - 115, - 44, - 109, - 106, - 115, - 44, - 99, - 106, - 115, - 44, - 116, - 115, - 44, - 109, - 116, - 115, - 44, - 99, - 116, - 115, - 44, - 106, - 115, - 120, - 44, - 116, - 115, - 120, - 125, - 39, - 93, - 44, - 10, - 32, - 32, - 32, - 32, - 10, - 32, - 32, - 32, - 32, - 114, - 101, - 112, - 111, - 114, - 116, - 101, - 114, - 115, - 58, - 32, - 91, - 39, - 100, - 101, - 102, - 97, - 117, - 108, - 116, - 39, - 93, - 44, - 10, - 32, - 32, - 32, - 32, - 99, - 111, - 118, - 101, - 114, - 97, - 103, - 101, - 58, - 32, - 123, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 114, - 101, - 112, - 111, - 114, - 116, - 115, - 68, - 105, - 114, - 101, - 99, - 116, - 111, - 114, - 121, - 58, - 32, - 39, - 46, - 46, - 47, - 99, - 111, - 118, - 101, - 114, - 97, - 103, - 101, - 47, - 109, - 121, - 45, - 118, - 105, - 116, - 101, - 45, - 97, - 112, - 112, - 39, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 112, - 114, - 111, - 118, - 105, - 100, - 101, - 114, - 58, - 32, - 39, - 118, - 56, - 39, - 44, - 10, - 32, - 32, - 32, - 32, - 125, - 10, - 32, - 32, - 125, - 44, - 10, - 32, - 32, - 32, - 32, - 32, - 32, - 125, - 41, - 59, - ], - "type": "Buffer", -} +" + /// + import { defineConfig } from 'vite'; + import react from '@vitejs/plugin-react'; + import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; + + export default defineConfig({ + root: __dirname, + cacheDir: '../node_modules/.vite/my-vite-app', + + server:{ + port: 4200, + host: 'localhost', + }, + + preview:{ + port: 4300, + host: 'localhost', + }, + + plugins: [react(), +nxViteTsPaths()], + + // Uncomment this if you are using workers. + // worker: { + // plugins: [ nxViteTsPaths() ], + // }, + + build: { + outDir: '../dist/my-vite-app', + reportCompressedSize: true, + commonjsOptions: { + transformMixedEsModules: true, + }, + }, + + + test: { + globals: true, + cache: { + dir: '../node_modules/.vitest/my-vite-app' + }, + environment: 'jsdom', + include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], + + reporters: ['default'], + coverage: { + reportsDirectory: '../coverage/my-vite-app', + provider: 'v8', + } + }, + });" `; diff --git a/packages/react/src/generators/application/__snapshots__/application.spec.ts.snap b/packages/react/src/generators/application/__snapshots__/application.spec.ts.snap index 42d410a1916c0..95ab50d478838 100644 --- a/packages/react/src/generators/application/__snapshots__/application.spec.ts.snap +++ b/packages/react/src/generators/application/__snapshots__/application.spec.ts.snap @@ -129,7 +129,7 @@ nxViteTsPaths()], test: { globals: true, cache: { - dir: '../node_modules/.vitest' + dir: '../node_modules/.vitest/my-app' }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -220,7 +220,7 @@ nxViteTsPaths()], test: { globals: true, cache: { - dir: '../node_modules/.vitest' + dir: '../node_modules/.vitest/my-app' }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -358,7 +358,7 @@ nxViteTsPaths()], test: { globals: true, cache: { - dir: '../node_modules/.vitest' + dir: '../node_modules/.vitest/my-app' }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/react/src/generators/application/application.legacy.spec.ts b/packages/react/src/generators/application/application.legacy.spec.ts index 7f3701f655b84..6ca252e032398 100644 --- a/packages/react/src/generators/application/application.legacy.spec.ts +++ b/packages/react/src/generators/application/application.legacy.spec.ts @@ -137,6 +137,8 @@ describe('react app generator (legacy)', () => { bundler: 'vite', skipFormat: true, }); - expect(appTree.read('my-vite-app/vite.config.ts')).toMatchSnapshot(); + expect( + appTree.read('my-vite-app/vite.config.ts', 'utf-8') + ).toMatchSnapshot(); }); }); diff --git a/packages/react/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/react/src/generators/library/__snapshots__/library.spec.ts.snap index 2395331590b98..83b335bbe9ee6 100644 --- a/packages/react/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/react/src/generators/library/__snapshots__/library.spec.ts.snap @@ -24,7 +24,7 @@ nxViteTsPaths(), test: { 'globals': true, -'cache': {"dir":"../node_modules/.vitest"}, +'cache': {"dir":"../node_modules/.vitest/my-lib"}, 'environment': "jsdom", 'include': ["src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}"], 'reporters': ["default"], @@ -84,7 +84,7 @@ dts({ entryRoot: 'src', tsconfigPath: path.join(__dirname, 'tsconfig.lib.json') test: { globals: true, cache: { - dir: '../node_modules/.vitest' + dir: '../node_modules/.vitest/my-lib' }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/remix/src/generators/application/__snapshots__/application.impl.spec.ts.snap b/packages/remix/src/generators/application/__snapshots__/application.impl.spec.ts.snap index dc9fad80308d7..0b0daaa448bc5 100644 --- a/packages/remix/src/generators/application/__snapshots__/application.impl.spec.ts.snap +++ b/packages/remix/src/generators/application/__snapshots__/application.impl.spec.ts.snap @@ -386,7 +386,7 @@ export default defineConfig({ setupFiles: ['test-setup.ts'], globals: true, cache: { - dir: '../node_modules/.vitest', + dir: '../node_modules/.vitest/test', }, environment: 'jsdom', include: ['./tests/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -899,7 +899,7 @@ export default defineConfig({ setupFiles: ['test-setup.ts'], globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/apps/test', }, environment: 'jsdom', include: ['./tests/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -1206,7 +1206,7 @@ import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; export default defineConfig({ root: __dirname, - cacheDir: './node_modules/.vite/.', + cacheDir: './node_modules/.vite/test', plugins: [react(), nxViteTsPaths()], @@ -1219,7 +1219,7 @@ export default defineConfig({ setupFiles: ['test-setup.ts'], globals: true, cache: { - dir: './node_modules/.vitest', + dir: './node_modules/.vitest/test', }, environment: 'jsdom', include: ['./tests/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/remix/src/generators/application/application.impl.spec.ts b/packages/remix/src/generators/application/application.impl.spec.ts index 1e9d681ac9db7..75d8ed5cad92d 100644 --- a/packages/remix/src/generators/application/application.impl.spec.ts +++ b/packages/remix/src/generators/application/application.impl.spec.ts @@ -1,13 +1,20 @@ import 'nx/src/internal-testing-utils/mock-project-graph'; -import type { Tree } from '@nx/devkit'; -import { joinPathFragments, readJson } from '@nx/devkit'; +import { joinPathFragments, readJson, type Tree } from '@nx/devkit'; +import * as devkit from '@nx/devkit'; import { ProjectNameAndRootFormat } from '@nx/devkit/src/generators/project-name-and-root-utils'; import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import applicationGenerator from './application.impl'; import { join } from 'path'; +import { PackageManagerCommands } from 'nx/src/utils/package-manager'; describe('Remix Application', () => { + beforeEach(() => { + jest + .spyOn(devkit, 'getPackageManagerCommand') + .mockReturnValue({ exec: 'npx' } as PackageManagerCommands); + }); + describe('Standalone Project Repo', () => { it('should create the application correctly', async () => { // ARRANGE diff --git a/packages/remix/src/generators/library/__snapshots__/library.impl.spec.ts.snap b/packages/remix/src/generators/library/__snapshots__/library.impl.spec.ts.snap index cd70040d87f7c..855fe8b641970 100644 --- a/packages/remix/src/generators/library/__snapshots__/library.impl.spec.ts.snap +++ b/packages/remix/src/generators/library/__snapshots__/library.impl.spec.ts.snap @@ -42,7 +42,7 @@ export default defineConfig({ test: { setupFiles: ['./src/test-setup.ts'], globals: true, - cache: { dir: '../node_modules/.vitest' }, + cache: { dir: '../node_modules/.vitest/test' }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], reporters: ['default'], @@ -120,7 +120,7 @@ export default defineConfig({ test: { setupFiles: ['./src/test-setup.ts'], globals: true, - cache: { dir: '../../node_modules/.vitest' }, + cache: { dir: '../../node_modules/.vitest/libs/test' }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], reporters: ['default'], diff --git a/packages/remix/src/generators/storybook-configuration/__snapshots__/storybook-configuration.impl.spec.ts.snap b/packages/remix/src/generators/storybook-configuration/__snapshots__/storybook-configuration.impl.spec.ts.snap index ff99d80c88abb..cfbd6f2bda4d1 100644 --- a/packages/remix/src/generators/storybook-configuration/__snapshots__/storybook-configuration.impl.spec.ts.snap +++ b/packages/remix/src/generators/storybook-configuration/__snapshots__/storybook-configuration.impl.spec.ts.snap @@ -1,6 +1,25 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`Storybook Configuration it should create a storybook configuration and use react-vite framework with testing framework jest 1`] = ` +"/// +import { defineConfig } from 'vite'; +import react from '@vitejs/plugin-react'; +import viteTsConfigPaths from 'vite-tsconfig-paths'; + +export default defineConfig({ + cacheDir: '../../node_modules/.vite/storybook/libs/storybook-test', + + plugins: [ + react(), + viteTsConfigPaths({ + root: '../../../', + }), + ], +}); +" +`; + +exports[`Storybook Configuration it should create a storybook configuration and use react-vite framework with testing framework jest 2`] = ` "import type { StorybookConfig } from '@storybook/react-vite'; const config: StorybookConfig = { @@ -25,6 +44,25 @@ export default config; `; exports[`Storybook Configuration it should create a storybook configuration and use react-vite framework with testing framework none 1`] = ` +"/// +import { defineConfig } from 'vite'; +import react from '@vitejs/plugin-react'; +import viteTsConfigPaths from 'vite-tsconfig-paths'; + +export default defineConfig({ + cacheDir: '../../node_modules/.vite/storybook/libs/storybook-test', + + plugins: [ + react(), + viteTsConfigPaths({ + root: '../../../', + }), + ], +}); +" +`; + +exports[`Storybook Configuration it should create a storybook configuration and use react-vite framework with testing framework none 2`] = ` "import type { StorybookConfig } from '@storybook/react-vite'; const config: StorybookConfig = { @@ -49,6 +87,38 @@ export default config; `; exports[`Storybook Configuration it should create a storybook configuration and use react-vite framework with testing framework vitest 1`] = ` +"import { defineConfig } from 'vite'; +import react from '@vitejs/plugin-react'; +import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; + +export default defineConfig({ + root: __dirname, + cacheDir: '../../node_modules/.vite/libs/storybook-test', + + plugins: [react(), nxViteTsPaths()], + + // Uncomment this if you are using workers. + // worker: { + // plugins: [ nxViteTsPaths() ], + // }, + + test: { + setupFiles: ['./src/test-setup.ts'], + globals: true, + cache: { dir: '../../node_modules/.vitest/libs/storybook-test' }, + environment: 'jsdom', + include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], + reporters: ['default'], + coverage: { + reportsDirectory: '../../coverage/libs/storybook-test', + provider: 'v8', + }, + }, +}); +" +`; + +exports[`Storybook Configuration it should create a storybook configuration and use react-vite framework with testing framework vitest 2`] = ` "import type { StorybookConfig } from '@storybook/react-vite'; const config: StorybookConfig = { diff --git a/packages/remix/src/generators/storybook-configuration/files/vite.config.ts__tpl__ b/packages/remix/src/generators/storybook-configuration/files/vite.config.ts__tpl__ index 5cc91668a0798..ea26a36b4fb2d 100644 --- a/packages/remix/src/generators/storybook-configuration/files/vite.config.ts__tpl__ +++ b/packages/remix/src/generators/storybook-configuration/files/vite.config.ts__tpl__ @@ -4,7 +4,7 @@ import react from '@vitejs/plugin-react'; import viteTsConfigPaths from 'vite-tsconfig-paths'; export default defineConfig({ - cacheDir: '../../../node_modules/.vite/storybook-generator-test', + cacheDir: '<%= cacheDir %>', plugins: [ react(), diff --git a/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.spec.ts b/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.spec.ts index 39266366d8aad..9e3d826d6101f 100644 --- a/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.spec.ts +++ b/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.spec.ts @@ -28,7 +28,9 @@ describe('Storybook Configuration', () => { }); // ASSERT - expect(tree.exists(`libs/storybook-test/vite.config.ts`)); + expect( + tree.read(`libs/storybook-test/vite.config.ts`, 'utf-8') + ).toMatchSnapshot(); expect( tree.read(`libs/storybook-test/.storybook/main.ts`, 'utf-8') ).toMatchSnapshot(); diff --git a/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.ts b/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.ts index 378621a315bb7..b6593e9b69aa6 100644 --- a/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.ts +++ b/packages/remix/src/generators/storybook-configuration/storybook-configuration.impl.ts @@ -1,6 +1,7 @@ import { generateFiles, joinPathFragments, + offsetFromRoot, readProjectConfiguration, readNxJson, type Tree, @@ -19,6 +20,12 @@ export function remixStorybookConfiguration( }); } +function normalizedJoinPaths(...paths: string[]): string { + const path = joinPathFragments(...paths); + + return path.startsWith('.') ? path : `./${path}`; +} + export default async function remixStorybookConfigurationInternal( tree: Tree, schema: StorybookConfigurationSchema @@ -31,7 +38,14 @@ export default async function remixStorybookConfigurationInternal( const { root } = readProjectConfiguration(tree, schema.project); if (!tree.exists(joinPathFragments(root, 'vite.config.ts'))) { - generateFiles(tree, join(__dirname, 'files'), root, { tpl: '' }); + const cacheDir = normalizedJoinPaths( + offsetFromRoot(root), + 'node_modules', + '.vite', + 'storybook', + root === '.' ? schema.project : root + ); + generateFiles(tree, join(__dirname, 'files'), root, { cacheDir, tpl: '' }); } const task = await storybookConfigurationGenerator(tree, schema); diff --git a/packages/vite/src/generators/configuration/__snapshots__/configuration.spec.ts.snap b/packages/vite/src/generators/configuration/__snapshots__/configuration.spec.ts.snap index c8b9be9b9d2c8..d4dfe3d6d52b4 100644 --- a/packages/vite/src/generators/configuration/__snapshots__/configuration.spec.ts.snap +++ b/packages/vite/src/generators/configuration/__snapshots__/configuration.spec.ts.snap @@ -104,7 +104,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/libs/react-lib-nonb-jest', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -161,7 +161,7 @@ export default defineConfig({ test: { globals: true, - cache: { dir: '../../node_modules/.vitest' }, + cache: { dir: '../../node_modules/.vitest/libs/react-lib-nonb-vitest' }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], reporters: ['default'], @@ -326,7 +326,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/apps/my-test-react-app', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/vite/src/generators/vitest/__snapshots__/vitest.spec.ts.snap b/packages/vite/src/generators/vitest/__snapshots__/vitest.spec.ts.snap index f747589308939..73d88591c02ef 100644 --- a/packages/vite/src/generators/vitest/__snapshots__/vitest.spec.ts.snap +++ b/packages/vite/src/generators/vitest/__snapshots__/vitest.spec.ts.snap @@ -23,7 +23,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/apps/my-test-react-app', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -63,7 +63,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/apps/my-test-react-app', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -98,7 +98,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/libs/react-lib-nonb-jest', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/vite/src/utils/generator-utils.ts b/packages/vite/src/utils/generator-utils.ts index 9bcea7666a6fd..a977fb6ae0436 100644 --- a/packages/vite/src/utils/generator-utils.ts +++ b/packages/vite/src/utils/generator-utils.ts @@ -432,7 +432,12 @@ export function createOrEditViteConfig( ? `test: { globals: true, cache: { - dir: '${offsetFromRoot(projectRoot)}node_modules/.vitest' + dir: '${normalizedJoinPaths( + offsetFromRoot(projectRoot), + 'node_modules', + '.vitest', + projectRoot === '.' ? options.project : projectRoot + )}' }, environment: '${options.testEnvironment ?? 'jsdom'}', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -483,9 +488,12 @@ export function createOrEditViteConfig( // plugins: [ nxViteTsPaths() ], // },`; - const cacheDir = `cacheDir: '${offsetFromRoot( - projectRoot - )}node_modules/.vite/${projectRoot}',`; + const cacheDir = `cacheDir: '${normalizedJoinPaths( + offsetFromRoot(projectRoot), + 'node_modules', + '.vite', + projectRoot === '.' ? options.project : projectRoot + )}',`; if (tree.exists(viteConfigPath)) { handleViteConfigFileExists( @@ -499,6 +507,7 @@ export function createOrEditViteConfig( testOption, reportsDirectory, cacheDir, + projectRoot, offsetFromRoot(projectRoot), projectAlreadyHasViteTargets ); @@ -670,6 +679,7 @@ function handleViteConfigFileExists( testOption: string, reportsDirectory: string, cacheDir: string, + projectRoot: string, offsetFromRoot: string, projectAlreadyHasViteTargets?: TargetFlags ) { @@ -714,7 +724,12 @@ function handleViteConfigFileExists( const testOptionObject = { globals: true, cache: { - dir: `${offsetFromRoot}node_modules/.vitest`, + dir: normalizedJoinPaths( + offsetFromRoot, + 'node_modules', + '.vitest', + projectRoot === '.' ? options.project : projectRoot + ), }, environment: options.testEnvironment ?? 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -748,3 +763,9 @@ function handleViteConfigFileExists( ); } } + +function normalizedJoinPaths(...paths: string[]): string { + const path = joinPathFragments(...paths); + + return path.startsWith('.') ? path : `./${path}`; +} diff --git a/packages/vue/src/generators/application/__snapshots__/application.spec.ts.snap b/packages/vue/src/generators/application/__snapshots__/application.spec.ts.snap index 67922085abb63..78d7d6ae315ac 100644 --- a/packages/vue/src/generators/application/__snapshots__/application.spec.ts.snap +++ b/packages/vue/src/generators/application/__snapshots__/application.spec.ts.snap @@ -77,7 +77,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../node_modules/.vitest', + dir: '../node_modules/.vitest/test', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/vue/src/generators/application/application.spec.ts b/packages/vue/src/generators/application/application.spec.ts index c4d3ee22e52d1..02650c5285ebf 100644 --- a/packages/vue/src/generators/application/application.spec.ts +++ b/packages/vue/src/generators/application/application.spec.ts @@ -1,3 +1,5 @@ +import 'nx/src/internal-testing-utils/mock-project-graph'; + import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; import { Tree, readProjectConfiguration } from '@nx/devkit'; diff --git a/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap b/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap index d8175efe1a6f2..c96472ddfd43c 100644 --- a/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap +++ b/packages/vue/src/generators/library/__snapshots__/library.spec.ts.snap @@ -52,7 +52,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../node_modules/.vitest', + dir: '../node_modules/.vitest/my-lib', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -147,7 +147,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../node_modules/.vitest', + dir: '../node_modules/.vitest/my-lib', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/vue/src/generators/library/library.spec.ts b/packages/vue/src/generators/library/library.spec.ts index 1df63a1df2bc0..459b42bffa267 100644 --- a/packages/vue/src/generators/library/library.spec.ts +++ b/packages/vue/src/generators/library/library.spec.ts @@ -1,3 +1,5 @@ +import 'nx/src/internal-testing-utils/mock-project-graph'; + import { getProjects, readJson, diff --git a/packages/workspace/src/generators/preset/__snapshots__/preset.spec.ts.snap b/packages/workspace/src/generators/preset/__snapshots__/preset.spec.ts.snap index 95ce7d633561a..5c4a55cb2bde8 100644 --- a/packages/workspace/src/generators/preset/__snapshots__/preset.spec.ts.snap +++ b/packages/workspace/src/generators/preset/__snapshots__/preset.spec.ts.snap @@ -78,7 +78,7 @@ import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; export default defineConfig({ root: __dirname, - cacheDir: './node_modules/.vite/.', + cacheDir: './node_modules/.vite/proj', server: { port: 4200, @@ -108,7 +108,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: './node_modules/.vitest', + dir: './node_modules/.vitest/proj', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -195,7 +195,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: '../../node_modules/.vitest', + dir: '../../node_modules/.vitest/apps/proj', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], @@ -218,7 +218,7 @@ import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; export default defineConfig({ root: __dirname, - cacheDir: './node_modules/.vite/.', + cacheDir: './node_modules/.vite/proj', server: { port: 4200, @@ -248,7 +248,7 @@ export default defineConfig({ test: { globals: true, cache: { - dir: './node_modules/.vitest', + dir: './node_modules/.vitest/proj', }, environment: 'jsdom', include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], diff --git a/packages/workspace/src/generators/preset/preset.spec.ts b/packages/workspace/src/generators/preset/preset.spec.ts index c3b811aee2f98..7ff5c1fea99ac 100644 --- a/packages/workspace/src/generators/preset/preset.spec.ts +++ b/packages/workspace/src/generators/preset/preset.spec.ts @@ -87,16 +87,12 @@ describe('preset', () => { }); it('should create files (preset = react-native)', async () => { - console.log('Hello'); await presetGenerator(tree, { name: 'proj', preset: Preset.ReactNative, linter: 'eslint', }); - process.stderr.write('HELLO'); - process.stderr.write(tree.listChanges().toString()); - expect(tree.exists('/apps/proj/src/app/App.tsx')).toBe(true); });