|
| 1 | +import { Tree, addProjectConfiguration, writeJson } from '@nx/devkit'; |
| 2 | +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; |
| 3 | +import setupVitestGenerator from './generator'; |
| 4 | +import { |
| 5 | + V19_X_ANALOG_JS_VITE_PLUGIN_ANGULAR, |
| 6 | + V19_X_ANALOG_JS_VITEST_ANGULAR, |
| 7 | + V19_X_JSDOM, |
| 8 | + V19_X_VITE_TSCONFIG_PATHS, |
| 9 | + V19_X_VITEST, |
| 10 | + V19_X_VITE, |
| 11 | + NX_X_LATEST_VITE, |
| 12 | + NX_X_LATEST_VITEST, |
| 13 | +} from '../../utils/versions/ng_19_X/versions'; |
| 14 | + |
| 15 | +describe('setup-vitest generator', () => { |
| 16 | + let tree: Tree; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + tree = createTreeWithEmptyWorkspace(); |
| 20 | + |
| 21 | + // Setup mock Angular project |
| 22 | + addProjectConfiguration(tree, 'test-app', { |
| 23 | + root: 'test-app', |
| 24 | + sourceRoot: 'test-app/src', |
| 25 | + projectType: 'application', |
| 26 | + targets: {}, |
| 27 | + }); |
| 28 | + |
| 29 | + // Add package.json with Angular dependencies |
| 30 | + writeJson(tree, 'package.json', { |
| 31 | + dependencies: { |
| 32 | + '@angular/core': '19.0.0', |
| 33 | + '@nx/angular': '20.0.0', |
| 34 | + nx: '20.0.0', |
| 35 | + }, |
| 36 | + devDependencies: {}, |
| 37 | + }); |
| 38 | + |
| 39 | + // Add Angular project files |
| 40 | + writeJson(tree, 'test-app/tsconfig.json', { |
| 41 | + compilerOptions: { |
| 42 | + types: [], |
| 43 | + }, |
| 44 | + include: [], |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should add the correct dev dependencies', async () => { |
| 49 | + await setupVitestGenerator(tree, { |
| 50 | + project: 'test-app', |
| 51 | + }); |
| 52 | + |
| 53 | + const packageJson = JSON.parse(tree.read('package.json', 'utf-8')); |
| 54 | + expect(packageJson.devDependencies).toMatchObject({ |
| 55 | + '@analogjs/vite-plugin-angular': V19_X_ANALOG_JS_VITE_PLUGIN_ANGULAR, |
| 56 | + '@analogjs/vitest-angular': V19_X_ANALOG_JS_VITEST_ANGULAR, |
| 57 | + vitest: V19_X_VITEST, |
| 58 | + jsdom: V19_X_JSDOM, |
| 59 | + vite: V19_X_VITE, |
| 60 | + 'vite-tsconfig-paths': V19_X_VITE_TSCONFIG_PATHS, |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should modify project configuration', async () => { |
| 65 | + await setupVitestGenerator(tree, { |
| 66 | + project: 'test-app', |
| 67 | + }); |
| 68 | + |
| 69 | + const projectConfig = JSON.parse( |
| 70 | + tree.read('test-app/project.json', 'utf-8'), |
| 71 | + ); |
| 72 | + |
| 73 | + expect(projectConfig.targets.test).toBeDefined(); |
| 74 | + expect(projectConfig.targets.test.executor).toBe( |
| 75 | + '@analogjs/vitest-angular:test', |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should create vite.config.mts with correct content', async () => { |
| 80 | + await setupVitestGenerator(tree, { |
| 81 | + project: 'test-app', |
| 82 | + }); |
| 83 | + |
| 84 | + expect(tree.exists('test-app/vite.config.mts')).toBeTruthy(); |
| 85 | + const vitestConfig = tree.read('test-app/vite.config.mts', 'utf-8'); |
| 86 | + expect(vitestConfig).toEqual( |
| 87 | + [ |
| 88 | + '/// <reference types="vitest" />', |
| 89 | + '', |
| 90 | + `import angular from '@analogjs/vite-plugin-angular';`, |
| 91 | + '', |
| 92 | + `import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';`, |
| 93 | + '', |
| 94 | + `import { defineConfig } from 'vite';`, |
| 95 | + '', |
| 96 | + '// https://vitejs.dev/config/', |
| 97 | + 'export default defineConfig(({ mode }) => {', |
| 98 | + ' return {', |
| 99 | + ' plugins: [angular(), nxViteTsPaths()],', |
| 100 | + ' test: {', |
| 101 | + ' globals: true,', |
| 102 | + " environment: 'jsdom',", |
| 103 | + " setupFiles: ['src/test-setup.ts'],", |
| 104 | + " include: ['**/*.spec.ts'],", |
| 105 | + " reporters: ['default'],", |
| 106 | + ' },', |
| 107 | + ' define: {', |
| 108 | + " 'import.meta.vitest': mode !== 'production',", |
| 109 | + ' },', |
| 110 | + ' };', |
| 111 | + '});', |
| 112 | + '', |
| 113 | + ].join('\n'), |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should update tsconfig.spec.json', async () => { |
| 118 | + // Create initial tsconfig.spec.json without files property |
| 119 | + writeJson(tree, 'test-app/tsconfig.spec.json', { |
| 120 | + compilerOptions: { |
| 121 | + types: ['node'], |
| 122 | + module: 'commonjs', |
| 123 | + }, |
| 124 | + }); |
| 125 | + |
| 126 | + await setupVitestGenerator(tree, { |
| 127 | + project: 'test-app', |
| 128 | + }); |
| 129 | + |
| 130 | + const tsconfig = JSON.parse( |
| 131 | + tree.read('test-app/tsconfig.spec.json', 'utf-8'), |
| 132 | + ); |
| 133 | + |
| 134 | + // Check all modifications made by updateTsConfig |
| 135 | + expect(tsconfig.compilerOptions.module).toBeUndefined(); |
| 136 | + expect(tsconfig.compilerOptions.target).toBe('es2016'); |
| 137 | + expect(tsconfig.compilerOptions.types).toEqual(['node', 'vitest/globals']); |
| 138 | + expect(tsconfig.files).toEqual(['src/test-setup.ts']); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should add vite 6 when using nx 20.5', async () => { |
| 142 | + // Setup with Nx 20.5 |
| 143 | + writeJson(tree, 'package.json', { |
| 144 | + dependencies: { |
| 145 | + '@angular/core': '19.0.0', |
| 146 | + '@nx/angular': '20.5.0', |
| 147 | + }, |
| 148 | + devDependencies: {}, |
| 149 | + }); |
| 150 | + |
| 151 | + await setupVitestGenerator(tree, { |
| 152 | + project: 'test-app', |
| 153 | + }); |
| 154 | + |
| 155 | + const packageJson = JSON.parse(tree.read('package.json', 'utf-8')); |
| 156 | + expect(packageJson.devDependencies.vite).toBe(NX_X_LATEST_VITE); // '^6.0.0' |
| 157 | + expect(packageJson.devDependencies.vitest).toBe(NX_X_LATEST_VITEST); // '^3.0.0' |
| 158 | + }); |
| 159 | + |
| 160 | + it('should create test-setup.ts with correct content', async () => { |
| 161 | + await setupVitestGenerator(tree, { |
| 162 | + project: 'test-app', |
| 163 | + }); |
| 164 | + |
| 165 | + expect(tree.exists('test-app/src/test-setup.ts')).toBeTruthy(); |
| 166 | + |
| 167 | + const setupContent = tree.read('test-app/src/test-setup.ts', 'utf-8'); |
| 168 | + expect(setupContent).toEqual( |
| 169 | + [ |
| 170 | + `import '@analogjs/vitest-angular/setup-zone';`, |
| 171 | + '', |
| 172 | + 'import {', |
| 173 | + ' BrowserDynamicTestingModule,', |
| 174 | + ' platformBrowserDynamicTesting,', |
| 175 | + `} from '@angular/platform-browser-dynamic/testing';`, |
| 176 | + `import { getTestBed } from '@angular/core/testing';`, |
| 177 | + '', |
| 178 | + 'getTestBed().initTestEnvironment(', |
| 179 | + ' BrowserDynamicTestingModule,', |
| 180 | + ' platformBrowserDynamicTesting(),', |
| 181 | + ');', |
| 182 | + '', |
| 183 | + ].join('\n'), |
| 184 | + ); |
| 185 | + }); |
| 186 | +}); |
0 commit comments