From fbe0e61c649dad2f0c0d1ac902b08b8e0dfef735 Mon Sep 17 00:00:00 2001 From: Katerina Skroumpelou Date: Wed, 8 Nov 2023 18:57:04 +0200 Subject: [PATCH] feat(vite): add tests for nodes --- .../plugins/__snapshots__/plugin.spec.ts.snap | 96 +++++++++++++++++ packages/vite/src/plugins/plugin.spec.ts | 101 +++++++++++++----- packages/vite/src/plugins/plugin.ts | 53 +++------ 3 files changed, 186 insertions(+), 64 deletions(-) create mode 100644 packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap diff --git a/packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap b/packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap new file mode 100644 index 00000000000000..51777c1408f31e --- /dev/null +++ b/packages/vite/src/plugins/__snapshots__/plugin.spec.ts.snap @@ -0,0 +1,96 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`@nx/vite/plugin root project should create nodes 1`] = ` +{ + "projects": { + ".": { + "projectType": "library", + "root": ".", + "targets": { + "build": { + "cache": true, + "configurations": { + "development": { + "mode": "development", + }, + "production": { + "mode": "production", + }, + }, + "defaultConfiguration": "production", + "executor": "@nx/vite:build", + "inputs": [ + "default", + "^production", + ], + "options": { + "outputPath": "dist", + }, + "outputs": [ + "{options.outputPath}", + ], + }, + "preview": { + "cache": true, + "configurations": { + "development": { + "buildTarget": "build:development", + "hmr": true, + }, + "production": { + "buildTarget": "build:production", + "hmr": false, + }, + }, + "defaultConfiguration": "development", + "executor": "@nx/vite:preview-server", + "inputs": [ + "default", + "^production", + ], + "options": { + "buildTarget": "build", + }, + }, + "serve": { + "cache": true, + "configurations": { + "development": { + "buildTarget": "build:development", + "hmr": true, + }, + "production": { + "buildTarget": "build:production", + "hmr": false, + }, + }, + "defaultConfiguration": "development", + "executor": "@nx/vite:dev-server", + "inputs": [ + "default", + "^production", + ], + "options": { + "buildTarget": "build", + }, + }, + "test": { + "cache": true, + "executor": "@nx/vite:test", + "inputs": [ + "default", + "^production", + ], + "options": { + "passWithNoTests": true, + "reportsDirectory": "coverage", + }, + "outputs": [ + "{options.reportsDirectory}", + ], + }, + }, + }, + }, +} +`; diff --git a/packages/vite/src/plugins/plugin.spec.ts b/packages/vite/src/plugins/plugin.spec.ts index fbe349e32787ec..9367e27b5cc506 100644 --- a/packages/vite/src/plugins/plugin.spec.ts +++ b/packages/vite/src/plugins/plugin.spec.ts @@ -1,44 +1,91 @@ import { CreateNodesContext } from '@nx/devkit'; - +import type { UserConfig } from 'vite'; import { createNodes } from './plugin'; - +import { TempFs } from 'nx/src/internal-testing-utils/temp-fs'; +import { join } from 'path'; describe('@nx/vite/plugin', () => { let createNodesFunction = createNodes[1]; let context: CreateNodesContext; + describe('root project', () => { + beforeEach(async () => { + context = { + nxJsonConfiguration: { + namedInputs: { + default: ['{projectRoot}/**/*'], + production: ['!{projectRoot}/**/*.spec.ts'], + }, + }, + workspaceRoot: '', + }; + }); - beforeEach(async () => { - context = { - nxJsonConfiguration: { - namedInputs: { - default: ['{projectRoot}/**/*'], - production: ['!{projectRoot}/**/*.spec.ts'], + afterEach(() => { + jest.resetModules(); + }); + + it('should create nodes', () => { + mockViteConfig('vite.config.ts', {}); + const nodes = createNodesFunction( + 'vite.config.ts', + { + buildTargetName: 'build', + serveTargetName: 'serve', + previewTargetName: 'preview', + testTargetName: 'test', }, - }, - workspaceRoot: '', - }; - }); + context + ); - afterEach(() => { - jest.resetModules(); + expect(nodes).toMatchSnapshot(); + }); }); - it('should create nodes', () => { - mockViteConfig({}); - const nodes = createNodesFunction( - 'TODO', - { - targetName: 'target', - }, - context - ); - - expect(nodes).toMatchInlineSnapshot(); + // some issue wiht the tempfs + xdescribe('not root project', () => { + const tempFs = new TempFs('test'); + beforeEach(() => { + context = { + nxJsonConfiguration: { + namedInputs: { + default: ['{projectRoot}/**/*'], + production: ['!{projectRoot}/**/*.spec.ts'], + }, + }, + workspaceRoot: tempFs.tempDir, + }; + + tempFs.createFileSync( + 'my-app/project.json', + JSON.stringify({ name: 'my-app' }) + ); + tempFs.createFileSync('my-app/vite.config.ts', ''); + }); + + afterEach(() => { + jest.resetModules(); + }); + + it('should create nodes', () => { + mockViteConfig('my-app/vite.config.ts', {}); + const nodes = createNodesFunction( + 'my-app/vite.config.ts', + { + buildTargetName: 'build-something', + serveTargetName: 'my-serve', + previewTargetName: 'preview-site', + testTargetName: 'vitest', + }, + context + ); + + expect(nodes).toMatchSnapshot(); + }); }); }); -function mockViteConfig(config: any) { +function mockViteConfig(configPath: string, config: UserConfig) { jest.mock( - 'TODO', + configPath, () => ({ default: config, }), diff --git a/packages/vite/src/plugins/plugin.ts b/packages/vite/src/plugins/plugin.ts index 979d08d7864681..1eb68b71cf27f9 100644 --- a/packages/vite/src/plugins/plugin.ts +++ b/packages/vite/src/plugins/plugin.ts @@ -26,7 +26,7 @@ export const createNodes: CreateNodes = [ '**/vite.config.{js,ts}', (configFilePath, options, context) => { const projectRoot = dirname(configFilePath); - + console.log('projectRoot', projectRoot); // Do not create a project if package.json and project.json isn't there. const siblingFiles = readdirSync(projectRoot); if ( @@ -48,8 +48,7 @@ export const createNodes: CreateNodes = [ configFilePath, projectRoot, options, - context, - projectName + context ), }, }, @@ -61,8 +60,7 @@ function buildViteTargets( configFilePath: string, projectRoot: string, options: VitePluginOptions, - context: CreateNodesContext, - projectName: string + context: CreateNodesContext ) { // Is this needed for some reason? getViteConfig(configFilePath, context); @@ -77,25 +75,14 @@ function buildViteTargets( projectRoot ); - targets[options.serveTargetName] = serveTarget( - context, - namedInputs, - projectRoot, - projectName - ); + targets[options.serveTargetName] = serveTarget(context, namedInputs); - targets[options.previewTargetName] = previewTarget( - context, - namedInputs, - projectRoot, - projectName - ); + targets[options.previewTargetName] = previewTarget(context, namedInputs); targets[options.testTargetName] = testTarget( context, namedInputs, - projectRoot, - projectName + projectRoot ); return targets; @@ -148,9 +135,7 @@ function serveTarget( context: CreateNodesContext, namedInputs: { [inputName: string]: any[]; - }, - projectRoot: string, - projectName: string + } ) { const targetDefaults = readTargetDefaultsForTarget( 'serve', @@ -162,16 +147,16 @@ function serveTarget( executor: '@nx/vite:dev-server', defaultConfiguration: 'development', options: { - buildTarget: `${projectName}:build`, + buildTarget: `build`, ...targetDefaults?.options, }, configurations: { development: { - buildTarget: `${projectName}:build:development`, + buildTarget: `build:development`, hmr: true, }, production: { - buildTarget: `${projectName}:build:production`, + buildTarget: `build:production`, hmr: false, }, }, @@ -193,9 +178,7 @@ function previewTarget( context: CreateNodesContext, namedInputs: { [inputName: string]: any[]; - }, - projectRoot: string, - projectName: string + } ) { const targetDefaults = readTargetDefaultsForTarget( 'preview', @@ -207,16 +190,16 @@ function previewTarget( executor: '@nx/vite:preview-server', defaultConfiguration: 'development', options: { - buildTarget: `${projectName}:build`, + buildTarget: `build`, ...targetDefaults?.options, }, configurations: { development: { - buildTarget: `${projectName}:build:development`, + buildTarget: `build:development`, hmr: true, }, production: { - buildTarget: `${projectName}:build:production`, + buildTarget: `build:production`, hmr: false, }, }, @@ -239,8 +222,7 @@ function testTarget( namedInputs: { [inputName: string]: any[]; }, - projectRoot: string, - projectName: string + projectRoot: string ) { const targetDefaults = readTargetDefaultsForTarget( 'test', @@ -248,10 +230,7 @@ function testTarget( '@nx/vite:test' ); - const coveragePath = joinPathFragments( - 'coverage', - projectRoot === '.' ? projectName : projectRoot - ); + const coveragePath = joinPathFragments('coverage', projectRoot); const baseTargetConfig: TargetConfiguration = { executor: '@nx/vite:test',