|
| 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 { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 10 | +import { Schema as ApplicationOptions } from '../../application/schema'; |
| 11 | +import { Schema as WorkspaceOptions } from '../../workspace/schema'; |
| 12 | + |
| 13 | +describe('Jasmine to Vitest Schematic', () => { |
| 14 | + const schematicRunner = new SchematicTestRunner( |
| 15 | + '@schematics/angular', |
| 16 | + require.resolve('../../collection.json'), |
| 17 | + ); |
| 18 | + |
| 19 | + let appTree: UnitTestTree; |
| 20 | + |
| 21 | + const workspaceOptions: WorkspaceOptions = { |
| 22 | + name: 'workspace', |
| 23 | + newProjectRoot: 'projects', |
| 24 | + version: '20.0.0', |
| 25 | + }; |
| 26 | + |
| 27 | + const appOptions: ApplicationOptions = { |
| 28 | + name: 'bar', |
| 29 | + inlineStyle: false, |
| 30 | + inlineTemplate: false, |
| 31 | + routing: false, |
| 32 | + skipTests: false, |
| 33 | + skipPackageJson: false, |
| 34 | + }; |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + appTree = await schematicRunner.runSchematic('workspace', workspaceOptions); |
| 38 | + appTree = await schematicRunner.runSchematic('application', appOptions, appTree); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should transform a basic Jasmine test file', async () => { |
| 42 | + const specFilePath = 'projects/bar/src/app/app.spec.ts'; |
| 43 | + const content = ` |
| 44 | + describe('AppComponent', () => { |
| 45 | + it('should create the app', () => { |
| 46 | + const service = { myMethod: () => {} }; |
| 47 | + spyOn(service, 'myMethod'); |
| 48 | + service.myMethod(); |
| 49 | + expect(service.myMethod).toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + }); |
| 52 | + `; |
| 53 | + appTree.overwrite(specFilePath, content); |
| 54 | + |
| 55 | + const tree = await schematicRunner.runSchematic( |
| 56 | + 'jasmine-to-vitest', |
| 57 | + { project: 'bar' }, |
| 58 | + appTree, |
| 59 | + ); |
| 60 | + |
| 61 | + const newContent = tree.readContent(specFilePath); |
| 62 | + expect(newContent).toContain(`vi.spyOn(service, 'myMethod');`); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should only transform files matching the fileSuffix option', async () => { |
| 66 | + const specFilePath = 'projects/bar/src/app/app.spec.ts'; |
| 67 | + const specFileContent = ` |
| 68 | + describe('AppComponent', () => { |
| 69 | + it('should test something', () => { |
| 70 | + spyOn(window, 'alert'); |
| 71 | + }); |
| 72 | + }); |
| 73 | + `; |
| 74 | + appTree.overwrite(specFilePath, specFileContent); |
| 75 | + |
| 76 | + const testFilePath = 'projects/bar/src/app/app.test.ts'; |
| 77 | + const testFileContent = ` |
| 78 | + describe('AppComponent Test', () => { |
| 79 | + it('should test another thing', () => { |
| 80 | + spyOn(window, 'confirm'); |
| 81 | + }); |
| 82 | + }); |
| 83 | + `; |
| 84 | + appTree.create(testFilePath, testFileContent); |
| 85 | + |
| 86 | + const tree = await schematicRunner.runSchematic( |
| 87 | + 'jasmine-to-vitest', |
| 88 | + { project: 'bar', fileSuffix: '.test.ts' }, |
| 89 | + appTree, |
| 90 | + ); |
| 91 | + |
| 92 | + const unchangedContent = tree.readContent(specFilePath); |
| 93 | + expect(unchangedContent).toContain(`spyOn(window, 'alert');`); |
| 94 | + expect(unchangedContent).not.toContain(`vi.spyOn(window, 'alert');`); |
| 95 | + |
| 96 | + const changedContent = tree.readContent(testFilePath); |
| 97 | + expect(changedContent).toContain(`vi.spyOn(window, 'confirm');`); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should print verbose logs when the verbose option is true', async () => { |
| 101 | + const specFilePath = 'projects/bar/src/app/app.spec.ts'; |
| 102 | + const content = ` |
| 103 | + describe('AppComponent', () => { |
| 104 | + it('should create the app', () => { |
| 105 | + const service = { myMethod: () => {} }; |
| 106 | + spyOn(service, 'myMethod'); |
| 107 | + }); |
| 108 | + }); |
| 109 | + `; |
| 110 | + appTree.overwrite(specFilePath, content); |
| 111 | + |
| 112 | + const logs: string[] = []; |
| 113 | + schematicRunner.logger.subscribe((entry) => logs.push(entry.message)); |
| 114 | + |
| 115 | + await schematicRunner.runSchematic( |
| 116 | + 'jasmine-to-vitest', |
| 117 | + { project: 'bar', verbose: true }, |
| 118 | + appTree, |
| 119 | + ); |
| 120 | + |
| 121 | + expect(logs).toContain('Detailed Transformation Log:'); |
| 122 | + expect(logs).toContain(`Processing: /${specFilePath}`); |
| 123 | + expect(logs.some((log) => log.includes('Transformed `spyOn` to `vi.spyOn`'))).toBe(true); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should print a summary report after running', async () => { |
| 127 | + const specFilePath = 'projects/bar/src/app/app.spec.ts'; |
| 128 | + const content = ` |
| 129 | + describe('AppComponent', () => { |
| 130 | + it('should create the app', () => { |
| 131 | + const service = { myMethod: () => {} }; |
| 132 | + jasmine.spyOnAllFunctions(service); |
| 133 | + }); |
| 134 | + }); |
| 135 | + `; |
| 136 | + appTree.overwrite(specFilePath, content); |
| 137 | + |
| 138 | + const logs: string[] = []; |
| 139 | + schematicRunner.logger.subscribe((entry) => logs.push(entry.message)); |
| 140 | + |
| 141 | + await schematicRunner.runSchematic('jasmine-to-vitest', { include: specFilePath }, appTree); |
| 142 | + |
| 143 | + expect(logs).toContain('Jasmine to Vitest Refactoring Summary:'); |
| 144 | + expect(logs).toContain('- 1 test file(s) scanned.'); |
| 145 | + expect(logs).toContain('- 1 file(s) transformed.'); |
| 146 | + expect(logs).toContain('- 1 TODO(s) added for manual review:'); |
| 147 | + expect(logs).toContain(' - 1x spyOnAllFunctions'); |
| 148 | + }); |
| 149 | +}); |
0 commit comments