Skip to content

Commit

Permalink
feat(vite): allow passing multiple test files to vite:test (#17496)
Browse files Browse the repository at this point in the history
Co-authored-by: Caleb Ukle <caleb.ukle+github@pm.me>
  • Loading branch information
Joe Walton and barbados-clemens committed Jun 26, 2023
1 parent c4b7a1f commit a995940
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 8 deletions.
7 changes: 4 additions & 3 deletions docs/generated/packages/vite/executors/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/executors/test/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export interface VitestExecutorOptions {
update?: boolean;
reportsDirectory?: string;
coverage?: boolean;
testFile?: string;
testFiles?: string[];
}
7 changes: 4 additions & 3 deletions packages/vite/src/executors/test/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/executors/test/vitest.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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",
],
},
},
},
}
`);
});
});
Original file line number Diff line number Diff line change
@@ -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);
}
);
}

1 comment on commit a995940

@vercel
Copy link

@vercel vercel bot commented on a995940 Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx.dev

Please sign in to comment.