Skip to content

Commit b72fa91

Browse files
committed
test(@schematics/angular): add unit tests for addTodoComment helper
This commit introduces unit tests for the `addTodoComment` helper function in the `jasmine-to-vitest` schematic. It also adds detailed comments to the function's AST traversal logic to improve clarity.
1 parent d2932cd commit b72fa91

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

packages/schematics/angular/refactor/jasmine-vitest/utils/comment-helpers.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ export function addTodoComment(
5151

5252
let statement: ts.Node = node;
5353

54-
// Attempt to find the containing statement
54+
// Traverse up the AST to find the containing statement for the node.
55+
// This ensures that the comment is placed before the entire statement,
56+
// rather than being attached to a deeply nested node. For example, if the
57+
// node is an `Identifier`, we want the comment on the `VariableStatement`
58+
// or `ExpressionStatement` that contains it.
5559
while (statement.parent && !ts.isBlock(statement.parent) && !ts.isSourceFile(statement.parent)) {
5660
if (ts.isExpressionStatement(statement) || ts.isVariableStatement(statement)) {
5761
break;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import ts from '../../../third_party/github.com/Microsoft/TypeScript/lib/typescript';
10+
import { addTodoComment } from './comment-helpers';
11+
12+
describe('addTodoComment', () => {
13+
function createTestHarness(sourceText: string) {
14+
const sourceFile = ts.createSourceFile('test.ts', sourceText, ts.ScriptTarget.Latest, true);
15+
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
16+
17+
return {
18+
sourceFile,
19+
run(node: ts.Node, category: 'pending') {
20+
addTodoComment(node, category);
21+
22+
return printer.printFile(sourceFile);
23+
},
24+
};
25+
}
26+
27+
it('should add a comment before the containing ExpressionStatement', () => {
28+
const sourceText = `myFunction();`;
29+
const { sourceFile, run } = createTestHarness(sourceText);
30+
const callExpression = (sourceFile.statements[0] as ts.ExpressionStatement).expression;
31+
32+
const result = run(callExpression, 'pending');
33+
34+
expect(result).toContain(
35+
'// TODO: vitest-migration: The pending() function was converted to a skipped test (`it.skip`). See: https://vitest.dev/api/vi.html#it-skip',
36+
);
37+
expect(result.trim().startsWith('// TODO')).toBe(true);
38+
});
39+
40+
it('should find the top-level statement for a deeply nested node', () => {
41+
const sourceText = `const result = myObject.prop.method();`;
42+
const { sourceFile, run } = createTestHarness(sourceText);
43+
44+
// Get a deeply nested identifier
45+
const varDeclaration = (sourceFile.statements[0] as ts.VariableStatement).declarationList
46+
.declarations[0];
47+
const methodIdentifier = (
48+
(varDeclaration.initializer as ts.CallExpression).expression as ts.PropertyAccessExpression
49+
).name;
50+
51+
const result = run(methodIdentifier, 'pending');
52+
53+
expect(result.trim().startsWith('// TODO')).toBe(true);
54+
expect(result).toContain('const result = myObject.prop.method()');
55+
});
56+
57+
it('should add a comment before a VariableStatement', () => {
58+
const sourceText = `const mySpy = jasmine.createSpy();`;
59+
const { sourceFile, run } = createTestHarness(sourceText);
60+
const varDeclaration = (sourceFile.statements[0] as ts.VariableStatement).declarationList
61+
.declarations[0];
62+
63+
const result = run(varDeclaration, 'pending');
64+
65+
expect(result.trim().startsWith('// TODO')).toBe(true);
66+
expect(result).toContain('const mySpy = jasmine.createSpy()');
67+
});
68+
});

0 commit comments

Comments
 (0)