|
| 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 { EmptyTree } from '@angular-devkit/schematics'; |
| 10 | +import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; |
| 11 | +import { Builders } from '../utility/workspace-models'; |
| 12 | + |
| 13 | +describe('Vitest Browser Provider Schematic', () => { |
| 14 | + const schematicRunner = new SchematicTestRunner( |
| 15 | + '@schematics/angular', |
| 16 | + require.resolve('../collection.json'), |
| 17 | + ); |
| 18 | + |
| 19 | + let tree: UnitTestTree; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + tree = new UnitTestTree(new EmptyTree()); |
| 23 | + tree.create( |
| 24 | + '/angular.json', |
| 25 | + JSON.stringify({ |
| 26 | + version: 1, |
| 27 | + projects: { |
| 28 | + app: { |
| 29 | + root: '', |
| 30 | + projectType: 'application', |
| 31 | + targets: { |
| 32 | + test: { |
| 33 | + builder: Builders.BuildUnitTest, |
| 34 | + options: { |
| 35 | + tsConfig: 'tsconfig.spec.json', |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }), |
| 42 | + ); |
| 43 | + tree.create( |
| 44 | + '/tsconfig.spec.json', |
| 45 | + JSON.stringify({ |
| 46 | + compilerOptions: { |
| 47 | + types: ['jasmine'], |
| 48 | + }, |
| 49 | + }), |
| 50 | + ); |
| 51 | + tree.create('/package.json', JSON.stringify({})); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should add dependencies and update tsconfig.spec.json', async () => { |
| 55 | + const options = { |
| 56 | + project: 'app', |
| 57 | + package: '@vitest/browser-playwright', |
| 58 | + skipInstall: true, |
| 59 | + }; |
| 60 | + |
| 61 | + const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree); |
| 62 | + |
| 63 | + const packageJson = JSON.parse(resultTree.readContent('/package.json')); |
| 64 | + expect(packageJson.devDependencies['@vitest/browser-playwright']).toBeDefined(); |
| 65 | + expect(packageJson.devDependencies['playwright']).toBeDefined(); |
| 66 | + |
| 67 | + const tsConfig = JSON.parse(resultTree.readContent('/tsconfig.spec.json')); |
| 68 | + expect(tsConfig.compilerOptions.types).toContain('vitest/globals'); |
| 69 | + expect(tsConfig.compilerOptions.types).toContain('@vitest/browser-playwright'); |
| 70 | + expect(tsConfig.compilerOptions.types).not.toContain('jasmine'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should add webdriverio dependency when @vitest/browser-webdriverio is used', async () => { |
| 74 | + const options = { |
| 75 | + project: 'app', |
| 76 | + package: '@vitest/browser-webdriverio', |
| 77 | + skipInstall: true, |
| 78 | + }; |
| 79 | + |
| 80 | + const resultTree = await schematicRunner.runSchematic('vitest-browser', options, tree); |
| 81 | + |
| 82 | + const packageJson = JSON.parse(resultTree.readContent('/package.json')); |
| 83 | + expect(packageJson.devDependencies['@vitest/browser-webdriverio']).toBeDefined(); |
| 84 | + expect(packageJson.devDependencies['webdriverio']).toBeDefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should throw if project does not exist', async () => { |
| 88 | + const options = { |
| 89 | + project: 'invalid', |
| 90 | + package: '@vitest/browser-playwright', |
| 91 | + }; |
| 92 | + |
| 93 | + await expectAsync( |
| 94 | + schematicRunner.runSchematic('vitest-browser', options, tree), |
| 95 | + ).toBeRejectedWithError('Project "invalid" does not exist.'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should throw if project uses Karma', async () => { |
| 99 | + const angularJson = JSON.parse(tree.readContent('/angular.json')); |
| 100 | + angularJson.projects.app.targets.test.options.runner = 'karma'; |
| 101 | + tree.overwrite('/angular.json', JSON.stringify(angularJson)); |
| 102 | + |
| 103 | + const options = { |
| 104 | + project: 'app', |
| 105 | + package: '@vitest/browser-playwright', |
| 106 | + }; |
| 107 | + |
| 108 | + await expectAsync( |
| 109 | + schematicRunner.runSchematic('vitest-browser', options, tree), |
| 110 | + ).toBeRejectedWithError( |
| 111 | + 'Project "app" is configured to use Karma. Please migrate to Vitest before adding browser testing support.', |
| 112 | + ); |
| 113 | + }); |
| 114 | +}); |
0 commit comments