-
Notifications
You must be signed in to change notification settings - Fork 11.9k
feat(@schematics/angular): introduce initial jasmine-to-vitest unit test refactor schematic #31435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
packages/schematics/angular/refactor/jasmine-vitest/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { | ||
| DirEntry, | ||
| Rule, | ||
| SchematicContext, | ||
| SchematicsException, | ||
| Tree, | ||
| } from '@angular-devkit/schematics'; | ||
| import { ProjectDefinition, getWorkspace } from '../../utility/workspace'; | ||
| import { Schema } from './schema'; | ||
| import { transformJasmineToVitest } from './test-file-transformer'; | ||
| import { RefactorReporter } from './utils/refactor-reporter'; | ||
|
|
||
| async function getProjectRoot(tree: Tree, projectName: string | undefined): Promise<string> { | ||
| const workspace = await getWorkspace(tree); | ||
|
|
||
| let project: ProjectDefinition | undefined; | ||
| if (projectName) { | ||
| project = workspace.projects.get(projectName); | ||
| if (!project) { | ||
| throw new SchematicsException(`Project "${projectName}" not found.`); | ||
| } | ||
| } else { | ||
| if (workspace.projects.size === 1) { | ||
| project = workspace.projects.values().next().value; | ||
| } else { | ||
| const projectNames = Array.from(workspace.projects.keys()); | ||
| throw new SchematicsException( | ||
| `Multiple projects found: [${projectNames.join(', ')}]. Please specify a project name.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (!project) { | ||
| // This case should theoretically not be hit due to the checks above, but it's good for type safety. | ||
| throw new SchematicsException('Could not determine a project.'); | ||
| } | ||
|
|
||
| return project.root; | ||
| } | ||
|
|
||
| const DIRECTORIES_TO_SKIP = new Set(['node_modules', '.git', 'dist', '.angular']); | ||
|
|
||
| function findTestFiles(directory: DirEntry, fileSuffix: string): string[] { | ||
| const files: string[] = []; | ||
| const stack: DirEntry[] = [directory]; | ||
|
|
||
| let current: DirEntry | undefined; | ||
| while ((current = stack.pop())) { | ||
| for (const path of current.subfiles) { | ||
| if (path.endsWith(fileSuffix)) { | ||
| files.push(current.path + '/' + path); | ||
| } | ||
| } | ||
|
|
||
| for (const path of current.subdirs) { | ||
| if (DIRECTORIES_TO_SKIP.has(path)) { | ||
| continue; | ||
| } | ||
| stack.push(current.dir(path)); | ||
| } | ||
| } | ||
|
|
||
| return files; | ||
| } | ||
|
|
||
| export default function (options: Schema): Rule { | ||
| return async (tree: Tree, context: SchematicContext) => { | ||
| const reporter = new RefactorReporter(context.logger); | ||
| const projectRoot = await getProjectRoot(tree, options.project); | ||
| const fileSuffix = options.fileSuffix ?? '.spec.ts'; | ||
|
|
||
| const files = findTestFiles(tree.getDir(projectRoot), fileSuffix); | ||
|
|
||
| if (files.length === 0) { | ||
| throw new SchematicsException( | ||
| `No files ending with '${fileSuffix}' found in project '${options.project}'.`, | ||
| ); | ||
| } | ||
|
|
||
| for (const file of files) { | ||
| reporter.incrementScannedFiles(); | ||
| const content = tree.readText(file); | ||
| const newContent = transformJasmineToVitest(file, content, reporter); | ||
|
|
||
| if (content !== newContent) { | ||
| tree.overwrite(file, newContent); | ||
| reporter.incrementTransformedFiles(); | ||
| } | ||
| } | ||
|
|
||
| reporter.printSummary(options.verbose); | ||
| }; | ||
| } |
26 changes: 26 additions & 0 deletions
26
packages/schematics/angular/refactor/jasmine-vitest/schema.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "$schema": "http://json-schema.org/draft-07/schema", | ||
| "$id": "SchematicsAngularJasmineToVitest", | ||
| "title": "Angular Jasmine to Vitest Schematic", | ||
| "type": "object", | ||
| "description": "Refactors a Jasmine test file to use Vitest.", | ||
| "properties": { | ||
| "fileSuffix": { | ||
| "type": "string", | ||
| "description": "The file suffix to identify test files (e.g., '.spec.ts', '.test.ts').", | ||
| "default": ".spec.ts" | ||
| }, | ||
| "project": { | ||
| "type": "string", | ||
| "description": "The name of the project where the tests should be refactored. If not specified, the CLI will determine the project from the current directory.", | ||
| "$default": { | ||
| "$source": "projectName" | ||
| } | ||
| }, | ||
| "verbose": { | ||
| "type": "boolean", | ||
| "description": "Enable verbose logging to see detailed information about the transformations being applied.", | ||
| "default": false | ||
| } | ||
| } | ||
| } |
80 changes: 80 additions & 0 deletions
80
packages/schematics/angular/refactor/jasmine-vitest/test-file-transformer.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript'; | ||
| import { | ||
| transformDoneCallback, | ||
| transformFocusedAndSkippedTests, | ||
| transformPending, | ||
| } from './transformers/jasmine-lifecycle'; | ||
| import { RefactorContext } from './utils/refactor-context'; | ||
| import { RefactorReporter } from './utils/refactor-reporter'; | ||
|
|
||
| /** | ||
| * Transforms a string of Jasmine test code to Vitest test code. | ||
| * This is the main entry point for the transformation. | ||
| * @param content The source code to transform. | ||
| * @param reporter The reporter to track TODOs. | ||
| * @returns The transformed code. | ||
| */ | ||
| export function transformJasmineToVitest( | ||
| filePath: string, | ||
| content: string, | ||
| reporter: RefactorReporter, | ||
| ): string { | ||
| const sourceFile = ts.createSourceFile( | ||
| filePath, | ||
| content, | ||
| ts.ScriptTarget.Latest, | ||
| true, | ||
| ts.ScriptKind.TS, | ||
| ); | ||
|
|
||
| const transformer: ts.TransformerFactory<ts.SourceFile> = (context) => { | ||
| const refactorCtx: RefactorContext = { | ||
| sourceFile, | ||
| reporter, | ||
| tsContext: context, | ||
| }; | ||
|
|
||
| const visitor: ts.Visitor = (node) => { | ||
| let transformedNode: ts.Node | readonly ts.Node[] = node; | ||
|
|
||
| // Transform the node itself based on its type | ||
| if (ts.isCallExpression(transformedNode)) { | ||
| const transformations = [ | ||
| transformFocusedAndSkippedTests, | ||
| transformPending, | ||
| transformDoneCallback, | ||
| ]; | ||
|
|
||
| for (const transformer of transformations) { | ||
| transformedNode = transformer(transformedNode, refactorCtx); | ||
| } | ||
| } | ||
|
|
||
| // Visit the children of the node to ensure they are transformed | ||
| if (Array.isArray(transformedNode)) { | ||
| return transformedNode.map((node) => ts.visitEachChild(node, visitor, context)); | ||
| } else { | ||
| return ts.visitEachChild(transformedNode, visitor, context); | ||
| } | ||
| }; | ||
|
|
||
| return (node) => ts.visitNode(node, visitor) as ts.SourceFile; | ||
| }; | ||
|
|
||
| const result = ts.transform(sourceFile, [transformer]); | ||
| if (result.transformed[0] === sourceFile) { | ||
| return content; | ||
| } | ||
|
|
||
| const printer = ts.createPrinter(); | ||
|
|
||
| return printer.printFile(result.transformed[0]); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.