|
| 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