|
5 | 5 | logger, |
6 | 6 | readProjectConfiguration, |
7 | 7 | Tree, |
| 8 | + updateProjectConfiguration, |
8 | 9 | } from '@nx/devkit'; |
9 | 10 | import { addGenerateApiGenerator } from './generator'; |
10 | 11 | import { AddGenerateApiSchema } from './schema'; |
@@ -206,4 +207,108 @@ describe('add-generate-api-target generator', () => { |
206 | 207 | '[@lambda-solutions/nx-plugin-openapi] ✨ Successfully added generate-api target to test-app project' |
207 | 208 | ); |
208 | 209 | }); |
| 210 | + |
| 211 | + describe('custom target name', () => { |
| 212 | + it('should use custom target name when provided', async () => { |
| 213 | + const options: AddGenerateApiSchema = { |
| 214 | + project: 'test-app', |
| 215 | + inputSpec: 'swagger.json', |
| 216 | + outputPath: 'libs/api', |
| 217 | + targetName: 'generate-client', |
| 218 | + }; |
| 219 | + |
| 220 | + await addGenerateApiGenerator(tree, options); |
| 221 | + |
| 222 | + const projectConfig = readProjectConfiguration(tree, 'test-app'); |
| 223 | + const target = projectConfig.targets['generate-client']; |
| 224 | + |
| 225 | + expect(target).toBeDefined(); |
| 226 | + expect(projectConfig.targets['generate-api']).toBeUndefined(); |
| 227 | + expect(target.executor).toBe( |
| 228 | + '@lambda-solutions/nx-plugin-openapi:generate-api' |
| 229 | + ); |
| 230 | + }); |
| 231 | + |
| 232 | + it('should add custom target to build dependsOn', async () => { |
| 233 | + // Add build target |
| 234 | + const projectConfig = readProjectConfiguration(tree, 'test-app'); |
| 235 | + projectConfig.targets['build'] = { |
| 236 | + executor: '@nx/webpack:build', |
| 237 | + options: {}, |
| 238 | + }; |
| 239 | + updateProjectConfiguration(tree, 'test-app', projectConfig); |
| 240 | + |
| 241 | + const options: AddGenerateApiSchema = { |
| 242 | + project: 'test-app', |
| 243 | + inputSpec: 'swagger.json', |
| 244 | + outputPath: 'libs/api', |
| 245 | + targetName: 'generate-client', |
| 246 | + }; |
| 247 | + |
| 248 | + await addGenerateApiGenerator(tree, options); |
| 249 | + |
| 250 | + const updatedConfig = readProjectConfiguration(tree, 'test-app'); |
| 251 | + expect(updatedConfig.targets['build'].dependsOn).toContain('generate-client'); |
| 252 | + }); |
| 253 | + |
| 254 | + it('should add default target name to build dependsOn when no custom name', async () => { |
| 255 | + // Add build target |
| 256 | + const projectConfig = readProjectConfiguration(tree, 'test-app'); |
| 257 | + projectConfig.targets['build'] = { |
| 258 | + executor: '@nx/webpack:build', |
| 259 | + options: {}, |
| 260 | + }; |
| 261 | + updateProjectConfiguration(tree, 'test-app', projectConfig); |
| 262 | + |
| 263 | + const options: AddGenerateApiSchema = { |
| 264 | + project: 'test-app', |
| 265 | + inputSpec: 'swagger.json', |
| 266 | + outputPath: 'libs/api', |
| 267 | + }; |
| 268 | + |
| 269 | + await addGenerateApiGenerator(tree, options); |
| 270 | + |
| 271 | + const updatedConfig = readProjectConfiguration(tree, 'test-app'); |
| 272 | + expect(updatedConfig.targets['build'].dependsOn).toContain('generate-api'); |
| 273 | + }); |
| 274 | + |
| 275 | + it('should not duplicate target in build dependsOn', async () => { |
| 276 | + // Add build target with existing dependsOn |
| 277 | + const projectConfig = readProjectConfiguration(tree, 'test-app'); |
| 278 | + projectConfig.targets['build'] = { |
| 279 | + executor: '@nx/webpack:build', |
| 280 | + options: {}, |
| 281 | + dependsOn: ['generate-client'], |
| 282 | + }; |
| 283 | + updateProjectConfiguration(tree, 'test-app', projectConfig); |
| 284 | + |
| 285 | + const options: AddGenerateApiSchema = { |
| 286 | + project: 'test-app', |
| 287 | + inputSpec: 'swagger.json', |
| 288 | + outputPath: 'libs/api', |
| 289 | + targetName: 'generate-client', |
| 290 | + }; |
| 291 | + |
| 292 | + await addGenerateApiGenerator(tree, options); |
| 293 | + |
| 294 | + const updatedConfig = readProjectConfiguration(tree, 'test-app'); |
| 295 | + expect(updatedConfig.targets['build'].dependsOn).toEqual(['generate-client']); |
| 296 | + expect(updatedConfig.targets['build'].dependsOn.length).toBe(1); |
| 297 | + }); |
| 298 | + |
| 299 | + it('should log correct success message with custom target name', async () => { |
| 300 | + const options: AddGenerateApiSchema = { |
| 301 | + project: 'test-app', |
| 302 | + inputSpec: 'swagger.json', |
| 303 | + outputPath: 'libs/api', |
| 304 | + targetName: 'my-custom-api', |
| 305 | + }; |
| 306 | + |
| 307 | + await addGenerateApiGenerator(tree, options); |
| 308 | + |
| 309 | + expect(mockedLogger.info).toHaveBeenCalledWith( |
| 310 | + '[@lambda-solutions/nx-plugin-openapi] ✨ Successfully added my-custom-api target to test-app project' |
| 311 | + ); |
| 312 | + }); |
| 313 | + }); |
209 | 314 | }); |
0 commit comments