|
| 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 } from '@angular-devkit/schematics/testing'; |
| 10 | +import { format } from 'prettier'; |
| 11 | +import { Schema as ApplicationOptions } from '../../application/schema'; |
| 12 | +import { Schema as WorkspaceOptions } from '../../workspace/schema'; |
| 13 | + |
| 14 | +describe('Angular `fakeAsync` to Vitest Fake Timers Schematic', () => { |
| 15 | + it("should replace `fakeAsync` with an async test using Vitest's fake timers", async () => { |
| 16 | + const { transformContent } = await setUp(); |
| 17 | + |
| 18 | + const newContent = await transformContent(` |
| 19 | +it("should work", fakeAsync(() => { |
| 20 | + expect(1).toBe(1); |
| 21 | +})); |
| 22 | +`); |
| 23 | + |
| 24 | + expect(newContent).toBe(`\ |
| 25 | +import { onTestFinished, vi } from "vitest"; |
| 26 | +it("should work", async () => { |
| 27 | + vi.useFakeTimers(); |
| 28 | + onTestFinished(() => { |
| 29 | + vi.useRealTimers(); |
| 30 | + }); |
| 31 | + expect(1).toBe(1); |
| 32 | +}); |
| 33 | +`); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should replace `tick` with `vi.advanceTimersByTimeAsync`', async () => { |
| 37 | + const { transformContent } = await setUp(); |
| 38 | + |
| 39 | + const newContent = await transformContent(` |
| 40 | +it("should work", fakeAsync(() => { |
| 41 | + tick(100); |
| 42 | +})); |
| 43 | +`); |
| 44 | + |
| 45 | + expect(newContent).toContain(`await vi.advanceTimersByTimeAsync(100);`); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should replace `tick()` with `vi.advanceTimersByTimeAsync(0)`', async () => { |
| 49 | + const { transformContent } = await setUp(); |
| 50 | + |
| 51 | + const newContent = await transformContent(` |
| 52 | +it("should work", fakeAsync(() => { |
| 53 | + tick(); |
| 54 | +})); |
| 55 | +`); |
| 56 | + |
| 57 | + expect(newContent).toContain(`await vi.advanceTimersByTimeAsync(0);`); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should replace `flush` with `vi.advanceTimersByTimeAsync`', async () => { |
| 61 | + const { transformContent } = await setUp(); |
| 62 | + |
| 63 | + const newContent = await transformContent(` |
| 64 | +it("should work", fakeAsync(() => { |
| 65 | + flush(); |
| 66 | +})); |
| 67 | +`); |
| 68 | + |
| 69 | + expect(newContent).toContain(`await vi.runAllTimersAsync();`); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +async function setUp() { |
| 74 | + const schematicRunner = new SchematicTestRunner( |
| 75 | + '@schematics/angular', |
| 76 | + require.resolve('../../collection.json'), |
| 77 | + ); |
| 78 | + |
| 79 | + const workspaceOptions: WorkspaceOptions = { |
| 80 | + name: 'workspace', |
| 81 | + newProjectRoot: 'projects', |
| 82 | + version: '21.0.0', |
| 83 | + }; |
| 84 | + |
| 85 | + const appOptions: ApplicationOptions = { |
| 86 | + name: 'bar', |
| 87 | + inlineStyle: false, |
| 88 | + inlineTemplate: false, |
| 89 | + routing: false, |
| 90 | + skipTests: false, |
| 91 | + skipPackageJson: false, |
| 92 | + }; |
| 93 | + |
| 94 | + let appTree = await schematicRunner.runSchematic('workspace', workspaceOptions); |
| 95 | + appTree = await schematicRunner.runSchematic('application', appOptions, appTree); |
| 96 | + |
| 97 | + return { |
| 98 | + transformContent: async (content: string): Promise<string> => { |
| 99 | + const specFilePath = 'projects/bar/src/app/app.spec.ts'; |
| 100 | + |
| 101 | + appTree.overwrite(specFilePath, content); |
| 102 | + |
| 103 | + const tree = await schematicRunner.runSchematic( |
| 104 | + 'refactor-fake-async', |
| 105 | + { project: 'bar' }, |
| 106 | + appTree, |
| 107 | + ); |
| 108 | + |
| 109 | + return format(tree.readContent(specFilePath), { parser: 'typescript' }); |
| 110 | + }, |
| 111 | + }; |
| 112 | +} |
0 commit comments