feat(vite): allow passing multiple test files to vite:test (#17496)

Co-authored-by: Caleb Ukle <caleb.ukle+github@pm.me>
This commit is contained in:
Joe Walton 2023-06-26 21:31:04 +01:00 committed by GitHub
parent c4b7a1f9d8
commit a995940b19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 163 additions and 8 deletions

View File

@ -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": [],

View File

@ -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": {

View File

@ -8,5 +8,5 @@ export interface VitestExecutorOptions {
update?: boolean;
reportsDirectory?: string;
coverage?: boolean;
testFile?: string;
testFiles?: string[];
}

View File

@ -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": [],

View File

@ -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);

View File

@ -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",
],
},
},
},
}
`);
});
});

View File

@ -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<VitestExecutorOptions, 'testFiles'> & {
testFile: string;
};
export default function update(tree: Tree) {
const projects = getProjects(tree);
forEachExecutorOptions<OldVitestExecutorOptions>(
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);
}
);
}