From a995940b192a99e8a8cb6f7e165d68e078ffdd11 Mon Sep 17 00:00:00 2001 From: Joe Walton Date: Mon, 26 Jun 2023 21:31:04 +0100 Subject: [PATCH] feat(vite): allow passing multiple test files to vite:test (#17496) Co-authored-by: Caleb Ukle --- .../packages/vite/executors/test.json | 7 +- packages/vite/migrations.json | 6 + packages/vite/src/executors/test/schema.d.ts | 2 +- packages/vite/src/executors/test/schema.json | 7 +- .../vite/src/executors/test/vitest.impl.ts | 2 +- .../update-16-4-1-test-file-config.spec.ts | 106 ++++++++++++++++++ .../update-16-4-1-test-file-config.ts | 41 +++++++ 7 files changed, 163 insertions(+), 8 deletions(-) create mode 100644 packages/vite/src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config.spec.ts create mode 100644 packages/vite/src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config.ts diff --git a/docs/generated/packages/vite/executors/test.json b/docs/generated/packages/vite/executors/test.json index c99380b9c996b..4f71b6bb7a155 100644 --- a/docs/generated/packages/vite/executors/test.json +++ b/docs/generated/packages/vite/executors/test.json @@ -58,9 +58,10 @@ "type": "string", "description": "Directory to write coverage report to." }, - "testFile": { - "description": "The name of the file to test.", - "type": "string" + "testFiles": { + "aliases": ["testFile"], + "type": "array", + "items": { "type": "string" } } }, "required": [], diff --git a/packages/vite/migrations.json b/packages/vite/migrations.json index 374941ca15870..b86d5bfcdee8b 100644 --- a/packages/vite/migrations.json +++ b/packages/vite/migrations.json @@ -23,6 +23,12 @@ "version": "16.0.0-beta.1", "description": "Replace @nrwl/vite with @nx/vite", "implementation": "./src/migrations/update-16-0-0-add-nx-packages/update-16-0-0-add-nx-packages" + }, + "update-16-4-1-test-file-config": { + "version": "16.4.1-beta.0", + "description": "Changes the testFile config in the vite:test exectutor from a string to an array of strings", + "cli": "nx", + "implementation": "./src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config" } }, "packageJsonUpdates": { diff --git a/packages/vite/src/executors/test/schema.d.ts b/packages/vite/src/executors/test/schema.d.ts index 4102b5292cd57..e4ffa4ae4a173 100644 --- a/packages/vite/src/executors/test/schema.d.ts +++ b/packages/vite/src/executors/test/schema.d.ts @@ -8,5 +8,5 @@ export interface VitestExecutorOptions { update?: boolean; reportsDirectory?: string; coverage?: boolean; - testFile?: string; + testFiles?: string[]; } diff --git a/packages/vite/src/executors/test/schema.json b/packages/vite/src/executors/test/schema.json index fa9fa8608375f..d730f7033a8d7 100644 --- a/packages/vite/src/executors/test/schema.json +++ b/packages/vite/src/executors/test/schema.json @@ -57,9 +57,10 @@ "type": "string", "description": "Directory to write coverage report to." }, - "testFile": { - "description": "The name of the file to test.", - "type": "string" + "testFiles": { + "aliases": ["testFile"], + "type": "array", + "items": { "type": "string" } } }, "required": [], diff --git a/packages/vite/src/executors/test/vitest.impl.ts b/packages/vite/src/executors/test/vitest.impl.ts index 30611b434c5f5..b72345193055b 100644 --- a/packages/vite/src/executors/test/vitest.impl.ts +++ b/packages/vite/src/executors/test/vitest.impl.ts @@ -62,7 +62,7 @@ export async function* vitestExecutor( const nxReporter = new NxReporter(options.watch); const settings = await getSettings(options, context, projectRoot); settings.reporters.push(nxReporter); - const cliFilters = options.testFile ? [options.testFile] : []; + const cliFilters = options.testFiles ?? []; const ctx = await startVitest(options.mode, cliFilters, settings); diff --git a/packages/vite/src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config.spec.ts b/packages/vite/src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config.spec.ts new file mode 100644 index 0000000000000..12f47c52ec6a0 --- /dev/null +++ b/packages/vite/src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config.spec.ts @@ -0,0 +1,106 @@ +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import { + Tree, + addProjectConfiguration, + readProjectConfiguration, +} from '@nx/devkit'; + +import updateTestFileOption from './update-16-4-1-test-file-config'; + +describe('update-16-5-0-vite-test-file-config migration', () => { + let tree: Tree; + + beforeEach(() => { + tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' }); + }); + + it('should run successfully when testFile does not exist in the config', () => { + addProjectConfiguration(tree, 'vitest', { + root: 'vitest', + targets: { + test: { + executor: '@nx/vite:test', + options: { + ci: true, + watch: true, + }, + }, + }, + }); + updateTestFileOption(tree); + expect(readProjectConfiguration(tree, 'vitest')).toMatchInlineSnapshot(` + { + "$schema": "../node_modules/nx/schemas/project-schema.json", + "name": "vitest", + "root": "vitest", + "targets": { + "test": { + "executor": "@nx/vite:test", + "options": { + "ci": true, + "watch": true, + }, + }, + }, + } + `); + }); + + it('should run successfully when testFile exists in options and configurations', () => { + addProjectConfiguration(tree, 'vitest', { + root: 'vitest', + targets: { + test: { + executor: '@nx/vite:test', + options: { + testFile: 'test-file.ts', + config: 'vite.config.ts', + }, + configurations: { + one: { + testFile: 'test-file-one.ts', + ci: true, + }, + two: { + testFile: 'test-file-two.ts', + watch: true, + }, + }, + }, + }, + }); + updateTestFileOption(tree); + expect(readProjectConfiguration(tree, 'vitest')).toMatchInlineSnapshot(` + { + "$schema": "../node_modules/nx/schemas/project-schema.json", + "name": "vitest", + "root": "vitest", + "targets": { + "test": { + "configurations": { + "one": { + "ci": true, + "testFiles": [ + "test-file-one.ts", + ], + }, + "two": { + "testFiles": [ + "test-file-two.ts", + ], + "watch": true, + }, + }, + "executor": "@nx/vite:test", + "options": { + "config": "vite.config.ts", + "testFiles": [ + "test-file.ts", + ], + }, + }, + }, + } + `); + }); +}); diff --git a/packages/vite/src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config.ts b/packages/vite/src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config.ts new file mode 100644 index 0000000000000..64f59dc34c49b --- /dev/null +++ b/packages/vite/src/migrations/update-16-4-1-update-test-file-config/update-16-4-1-test-file-config.ts @@ -0,0 +1,41 @@ +import { Tree, getProjects, updateProjectConfiguration } from '@nx/devkit'; +import { forEachExecutorOptions } from '@nx/devkit/src/generators/executor-options-utils'; +import { VitestExecutorOptions } from '../../executors/test/schema'; + +type OldVitestExecutorOptions = Omit & { + testFile: string; +}; + +export default function update(tree: Tree) { + const projects = getProjects(tree); + + forEachExecutorOptions( + tree, + '@nx/vite:test', + (options, projectName, targetName, configuration) => { + const projectConfig = projects.get(projectName); + + if (!options.testFile) { + return; + } + + const newTestFileArgs = [options.testFile]; + + delete options.testFile; + + if (configuration) { + projectConfig.targets[targetName].configurations[configuration] = { + ...options, + testFiles: newTestFileArgs, + }; + } else { + projectConfig.targets[targetName].options = { + ...options, + testFiles: newTestFileArgs, + }; + } + + updateProjectConfiguration(tree, projectName, projectConfig); + } + ); +}