From 399ba0f6257ec96337dfedd68f3766acc424fd7f Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Wed, 8 Jan 2025 11:20:06 +0100 Subject: [PATCH 01/20] tests: add test skeleton --- .../targets/create-targets.unit-test.ts | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts new file mode 100644 index 0000000..fb7019c --- /dev/null +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -0,0 +1,60 @@ +import { describe } from 'vitest'; + +describe('createProjectConfiguration', (): void => { + + // spies + // const { environments, packages } = normalizeCreateNodesOptions(options); + it('should call normalizeCreateNodesOptions ones with options', (): void => { + + }); + // const isE2eProject = isEnvProject(projectConfiguration, environments); + it('should call normalizeCreateNodesOptions ones with projectConfiguration and environments', (): void => { + + }); + // const isPublishableProject = isPkgProject(projectConfiguration, packages); + it('should call isPublishableProject ones with projectConfiguration and packages', (): void => { + + }); + + // if (!isE2eProject && !isPublishableProject) { + // return {}; + // } + it('should return empty object if !isE2eProject and !isPublishableProject', (): void => { + + }) + + // if (isE2eProject && !projectConfiguration.implicitDependencies?.length) { + // logger.warn( + // `Project ${projectConfiguration.name} is an environment project but has no implicit dependencies.` + // ); + // } + // i need a spy or mock for warn? idk + it('should log warn if isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { + + }) + + // i should check for ENVIRONMENT TARGETS structure and PACKAGE TARGETS + it('should generate project configuration with correct structure', (): void => { + + }); + + // spies + // ...verdaccioTargets(projectConfiguration, { + // environmentsDir: environments.environmentsDir, + // } + it('should call verdaccioTargets ones with correct arguments', (): void => { + + }); + + // ...getEnvTargets(projectConfiguration, environments) + it('should call verdaccioTargets ones with correct arguments', (): void => { + + }); + + // ...updateEnvTargetNames(projectConfiguration + it('should call updateEnvTargetNames ones with correct arguments', (): void => { + + }); + + //getPkgTargets() not sure if this should be a spy +}) From c0adb816553cf73ce7b2d1c3d9ad9f5585556a33 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Wed, 8 Jan 2025 16:51:55 +0100 Subject: [PATCH 02/20] tests: add normalizeCreateNodesOptions spy and test --- .../targets/create-targets.unit-test.ts | 51 +++++++++++++++++-- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index fb7019c..fc8c9f7 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -1,12 +1,55 @@ -import { describe } from 'vitest'; +import { beforeEach, describe, MockInstance } from 'vitest'; +import { ProjectConfiguration } from '@nx/devkit'; + +import { createProjectConfiguration } from './create-targets'; + +import { NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; +import { NxVerdaccioCreateNodeOptions } from '../schema'; + +import * as normalizeCreateNodesModule from './../normalize-create-nodes-options'; describe('createProjectConfiguration', (): void => { + const config: ProjectConfiguration = { + root: 'mock-root', + name: 'unit-test-project', + targets: { build: { executor: 'nx:build', options: {} },}, + tags: ['env:production'], + }; + + const options: NormalizedCreateNodeOptions = { + environments: { + targetNames: ['build'], + environmentsDir: './environments', + }, + packages: { + targetNames: ['test', 'lint'], + environmentsDir: './packages', + filterByTags: ['env:production', 'utility'], + }, + }; + + let normalizeCreateNodesOptionsSpy: MockInstance< + [options: NxVerdaccioCreateNodeOptions], + NormalizedCreateNodeOptions + >; + + beforeEach((): void => { + normalizeCreateNodesOptionsSpy = vi + .spyOn(normalizeCreateNodesModule, 'normalizeCreateNodesOptions') + .mockReturnValue(options) + }) - // spies - // const { environments, packages } = normalizeCreateNodesOptions(options); - it('should call normalizeCreateNodesOptions ones with options', (): void => { + afterEach((): void => { + normalizeCreateNodesOptionsSpy.mockRestore(); + }) + it('should call normalizeCreateNodesOptions ones with config and options', (): void => { + createProjectConfiguration(config, options); + expect(normalizeCreateNodesModule.normalizeCreateNodesOptions).toHaveBeenCalledWith( + options + ); }); + // const isE2eProject = isEnvProject(projectConfiguration, environments); it('should call normalizeCreateNodesOptions ones with projectConfiguration and environments', (): void => { From 73d3a512fcadce3814220019e89503b90f3be6df Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 09:14:09 +0100 Subject: [PATCH 03/20] tests: add isEnvProject spy and test --- .../targets/create-targets.unit-test.ts | 80 +++++++++++-------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index fc8c9f7..188ffcf 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -4,19 +4,24 @@ import { ProjectConfiguration } from '@nx/devkit'; import { createProjectConfiguration } from './create-targets'; import { NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; -import { NxVerdaccioCreateNodeOptions } from '../schema'; +import { + NxVerdaccioCreateNodeOptions, + NxVerdaccioEnvironmentsOptions, +} from '../schema'; import * as normalizeCreateNodesModule from './../normalize-create-nodes-options'; +import * as isEnvProjectModule from './environment.targets'; +import { isEnvProject } from './environment.targets'; describe('createProjectConfiguration', (): void => { const config: ProjectConfiguration = { root: 'mock-root', name: 'unit-test-project', - targets: { build: { executor: 'nx:build', options: {} },}, + targets: { build: { executor: 'nx:build', options: {} } }, tags: ['env:production'], }; - const options: NormalizedCreateNodeOptions = { + const normalizedOptions: NormalizedCreateNodeOptions = { environments: { targetNames: ['build'], environmentsDir: './environments', @@ -28,43 +33,62 @@ describe('createProjectConfiguration', (): void => { }, }; + const options: NxVerdaccioCreateNodeOptions = { + environments: { + targetNames: ['build'], // Minimal required to pass validation + }, + }; + let normalizeCreateNodesOptionsSpy: MockInstance< [options: NxVerdaccioCreateNodeOptions], NormalizedCreateNodeOptions >; + let isEnvProjectSpy: MockInstance< + [projectConfig: ProjectConfiguration, options: NormalizedCreateNodeOptions['environments']], + boolean + >; + beforeEach((): void => { normalizeCreateNodesOptionsSpy = vi .spyOn(normalizeCreateNodesModule, 'normalizeCreateNodesOptions') - .mockReturnValue(options) - }) + .mockReturnValue(normalizedOptions); + isEnvProjectSpy = vi + .spyOn(isEnvProjectModule, 'isEnvProject') + .mockReturnValue(true); + }); afterEach((): void => { normalizeCreateNodesOptionsSpy.mockRestore(); - }) + }); it('should call normalizeCreateNodesOptions ones with config and options', (): void => { createProjectConfiguration(config, options); - expect(normalizeCreateNodesModule.normalizeCreateNodesOptions).toHaveBeenCalledWith( - options - ); + expect( + normalizeCreateNodesModule.normalizeCreateNodesOptions + ).toHaveBeenCalledOnce(); + expect( + normalizeCreateNodesModule.normalizeCreateNodesOptions + ).toHaveBeenCalledWith(options); }); // const isE2eProject = isEnvProject(projectConfiguration, environments); - it('should call normalizeCreateNodesOptions ones with projectConfiguration and environments', (): void => { - + it('should call isEnvProject ones with projectConfiguration and environments', (): void => { + createProjectConfiguration(config, options); + expect(isEnvProjectModule.isEnvProject).toHaveBeenCalledOnce(); + expect(isEnvProjectModule.isEnvProject).toHaveBeenCalledWith( + config, + normalizedOptions['environments'] + ); }); - // const isPublishableProject = isPkgProject(projectConfiguration, packages); - it('should call isPublishableProject ones with projectConfiguration and packages', (): void => { - }); + // const isPublishableProject = isPkgProject(projectConfiguration, packages); + it('should call isPublishableProject ones with projectConfiguration and packages', (): void => {}); // if (!isE2eProject && !isPublishableProject) { // return {}; // } - it('should return empty object if !isE2eProject and !isPublishableProject', (): void => { - - }) + it('should return empty object if !isE2eProject and !isPublishableProject', (): void => {}); // if (isE2eProject && !projectConfiguration.implicitDependencies?.length) { // logger.warn( @@ -72,32 +96,22 @@ describe('createProjectConfiguration', (): void => { // ); // } // i need a spy or mock for warn? idk - it('should log warn if isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { - - }) + it('should log warn if isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => {}); // i should check for ENVIRONMENT TARGETS structure and PACKAGE TARGETS - it('should generate project configuration with correct structure', (): void => { - - }); + it('should generate project configuration with correct structure', (): void => {}); // spies // ...verdaccioTargets(projectConfiguration, { // environmentsDir: environments.environmentsDir, // } - it('should call verdaccioTargets ones with correct arguments', (): void => { - - }); + it('should call verdaccioTargets ones with correct arguments', (): void => {}); // ...getEnvTargets(projectConfiguration, environments) - it('should call verdaccioTargets ones with correct arguments', (): void => { - - }); + it('should call verdaccioTargets ones with correct arguments', (): void => {}); // ...updateEnvTargetNames(projectConfiguration - it('should call updateEnvTargetNames ones with correct arguments', (): void => { - - }); + it('should call updateEnvTargetNames ones with correct arguments', (): void => {}); //getPkgTargets() not sure if this should be a spy -}) +}); From eb0390d5cff4fa3b2c4ad5cf174b106dc697d2ed Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 10:47:05 +0100 Subject: [PATCH 04/20] tests: add isPkgProject spy and test --- .../targets/create-targets.unit-test.ts | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index 188ffcf..e34c7ac 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -7,11 +7,12 @@ import { NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; import { NxVerdaccioCreateNodeOptions, NxVerdaccioEnvironmentsOptions, + NxVerdaccioPackagesOptions, } from '../schema'; import * as normalizeCreateNodesModule from './../normalize-create-nodes-options'; -import * as isEnvProjectModule from './environment.targets'; -import { isEnvProject } from './environment.targets'; +import * as environmentTargetsModule from './environment.targets'; +import * as packageTargetsModule from './package.targets'; describe('createProjectConfiguration', (): void => { const config: ProjectConfiguration = { @@ -45,7 +46,15 @@ describe('createProjectConfiguration', (): void => { >; let isEnvProjectSpy: MockInstance< - [projectConfig: ProjectConfiguration, options: NormalizedCreateNodeOptions['environments']], + [ + projectConfig: ProjectConfiguration, + options: NormalizedCreateNodeOptions['environments'] + ], + boolean + >; + + let isPkgSpy: MockInstance< + [projectConfig: ProjectConfiguration, options: NxVerdaccioPackagesOptions], boolean >; @@ -54,7 +63,10 @@ describe('createProjectConfiguration', (): void => { .spyOn(normalizeCreateNodesModule, 'normalizeCreateNodesOptions') .mockReturnValue(normalizedOptions); isEnvProjectSpy = vi - .spyOn(isEnvProjectModule, 'isEnvProject') + .spyOn(environmentTargetsModule, 'isEnvProject') + .mockReturnValue(true); + isPkgSpy = vi + .spyOn(packageTargetsModule, 'isPkgProject') .mockReturnValue(true); }); @@ -72,18 +84,23 @@ describe('createProjectConfiguration', (): void => { ).toHaveBeenCalledWith(options); }); - // const isE2eProject = isEnvProject(projectConfiguration, environments); it('should call isEnvProject ones with projectConfiguration and environments', (): void => { createProjectConfiguration(config, options); - expect(isEnvProjectModule.isEnvProject).toHaveBeenCalledOnce(); - expect(isEnvProjectModule.isEnvProject).toHaveBeenCalledWith( + expect(environmentTargetsModule.isEnvProject).toHaveBeenCalledOnce(); + expect(environmentTargetsModule.isEnvProject).toHaveBeenCalledWith( config, normalizedOptions['environments'] ); }); - // const isPublishableProject = isPkgProject(projectConfiguration, packages); - it('should call isPublishableProject ones with projectConfiguration and packages', (): void => {}); + it('should call isPublishableProject ones with projectConfiguration and packages', (): void => { + createProjectConfiguration(config, options); + expect(packageTargetsModule.isPkgProject).toHaveBeenCalledOnce(); + expect(packageTargetsModule.isPkgProject).toHaveBeenCalledWith( + config, + normalizedOptions['packages'] + ); + }); // if (!isE2eProject && !isPublishableProject) { // return {}; From 3a88a54de0fc1c75bbe3d52b1815e289d5bb16ab Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 11:14:49 +0100 Subject: [PATCH 05/20] tests: should return empty object if !isE2eProject and !isPublishableProject --- .../src/plugin/targets/create-targets.unit-test.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index e34c7ac..e89f9ea 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -1,4 +1,4 @@ -import { beforeEach, describe, MockInstance } from 'vitest'; +import { beforeEach, describe, expect, MockInstance } from 'vitest'; import { ProjectConfiguration } from '@nx/devkit'; import { createProjectConfiguration } from './create-targets'; @@ -72,6 +72,8 @@ describe('createProjectConfiguration', (): void => { afterEach((): void => { normalizeCreateNodesOptionsSpy.mockRestore(); + isEnvProjectSpy.mockRestore(); + isPkgSpy.mockRestore(); }); it('should call normalizeCreateNodesOptions ones with config and options', (): void => { @@ -102,10 +104,12 @@ describe('createProjectConfiguration', (): void => { ); }); - // if (!isE2eProject && !isPublishableProject) { - // return {}; - // } - it('should return empty object if !isE2eProject and !isPublishableProject', (): void => {}); + it('should return empty object if !isE2eProject and !isPublishableProject', (): void => { + isEnvProjectSpy.mockReturnValue(false); + isPkgSpy.mockReturnValue(false); + const projectConfiguration = createProjectConfiguration(config, options); + expect(projectConfiguration).toStrictEqual({}) + }); // if (isE2eProject && !projectConfiguration.implicitDependencies?.length) { // logger.warn( From 8a8fcf5015ac04d8e5e249555205d151457a2d8d Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 11:31:26 +0100 Subject: [PATCH 06/20] tests: add logger warn tests --- .../targets/create-targets.unit-test.ts | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index e89f9ea..369721e 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -6,7 +6,6 @@ import { createProjectConfiguration } from './create-targets'; import { NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; import { NxVerdaccioCreateNodeOptions, - NxVerdaccioEnvironmentsOptions, NxVerdaccioPackagesOptions, } from '../schema'; @@ -14,6 +13,8 @@ import * as normalizeCreateNodesModule from './../normalize-create-nodes-options import * as environmentTargetsModule from './environment.targets'; import * as packageTargetsModule from './package.targets'; +import * as nxDevkitModule from '@nx/devkit'; + describe('createProjectConfiguration', (): void => { const config: ProjectConfiguration = { root: 'mock-root', @@ -40,6 +41,14 @@ describe('createProjectConfiguration', (): void => { }, }; + vi.mock('@nx/devkit', () => { + return { + logger: { + warn: vi.fn() + } + } + }) + let normalizeCreateNodesOptionsSpy: MockInstance< [options: NxVerdaccioCreateNodeOptions], NormalizedCreateNodeOptions @@ -111,13 +120,32 @@ describe('createProjectConfiguration', (): void => { expect(projectConfiguration).toStrictEqual({}) }); - // if (isE2eProject && !projectConfiguration.implicitDependencies?.length) { - // logger.warn( - // `Project ${projectConfiguration.name} is an environment project but has no implicit dependencies.` - // ); - // } - // i need a spy or mock for warn? idk - it('should log warn if isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => {}); + + it('should log warn if isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { + createProjectConfiguration(config, options); + expect(nxDevkitModule.logger.warn).toHaveBeenCalledOnce(); + }); + + it('should not log warn if isE2eProject and projectConfiguration.implicitDependencies?.length', (): void => { + const configWithImplicitDependencies = {...config, implicitDependencies: ['mock-implicit-dep']} + createProjectConfiguration(configWithImplicitDependencies, options); + expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); + }); + + it('should not log warn if !isE2eProject and projectConfiguration.implicitDependencies?.length', (): void => { + isEnvProjectSpy.mockReturnValue(false); + const configWithImplicitDependencies = {...config, implicitDependencies: ['mock-implicit-dep']} + createProjectConfiguration(configWithImplicitDependencies, options); + expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); + }); + + it('should not log warn if !isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { + isEnvProjectSpy.mockReturnValue(false); + createProjectConfiguration(config, options); + expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); + }); + + // i should check for ENVIRONMENT TARGETS structure and PACKAGE TARGETS it('should generate project configuration with correct structure', (): void => {}); From 37b765469a69a2a5c66154178c4e00ab35a7f0c5 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 12:26:54 +0100 Subject: [PATCH 07/20] tests: add project configuration top level structure --- .../targets/create-targets.unit-test.ts | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index 369721e..a70ca34 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -14,6 +14,15 @@ import * as environmentTargetsModule from './environment.targets'; import * as packageTargetsModule from './package.targets'; import * as nxDevkitModule from '@nx/devkit'; +import { + TARGET_ENVIRONMENT_BOOTSTRAP, TARGET_ENVIRONMENT_E2E, + TARGET_ENVIRONMENT_INSTALL, + TARGET_ENVIRONMENT_PUBLISH_ONLY, + TARGET_ENVIRONMENT_SETUP, + TARGET_ENVIRONMENT_TEARDOWN, + TARGET_ENVIRONMENT_VERDACCIO_START, + TARGET_ENVIRONMENT_VERDACCIO_STOP +} from './environment.targets'; describe('createProjectConfiguration', (): void => { const config: ProjectConfiguration = { @@ -145,10 +154,41 @@ describe('createProjectConfiguration', (): void => { expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); }); + it('should generate project configuration with namedInputs and targets if isE2eProject and isPublishableProject', (): void => { + const result = createProjectConfiguration(config, options); + expect(result).toMatchObject({ + namedInputs: expect.any(Object), + targets: expect.any(Object), + }); + }); + it('should generate project configuration with namedInputs if isE2eProject and !isPublishableProject', (): void => { + const result = createProjectConfiguration(config, options); + expect(result).toMatchObject({ + namedInputs: expect.any(Object), + }); + }); + + it('should generate project configuration with targets if !isE2eProject and isPublishableProject', (): void => { + const result = createProjectConfiguration(config, options); + expect(result).toMatchObject({ + targets: expect.any(Object), + }); + }); - // i should check for ENVIRONMENT TARGETS structure and PACKAGE TARGETS - it('should generate project configuration with correct structure', (): void => {}); + // it('should generate nameInputs with correct structure and data', (): void => { + // const result = createProjectConfiguration(config, options); + // expect(result['namedInputs']).toMatchObject({ + // [TARGET_ENVIRONMENT_VERDACCIO_START]: expect.any(Object), + // [TARGET_ENVIRONMENT_VERDACCIO_STOP]: expect.any(Object), + // [TARGET_ENVIRONMENT_BOOTSTRAP]: expect.any(Object), + // [TARGET_ENVIRONMENT_INSTALL]: expect.any(Object), + // [TARGET_ENVIRONMENT_PUBLISH_ONLY]: expect.any(Object), + // [TARGET_ENVIRONMENT_SETUP]: expect.any(Object), + // [TARGET_ENVIRONMENT_TEARDOWN]: expect.any(Object), + // [TARGET_ENVIRONMENT_E2E]: expect.any(Object), + // }); + // }); // spies // ...verdaccioTargets(projectConfiguration, { From b559c855ff6edac8d61c6fe878ea3d879964dac9 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 13:06:23 +0100 Subject: [PATCH 08/20] tests: add verdaccioTargets spy and test --- .../targets/create-targets.unit-test.ts | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index a70ca34..aaff7bd 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -21,10 +21,15 @@ import { TARGET_ENVIRONMENT_SETUP, TARGET_ENVIRONMENT_TEARDOWN, TARGET_ENVIRONMENT_VERDACCIO_START, - TARGET_ENVIRONMENT_VERDACCIO_STOP + TARGET_ENVIRONMENT_VERDACCIO_STOP, verdaccioTargets } from './environment.targets'; describe('createProjectConfiguration', (): void => { + const verdaccioTargetsMock = { + [TARGET_ENVIRONMENT_VERDACCIO_START]: {}, + [TARGET_ENVIRONMENT_VERDACCIO_STOP]: {} + } + const config: ProjectConfiguration = { root: 'mock-root', name: 'unit-test-project', @@ -76,6 +81,8 @@ describe('createProjectConfiguration', (): void => { boolean >; + let verdaccioTargetsSpy; + beforeEach((): void => { normalizeCreateNodesOptionsSpy = vi .spyOn(normalizeCreateNodesModule, 'normalizeCreateNodesOptions') @@ -86,12 +93,16 @@ describe('createProjectConfiguration', (): void => { isPkgSpy = vi .spyOn(packageTargetsModule, 'isPkgProject') .mockReturnValue(true); + verdaccioTargetsSpy = vi + .spyOn(environmentTargetsModule, 'verdaccioTargets') + .mockReturnValue(verdaccioTargetsMock) }); afterEach((): void => { normalizeCreateNodesOptionsSpy.mockRestore(); isEnvProjectSpy.mockRestore(); isPkgSpy.mockRestore(); + verdaccioTargetsSpy.mockRestore(); }); it('should call normalizeCreateNodesOptions ones with config and options', (): void => { @@ -190,11 +201,15 @@ describe('createProjectConfiguration', (): void => { // }); // }); - // spies - // ...verdaccioTargets(projectConfiguration, { - // environmentsDir: environments.environmentsDir, - // } - it('should call verdaccioTargets ones with correct arguments', (): void => {}); + + it('should call verdaccioTargets ones with correct arguments', (): void => { + createProjectConfiguration(config, options); + expect(environmentTargetsModule.verdaccioTargets).toHaveBeenCalledOnce(); + expect(environmentTargetsModule.verdaccioTargets).toHaveBeenCalledWith( + config, + {environmentsDir: normalizedOptions.environments.environmentsDir} + ); + }); // ...getEnvTargets(projectConfiguration, environments) it('should call verdaccioTargets ones with correct arguments', (): void => {}); From 49f1f3ea877ab16225da17a9efee2c06a5b970c7 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 13:08:04 +0100 Subject: [PATCH 09/20] tests: add verdaccioTargets spy and test --- .../targets/create-targets.unit-test.ts | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index aaff7bd..cfa3be3 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -1,11 +1,12 @@ import { beforeEach, describe, expect, MockInstance } from 'vitest'; -import { ProjectConfiguration } from '@nx/devkit'; +import { ProjectConfiguration, type TargetConfiguration } from '@nx/devkit'; import { createProjectConfiguration } from './create-targets'; import { NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; import { NxVerdaccioCreateNodeOptions, + NxVerdaccioEnvironmentsOptions, NxVerdaccioPackagesOptions, } from '../schema'; @@ -15,20 +16,23 @@ import * as packageTargetsModule from './package.targets'; import * as nxDevkitModule from '@nx/devkit'; import { - TARGET_ENVIRONMENT_BOOTSTRAP, TARGET_ENVIRONMENT_E2E, + TARGET_ENVIRONMENT_BOOTSTRAP, + TARGET_ENVIRONMENT_E2E, TARGET_ENVIRONMENT_INSTALL, TARGET_ENVIRONMENT_PUBLISH_ONLY, TARGET_ENVIRONMENT_SETUP, TARGET_ENVIRONMENT_TEARDOWN, TARGET_ENVIRONMENT_VERDACCIO_START, - TARGET_ENVIRONMENT_VERDACCIO_STOP, verdaccioTargets + TARGET_ENVIRONMENT_VERDACCIO_STOP, + verdaccioTargets, } from './environment.targets'; +import { StartVerdaccioOptions } from '../../executors/env-bootstrap/verdaccio-registry'; describe('createProjectConfiguration', (): void => { const verdaccioTargetsMock = { [TARGET_ENVIRONMENT_VERDACCIO_START]: {}, - [TARGET_ENVIRONMENT_VERDACCIO_STOP]: {} - } + [TARGET_ENVIRONMENT_VERDACCIO_STOP]: {}, + }; const config: ProjectConfiguration = { root: 'mock-root', @@ -55,13 +59,13 @@ describe('createProjectConfiguration', (): void => { }, }; - vi.mock('@nx/devkit', () => { + vi.mock('@nx/devkit', () => { return { logger: { - warn: vi.fn() - } - } - }) + warn: vi.fn(), + }, + }; + }); let normalizeCreateNodesOptionsSpy: MockInstance< [options: NxVerdaccioCreateNodeOptions], @@ -81,7 +85,17 @@ describe('createProjectConfiguration', (): void => { boolean >; - let verdaccioTargetsSpy; + let verdaccioTargetsSpy: MockInstance< + [ + projectConfig: ProjectConfiguration, + options: Pick< + NormalizedCreateNodeOptions['environments'], + 'environmentsDir' + > & + Omit + ], + Record + >; beforeEach((): void => { normalizeCreateNodesOptionsSpy = vi @@ -95,7 +109,7 @@ describe('createProjectConfiguration', (): void => { .mockReturnValue(true); verdaccioTargetsSpy = vi .spyOn(environmentTargetsModule, 'verdaccioTargets') - .mockReturnValue(verdaccioTargetsMock) + .mockReturnValue(verdaccioTargetsMock); }); afterEach((): void => { @@ -137,24 +151,29 @@ describe('createProjectConfiguration', (): void => { isEnvProjectSpy.mockReturnValue(false); isPkgSpy.mockReturnValue(false); const projectConfiguration = createProjectConfiguration(config, options); - expect(projectConfiguration).toStrictEqual({}) + expect(projectConfiguration).toStrictEqual({}); }); - it('should log warn if isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { createProjectConfiguration(config, options); expect(nxDevkitModule.logger.warn).toHaveBeenCalledOnce(); }); it('should not log warn if isE2eProject and projectConfiguration.implicitDependencies?.length', (): void => { - const configWithImplicitDependencies = {...config, implicitDependencies: ['mock-implicit-dep']} + const configWithImplicitDependencies = { + ...config, + implicitDependencies: ['mock-implicit-dep'], + }; createProjectConfiguration(configWithImplicitDependencies, options); expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); }); it('should not log warn if !isE2eProject and projectConfiguration.implicitDependencies?.length', (): void => { isEnvProjectSpy.mockReturnValue(false); - const configWithImplicitDependencies = {...config, implicitDependencies: ['mock-implicit-dep']} + const configWithImplicitDependencies = { + ...config, + implicitDependencies: ['mock-implicit-dep'], + }; createProjectConfiguration(configWithImplicitDependencies, options); expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); }); @@ -201,13 +220,12 @@ describe('createProjectConfiguration', (): void => { // }); // }); - it('should call verdaccioTargets ones with correct arguments', (): void => { createProjectConfiguration(config, options); expect(environmentTargetsModule.verdaccioTargets).toHaveBeenCalledOnce(); expect(environmentTargetsModule.verdaccioTargets).toHaveBeenCalledWith( config, - {environmentsDir: normalizedOptions.environments.environmentsDir} + { environmentsDir: normalizedOptions.environments.environmentsDir } ); }); From ba2c40bcc79cacf9336176811476bc078c6628d1 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 13:15:03 +0100 Subject: [PATCH 10/20] tests: add getEnvTargets spy and test --- .../targets/create-targets.unit-test.ts | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index cfa3be3..7e4c785 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -16,6 +16,7 @@ import * as packageTargetsModule from './package.targets'; import * as nxDevkitModule from '@nx/devkit'; import { + getEnvTargets, TARGET_ENVIRONMENT_BOOTSTRAP, TARGET_ENVIRONMENT_E2E, TARGET_ENVIRONMENT_INSTALL, @@ -24,7 +25,7 @@ import { TARGET_ENVIRONMENT_TEARDOWN, TARGET_ENVIRONMENT_VERDACCIO_START, TARGET_ENVIRONMENT_VERDACCIO_STOP, - verdaccioTargets, + verdaccioTargets } from './environment.targets'; import { StartVerdaccioOptions } from '../../executors/env-bootstrap/verdaccio-registry'; @@ -34,6 +35,15 @@ describe('createProjectConfiguration', (): void => { [TARGET_ENVIRONMENT_VERDACCIO_STOP]: {}, }; + const envTargetsMock = { + [TARGET_ENVIRONMENT_BOOTSTRAP]: {}, + [TARGET_ENVIRONMENT_INSTALL]: {}, + [TARGET_ENVIRONMENT_PUBLISH_ONLY]: {}, + [TARGET_ENVIRONMENT_SETUP]: {}, + [TARGET_ENVIRONMENT_TEARDOWN]: {}, + [TARGET_ENVIRONMENT_E2E]: {}, + }; + const config: ProjectConfiguration = { root: 'mock-root', name: 'unit-test-project', @@ -97,6 +107,8 @@ describe('createProjectConfiguration', (): void => { Record >; + let getEnvTargetsSpy; + beforeEach((): void => { normalizeCreateNodesOptionsSpy = vi .spyOn(normalizeCreateNodesModule, 'normalizeCreateNodesOptions') @@ -110,6 +122,8 @@ describe('createProjectConfiguration', (): void => { verdaccioTargetsSpy = vi .spyOn(environmentTargetsModule, 'verdaccioTargets') .mockReturnValue(verdaccioTargetsMock); + getEnvTargetsSpy = vi.spyOn(environmentTargetsModule, 'getEnvTargets') + .mockReturnValue(envTargetsMock) }); afterEach((): void => { @@ -229,8 +243,14 @@ describe('createProjectConfiguration', (): void => { ); }); - // ...getEnvTargets(projectConfiguration, environments) - it('should call verdaccioTargets ones with correct arguments', (): void => {}); + it('should call getEnvTargets ones with correct arguments', (): void => { + createProjectConfiguration(config, options); + expect(environmentTargetsModule.getEnvTargets).toHaveBeenCalledOnce(); + expect(environmentTargetsModule.getEnvTargets).toHaveBeenCalledWith( + config, + normalizedOptions.environments + ); + }); // ...updateEnvTargetNames(projectConfiguration it('should call updateEnvTargetNames ones with correct arguments', (): void => {}); From dcc37cd3f82cba5fdd493140b0d0c1a9ee85a6de Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 13:46:45 +0100 Subject: [PATCH 11/20] tests: add object structure --- .../targets/create-targets.unit-test.ts | 65 +++++++++++++------ 1 file changed, 44 insertions(+), 21 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index 7e4c785..54dcfc7 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -28,6 +28,7 @@ import { verdaccioTargets } from './environment.targets'; import { StartVerdaccioOptions } from '../../executors/env-bootstrap/verdaccio-registry'; +import { TARGET_PACKAGE_INSTALL, TARGET_PACKAGE_PUBLISH } from './package.targets'; describe('createProjectConfiguration', (): void => { const verdaccioTargetsMock = { @@ -107,7 +108,22 @@ describe('createProjectConfiguration', (): void => { Record >; - let getEnvTargetsSpy; + let getEnvTargetsSpy: MockInstance< + [ + projectConfig: ProjectConfiguration, + options: Omit< + NxVerdaccioEnvironmentsOptions, + 'targetNames' | 'environmentsDir' + > & + Required< + Pick< + NxVerdaccioEnvironmentsOptions, + 'targetNames' | 'environmentsDir' + > + > + ], + Record> + >; beforeEach((): void => { normalizeCreateNodesOptionsSpy = vi @@ -131,6 +147,7 @@ describe('createProjectConfiguration', (): void => { isEnvProjectSpy.mockRestore(); isPkgSpy.mockRestore(); verdaccioTargetsSpy.mockRestore(); + getEnvTargetsSpy.mockRestore(); }); it('should call normalizeCreateNodesOptions ones with config and options', (): void => { @@ -206,34 +223,42 @@ describe('createProjectConfiguration', (): void => { }); }); - it('should generate project configuration with namedInputs if isE2eProject and !isPublishableProject', (): void => { + it('should generate project configuration with targets if !isE2eProject and isPublishableProject', (): void => { const result = createProjectConfiguration(config, options); expect(result).toMatchObject({ - namedInputs: expect.any(Object), + targets: expect.any(Object), }); }); - it('should generate project configuration with targets if !isE2eProject and isPublishableProject', (): void => { + it('should generate targets with correct structure if isE2eProject and !isPublishableProject', (): void => { + isPkgSpy.mockReturnValue(false); + const result = createProjectConfiguration(config, options); + expect(result['targets']).toMatchObject( + {build: expect.any(Object), + [TARGET_ENVIRONMENT_VERDACCIO_START]: expect.any(Object), + [TARGET_ENVIRONMENT_VERDACCIO_STOP]: expect.any(Object), + [TARGET_ENVIRONMENT_BOOTSTRAP]: expect.any(Object), + [TARGET_ENVIRONMENT_INSTALL]: expect.any(Object), + [TARGET_ENVIRONMENT_PUBLISH_ONLY]: expect.any(Object), + [TARGET_ENVIRONMENT_SETUP]: expect.any(Object), + [TARGET_ENVIRONMENT_TEARDOWN]: expect.any(Object), + [TARGET_ENVIRONMENT_E2E]: expect.any(Object), + } + ); + }); + + it('should generate nameInputs with correct structure and data', (): void => { const result = createProjectConfiguration(config, options); expect(result).toMatchObject({ - targets: expect.any(Object), + namedInputs: expect.any(Object), + targets: + { + [TARGET_PACKAGE_PUBLISH]: expect.any(Object), + [TARGET_PACKAGE_INSTALL]: expect.any(Object), + } }); }); - // it('should generate nameInputs with correct structure and data', (): void => { - // const result = createProjectConfiguration(config, options); - // expect(result['namedInputs']).toMatchObject({ - // [TARGET_ENVIRONMENT_VERDACCIO_START]: expect.any(Object), - // [TARGET_ENVIRONMENT_VERDACCIO_STOP]: expect.any(Object), - // [TARGET_ENVIRONMENT_BOOTSTRAP]: expect.any(Object), - // [TARGET_ENVIRONMENT_INSTALL]: expect.any(Object), - // [TARGET_ENVIRONMENT_PUBLISH_ONLY]: expect.any(Object), - // [TARGET_ENVIRONMENT_SETUP]: expect.any(Object), - // [TARGET_ENVIRONMENT_TEARDOWN]: expect.any(Object), - // [TARGET_ENVIRONMENT_E2E]: expect.any(Object), - // }); - // }); - it('should call verdaccioTargets ones with correct arguments', (): void => { createProjectConfiguration(config, options); expect(environmentTargetsModule.verdaccioTargets).toHaveBeenCalledOnce(); @@ -254,6 +279,4 @@ describe('createProjectConfiguration', (): void => { // ...updateEnvTargetNames(projectConfiguration it('should call updateEnvTargetNames ones with correct arguments', (): void => {}); - - //getPkgTargets() not sure if this should be a spy }); From 03697f531ba460e55f87a555fea3320ae9922abc Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 13:53:00 +0100 Subject: [PATCH 12/20] tests: add updateEnvTargetNames spy and test --- .../plugin/targets/create-targets.unit-test.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index 54dcfc7..13b5e4e 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -24,7 +24,7 @@ import { TARGET_ENVIRONMENT_SETUP, TARGET_ENVIRONMENT_TEARDOWN, TARGET_ENVIRONMENT_VERDACCIO_START, - TARGET_ENVIRONMENT_VERDACCIO_STOP, + TARGET_ENVIRONMENT_VERDACCIO_STOP, updateEnvTargetNames, verdaccioTargets } from './environment.targets'; import { StartVerdaccioOptions } from '../../executors/env-bootstrap/verdaccio-registry'; @@ -125,6 +125,8 @@ describe('createProjectConfiguration', (): void => { Record> >; + let updateEnvTargetNamesSpy; + beforeEach((): void => { normalizeCreateNodesOptionsSpy = vi .spyOn(normalizeCreateNodesModule, 'normalizeCreateNodesOptions') @@ -137,9 +139,8 @@ describe('createProjectConfiguration', (): void => { .mockReturnValue(true); verdaccioTargetsSpy = vi .spyOn(environmentTargetsModule, 'verdaccioTargets') - .mockReturnValue(verdaccioTargetsMock); getEnvTargetsSpy = vi.spyOn(environmentTargetsModule, 'getEnvTargets') - .mockReturnValue(envTargetsMock) + updateEnvTargetNamesSpy = vi.spyOn(environmentTargetsModule, 'updateEnvTargetNames') }); afterEach((): void => { @@ -148,6 +149,7 @@ describe('createProjectConfiguration', (): void => { isPkgSpy.mockRestore(); verdaccioTargetsSpy.mockRestore(); getEnvTargetsSpy.mockRestore(); + updateEnvTargetNamesSpy.mockRestore(); }); it('should call normalizeCreateNodesOptions ones with config and options', (): void => { @@ -277,6 +279,12 @@ describe('createProjectConfiguration', (): void => { ); }); - // ...updateEnvTargetNames(projectConfiguration - it('should call updateEnvTargetNames ones with correct arguments', (): void => {}); + it('should call updateEnvTargetNames ones with correct arguments', (): void => { + createProjectConfiguration(config, options); + expect(environmentTargetsModule.updateEnvTargetNames).toHaveBeenCalledOnce(); + expect(environmentTargetsModule.updateEnvTargetNames).toHaveBeenCalledWith( + config, + normalizedOptions.environments + ); + }); }); From 16379ffc621b67a9906a01af33056690e412b232 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 14:54:59 +0100 Subject: [PATCH 13/20] tests: code cleanup --- .../targets/create-targets.unit-test.ts | 226 +++++++----------- 1 file changed, 84 insertions(+), 142 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index 13b5e4e..4274a62 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -1,57 +1,43 @@ -import { beforeEach, describe, expect, MockInstance } from 'vitest'; -import { ProjectConfiguration, type TargetConfiguration } from '@nx/devkit'; +import { beforeEach, describe, expect, type MockInstance } from 'vitest'; +import { type ProjectConfiguration } from '@nx/devkit'; -import { createProjectConfiguration } from './create-targets'; +import * as nxDevkitMockModule from '@nx/devkit'; -import { NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; +import { createProjectConfiguration } from './create-targets'; import { - NxVerdaccioCreateNodeOptions, - NxVerdaccioEnvironmentsOptions, - NxVerdaccioPackagesOptions, -} from '../schema'; - -import * as normalizeCreateNodesModule from './../normalize-create-nodes-options'; -import * as environmentTargetsModule from './environment.targets'; -import * as packageTargetsModule from './package.targets'; - -import * as nxDevkitModule from '@nx/devkit'; + TARGET_PACKAGE_INSTALL, + TARGET_PACKAGE_PUBLISH, +} from './package.targets'; import { - getEnvTargets, - TARGET_ENVIRONMENT_BOOTSTRAP, TARGET_ENVIRONMENT_E2E, - TARGET_ENVIRONMENT_INSTALL, - TARGET_ENVIRONMENT_PUBLISH_ONLY, TARGET_ENVIRONMENT_SETUP, + TARGET_ENVIRONMENT_INSTALL, TARGET_ENVIRONMENT_TEARDOWN, + TARGET_ENVIRONMENT_BOOTSTRAP, + TARGET_ENVIRONMENT_PUBLISH_ONLY, + TARGET_ENVIRONMENT_VERDACCIO_STOP, TARGET_ENVIRONMENT_VERDACCIO_START, - TARGET_ENVIRONMENT_VERDACCIO_STOP, updateEnvTargetNames, - verdaccioTargets } from './environment.targets'; -import { StartVerdaccioOptions } from '../../executors/env-bootstrap/verdaccio-registry'; -import { TARGET_PACKAGE_INSTALL, TARGET_PACKAGE_PUBLISH } from './package.targets'; -describe('createProjectConfiguration', (): void => { - const verdaccioTargetsMock = { - [TARGET_ENVIRONMENT_VERDACCIO_START]: {}, - [TARGET_ENVIRONMENT_VERDACCIO_STOP]: {}, - }; +import { type NxVerdaccioCreateNodeOptions } from '../schema'; +import { type NormalizedCreateNodeOptions } from '../normalize-create-nodes-options'; - const envTargetsMock = { - [TARGET_ENVIRONMENT_BOOTSTRAP]: {}, - [TARGET_ENVIRONMENT_INSTALL]: {}, - [TARGET_ENVIRONMENT_PUBLISH_ONLY]: {}, - [TARGET_ENVIRONMENT_SETUP]: {}, - [TARGET_ENVIRONMENT_TEARDOWN]: {}, - [TARGET_ENVIRONMENT_E2E]: {}, - }; +import * as packageTargetsSpyModule from './package.targets'; +import * as environmentTargetsModule from './environment.targets'; +import * as normalizeCreateNodesSpyModule from './../normalize-create-nodes-options'; - const config: ProjectConfiguration = { +describe('createProjectConfiguration', (): void => { + const implicitDependencies = ['mock-implicit-dep']; + const projectConfiguration: ProjectConfiguration = { root: 'mock-root', name: 'unit-test-project', - targets: { build: { executor: 'nx:build', options: {} } }, - tags: ['env:production'], + targets: { build: {} }, + }; + const options: NxVerdaccioCreateNodeOptions = { + environments: { + targetNames: ['build'], + }, }; - const normalizedOptions: NormalizedCreateNodeOptions = { environments: { targetNames: ['build'], @@ -60,13 +46,6 @@ describe('createProjectConfiguration', (): void => { packages: { targetNames: ['test', 'lint'], environmentsDir: './packages', - filterByTags: ['env:production', 'utility'], - }, - }; - - const options: NxVerdaccioCreateNodeOptions = { - environments: { - targetNames: ['build'], // Minimal required to pass validation }, }; @@ -78,69 +57,32 @@ describe('createProjectConfiguration', (): void => { }; }); - let normalizeCreateNodesOptionsSpy: MockInstance< - [options: NxVerdaccioCreateNodeOptions], - NormalizedCreateNodeOptions - >; - - let isEnvProjectSpy: MockInstance< - [ - projectConfig: ProjectConfiguration, - options: NormalizedCreateNodeOptions['environments'] - ], - boolean - >; - - let isPkgSpy: MockInstance< - [projectConfig: ProjectConfiguration, options: NxVerdaccioPackagesOptions], - boolean - >; - - let verdaccioTargetsSpy: MockInstance< - [ - projectConfig: ProjectConfiguration, - options: Pick< - NormalizedCreateNodeOptions['environments'], - 'environmentsDir' - > & - Omit - ], - Record - >; - - let getEnvTargetsSpy: MockInstance< - [ - projectConfig: ProjectConfiguration, - options: Omit< - NxVerdaccioEnvironmentsOptions, - 'targetNames' | 'environmentsDir' - > & - Required< - Pick< - NxVerdaccioEnvironmentsOptions, - 'targetNames' | 'environmentsDir' - > - > - ], - Record> - >; - - let updateEnvTargetNamesSpy; + let normalizeCreateNodesOptionsSpy: MockInstance; + let isEnvProjectSpy: MockInstance; + let isPkgSpy: MockInstance; + let verdaccioTargetsSpy: MockInstance; + let getEnvTargetsSpy: MockInstance; + let updateEnvTargetNamesSpy: MockInstance; beforeEach((): void => { normalizeCreateNodesOptionsSpy = vi - .spyOn(normalizeCreateNodesModule, 'normalizeCreateNodesOptions') + .spyOn(normalizeCreateNodesSpyModule, 'normalizeCreateNodesOptions') .mockReturnValue(normalizedOptions); isEnvProjectSpy = vi .spyOn(environmentTargetsModule, 'isEnvProject') .mockReturnValue(true); isPkgSpy = vi - .spyOn(packageTargetsModule, 'isPkgProject') + .spyOn(packageTargetsSpyModule, 'isPkgProject') .mockReturnValue(true); - verdaccioTargetsSpy = vi - .spyOn(environmentTargetsModule, 'verdaccioTargets') - getEnvTargetsSpy = vi.spyOn(environmentTargetsModule, 'getEnvTargets') - updateEnvTargetNamesSpy = vi.spyOn(environmentTargetsModule, 'updateEnvTargetNames') + verdaccioTargetsSpy = vi.spyOn( + environmentTargetsModule, + 'verdaccioTargets' + ); + getEnvTargetsSpy = vi.spyOn(environmentTargetsModule, 'getEnvTargets'); + updateEnvTargetNamesSpy = vi.spyOn( + environmentTargetsModule, + 'updateEnvTargetNames' + ); }); afterEach((): void => { @@ -152,30 +94,30 @@ describe('createProjectConfiguration', (): void => { updateEnvTargetNamesSpy.mockRestore(); }); - it('should call normalizeCreateNodesOptions ones with config and options', (): void => { - createProjectConfiguration(config, options); + it('should call normalizeCreateNodesOptions ones with projectConfiguration and options', (): void => { + createProjectConfiguration(projectConfiguration, options); expect( - normalizeCreateNodesModule.normalizeCreateNodesOptions + normalizeCreateNodesSpyModule.normalizeCreateNodesOptions ).toHaveBeenCalledOnce(); expect( - normalizeCreateNodesModule.normalizeCreateNodesOptions + normalizeCreateNodesSpyModule.normalizeCreateNodesOptions ).toHaveBeenCalledWith(options); }); it('should call isEnvProject ones with projectConfiguration and environments', (): void => { - createProjectConfiguration(config, options); + createProjectConfiguration(projectConfiguration, options); expect(environmentTargetsModule.isEnvProject).toHaveBeenCalledOnce(); expect(environmentTargetsModule.isEnvProject).toHaveBeenCalledWith( - config, + projectConfiguration, normalizedOptions['environments'] ); }); it('should call isPublishableProject ones with projectConfiguration and packages', (): void => { - createProjectConfiguration(config, options); - expect(packageTargetsModule.isPkgProject).toHaveBeenCalledOnce(); - expect(packageTargetsModule.isPkgProject).toHaveBeenCalledWith( - config, + createProjectConfiguration(projectConfiguration, options); + expect(packageTargetsSpyModule.isPkgProject).toHaveBeenCalledOnce(); + expect(packageTargetsSpyModule.isPkgProject).toHaveBeenCalledWith( + projectConfiguration, normalizedOptions['packages'] ); }); @@ -183,42 +125,42 @@ describe('createProjectConfiguration', (): void => { it('should return empty object if !isE2eProject and !isPublishableProject', (): void => { isEnvProjectSpy.mockReturnValue(false); isPkgSpy.mockReturnValue(false); - const projectConfiguration = createProjectConfiguration(config, options); - expect(projectConfiguration).toStrictEqual({}); + const result = createProjectConfiguration(projectConfiguration, options); + expect(result).toStrictEqual({}); }); it('should log warn if isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { - createProjectConfiguration(config, options); - expect(nxDevkitModule.logger.warn).toHaveBeenCalledOnce(); + createProjectConfiguration(projectConfiguration, options); + expect(nxDevkitMockModule.logger.warn).toHaveBeenCalledOnce(); }); it('should not log warn if isE2eProject and projectConfiguration.implicitDependencies?.length', (): void => { const configWithImplicitDependencies = { - ...config, - implicitDependencies: ['mock-implicit-dep'], + ...projectConfiguration, + implicitDependencies, }; createProjectConfiguration(configWithImplicitDependencies, options); - expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); + expect(nxDevkitMockModule.logger.warn).toHaveBeenCalledTimes(0); }); it('should not log warn if !isE2eProject and projectConfiguration.implicitDependencies?.length', (): void => { isEnvProjectSpy.mockReturnValue(false); const configWithImplicitDependencies = { - ...config, - implicitDependencies: ['mock-implicit-dep'], + ...projectConfiguration, + implicitDependencies, }; createProjectConfiguration(configWithImplicitDependencies, options); - expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); + expect(nxDevkitMockModule.logger.warn).toHaveBeenCalledTimes(0); }); it('should not log warn if !isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { isEnvProjectSpy.mockReturnValue(false); - createProjectConfiguration(config, options); - expect(nxDevkitModule.logger.warn).toHaveBeenCalledTimes(0); + createProjectConfiguration(projectConfiguration, options); + expect(nxDevkitMockModule.logger.warn).toHaveBeenCalledTimes(0); }); it('should generate project configuration with namedInputs and targets if isE2eProject and isPublishableProject', (): void => { - const result = createProjectConfiguration(config, options); + const result = createProjectConfiguration(projectConfiguration, options); expect(result).toMatchObject({ namedInputs: expect.any(Object), targets: expect.any(Object), @@ -226,7 +168,7 @@ describe('createProjectConfiguration', (): void => { }); it('should generate project configuration with targets if !isE2eProject and isPublishableProject', (): void => { - const result = createProjectConfiguration(config, options); + const result = createProjectConfiguration(projectConfiguration, options); expect(result).toMatchObject({ targets: expect.any(Object), }); @@ -234,9 +176,9 @@ describe('createProjectConfiguration', (): void => { it('should generate targets with correct structure if isE2eProject and !isPublishableProject', (): void => { isPkgSpy.mockReturnValue(false); - const result = createProjectConfiguration(config, options); - expect(result['targets']).toMatchObject( - {build: expect.any(Object), + const result = createProjectConfiguration(projectConfiguration, options); + expect(result['targets']).toMatchObject({ + build: expect.any(Object), [TARGET_ENVIRONMENT_VERDACCIO_START]: expect.any(Object), [TARGET_ENVIRONMENT_VERDACCIO_STOP]: expect.any(Object), [TARGET_ENVIRONMENT_BOOTSTRAP]: expect.any(Object), @@ -245,45 +187,45 @@ describe('createProjectConfiguration', (): void => { [TARGET_ENVIRONMENT_SETUP]: expect.any(Object), [TARGET_ENVIRONMENT_TEARDOWN]: expect.any(Object), [TARGET_ENVIRONMENT_E2E]: expect.any(Object), - } - ); + }); }); it('should generate nameInputs with correct structure and data', (): void => { - const result = createProjectConfiguration(config, options); + const result = createProjectConfiguration(projectConfiguration, options); expect(result).toMatchObject({ namedInputs: expect.any(Object), - targets: - { - [TARGET_PACKAGE_PUBLISH]: expect.any(Object), - [TARGET_PACKAGE_INSTALL]: expect.any(Object), - } + targets: { + [TARGET_PACKAGE_PUBLISH]: expect.any(Object), + [TARGET_PACKAGE_INSTALL]: expect.any(Object), + }, }); }); it('should call verdaccioTargets ones with correct arguments', (): void => { - createProjectConfiguration(config, options); + createProjectConfiguration(projectConfiguration, options); expect(environmentTargetsModule.verdaccioTargets).toHaveBeenCalledOnce(); expect(environmentTargetsModule.verdaccioTargets).toHaveBeenCalledWith( - config, + projectConfiguration, { environmentsDir: normalizedOptions.environments.environmentsDir } ); }); it('should call getEnvTargets ones with correct arguments', (): void => { - createProjectConfiguration(config, options); + createProjectConfiguration(projectConfiguration, options); expect(environmentTargetsModule.getEnvTargets).toHaveBeenCalledOnce(); expect(environmentTargetsModule.getEnvTargets).toHaveBeenCalledWith( - config, + projectConfiguration, normalizedOptions.environments ); }); it('should call updateEnvTargetNames ones with correct arguments', (): void => { - createProjectConfiguration(config, options); - expect(environmentTargetsModule.updateEnvTargetNames).toHaveBeenCalledOnce(); + createProjectConfiguration(projectConfiguration, options); + expect( + environmentTargetsModule.updateEnvTargetNames + ).toHaveBeenCalledOnce(); expect(environmentTargetsModule.updateEnvTargetNames).toHaveBeenCalledWith( - config, + projectConfiguration, normalizedOptions.environments ); }); From d9a12d0935f589c29f9e4e32db2279ee9a17d2a8 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Thu, 9 Jan 2025 17:10:23 +0100 Subject: [PATCH 14/20] tests: getPkgTargets object structure --- .../targets/package.targets.unit-test.ts | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts index d9903da..2741d42 100644 --- a/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts @@ -1,5 +1,14 @@ import { describe, it, expect } from 'vitest'; -import { isPkgProject } from './package.targets'; + +import { + getPkgTargets, + isPkgProject, + TARGET_PACKAGE_INSTALL, + TARGET_PACKAGE_PUBLISH, +} from './package.targets'; + +import { EXECUTOR_PACKAGE_NPM_INSTALL } from '../../executors/pkg-install/constants'; +import { EXECUTOR_PACKAGE_NPM_PUBLISH } from '../../executors/pkg-publish/constants'; describe('isPkgProject', () => { it('should return true for projects with projectType is library', () => { @@ -58,3 +67,31 @@ describe('isPkgProject', () => { ).toBe(false); }); }); + +describe('getPkgTargets', (): void => { + it('should generate PkgTargets with correct structure', (): void => { + const result = getPkgTargets(); + expect(result).toMatchObject({ + [TARGET_PACKAGE_PUBLISH]: expect.any(Object), + [TARGET_PACKAGE_INSTALL]: expect.any(Object), + }); + }); + + it('should generate TARGET_PACKAGE_PUBLISH with correct structure', (): void => { + const result = getPkgTargets(); + expect(result[TARGET_PACKAGE_PUBLISH]).toMatchObject({ + dependsOn: expect.any(Array), + executor: expect.stringContaining(EXECUTOR_PACKAGE_NPM_PUBLISH), + options: expect.any(Object), + }); + }); + + it('should generate TARGET_PACKAGE_INSTALL with correct structure', (): void => { + const result = getPkgTargets(); + expect(result[TARGET_PACKAGE_INSTALL]).toMatchObject({ + dependsOn: expect.any(Array), + executor: expect.stringContaining(EXECUTOR_PACKAGE_NPM_INSTALL), + options: expect.any(Object), + }); + }); +}); From 8da9a7031005b6096a98fa3dcbf27af050228d26 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 10 Jan 2025 09:24:04 +0100 Subject: [PATCH 15/20] docs: add createProjectConfiguration --- .../src/plugin/targets/create-targets.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.ts index d5ae71e..3bf0987 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.ts @@ -9,6 +9,24 @@ import { } from './environment.targets'; import { getPkgTargets, isPkgProject } from './package.targets'; +/** + * Generates a project configuration with targets and namedInputs. + * + * If the project is an environment project - `isEnvProject`(), + * wraps the results of `verdaccioTargets()`, `getEnvTargets()`, + *`updateEnvTargetNames()`, and namedInputs + * + * If the project is a publishable project - `isPkgProject()`, + * wraps the results of `getPkgTargets()`, and namedInputs + * + * Otherwise, returns and empty object (early return) + * + * Logs warnings for missing implicit dependencies in environment projects. + * + * @param projectConfiguration + * @param options + * @returns A partial project configuration with targets and namedInputs. + */ export function createProjectConfiguration( projectConfiguration: ProjectConfiguration, options: NxVerdaccioCreateNodeOptions From b8a082dbedc9f31001767b52a09ad72babcccad2 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 10 Jan 2025 09:24:53 +0100 Subject: [PATCH 16/20] docs: createProjectConfiguration docs formatting --- projects/nx-verdaccio/src/plugin/targets/create-targets.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.ts index 3bf0987..f81d55a 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.ts @@ -47,8 +47,10 @@ export function createProjectConfiguration( /** * Unfortunately namedInputs are not picked up by tasks graph: Error: Input 'build-artifacts' is not defined - * When you pass your own namedInputs (like you would in a project.json file) via the inferred tasks plugin, the tasks pipeline ignores them and throws this error. - * Some Nx plugins use the default namedInput, probably for that reason, but I'm concerned that if developers change those inputs, it might lead to undesired behavior. + * When you pass your own namedInputs (like you would in a project.json file) via the inferred tasks plugin, + * the tasks pipeline ignores them and throws this error. + * Some Nx plugins use the default namedInput, probably for that reason, + * but I'm concerned that if developers change those inputs, it might lead to undesired behavior. * @todo investigate if there is a way to pass namedInputs to the tasks graph */ const namedInputs: ProjectConfiguration['namedInputs'] = { From 25501d37a860a36ce6c1bc7c11c69694bd3f93f9 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 10 Jan 2025 09:31:41 +0100 Subject: [PATCH 17/20] docs: add isPkgProject --- .../nx-verdaccio/src/plugin/targets/package.targets.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/projects/nx-verdaccio/src/plugin/targets/package.targets.ts b/projects/nx-verdaccio/src/plugin/targets/package.targets.ts index dc6047c..6bdd1eb 100644 --- a/projects/nx-verdaccio/src/plugin/targets/package.targets.ts +++ b/projects/nx-verdaccio/src/plugin/targets/package.targets.ts @@ -7,6 +7,16 @@ import { EXECUTOR_PACKAGE_NPM_INSTALL } from '../../executors/pkg-install/consta export const TARGET_PACKAGE_INSTALL = 'nxv-pkg-install'; export const TARGET_PACKAGE_PUBLISH = 'nxv-pkg-publish'; + +/** + * Determines if the given project is a publishable project. + * A project qualifies as a publishable if it's of type 'library' (early return if it's not) + * and, when options tag filters are provided, and they have match with any config tag. + * + * @param projectConfig + * @param options + * @returns `true` if the project is a publishable; otherwise, `false`. + */ export function isPkgProject( projectConfig: ProjectConfiguration, options: NormalizedCreateNodeOptions['packages'] From b00c0cd3f4170a0b81d53ae8c32b7d50f7b81055 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 10 Jan 2025 09:35:36 +0100 Subject: [PATCH 18/20] docs: add getPkgTargets --- .../nx-verdaccio/src/plugin/targets/package.targets.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/projects/nx-verdaccio/src/plugin/targets/package.targets.ts b/projects/nx-verdaccio/src/plugin/targets/package.targets.ts index 6bdd1eb..1f1f561 100644 --- a/projects/nx-verdaccio/src/plugin/targets/package.targets.ts +++ b/projects/nx-verdaccio/src/plugin/targets/package.targets.ts @@ -39,6 +39,13 @@ export function isPkgProject( return true; } +/** + * Creates package-related targets for build pipelines. + * Includes `TARGET_PACKAGE_PUBLISH` and `TARGET_PACKAGE_INSTALL` targets, + * each with targets they depend on. + * + * @returns A record of package targets with their configurations. + */ export function getPkgTargets(): Record { return { [TARGET_PACKAGE_PUBLISH]: { From 1e3d7d3a89efc528aa14c9ab2381bf475989241f Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 10 Jan 2025 09:48:00 +0100 Subject: [PATCH 19/20] self-review: test names polish, add missing structure test --- .../targets/create-targets.unit-test.ts | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts index 4274a62..9f68e1b 100644 --- a/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/create-targets.unit-test.ts @@ -129,12 +129,12 @@ describe('createProjectConfiguration', (): void => { expect(result).toStrictEqual({}); }); - it('should log warn if isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { + it('should log warn if isE2eProject and implicitDependencies are empty', (): void => { createProjectConfiguration(projectConfiguration, options); expect(nxDevkitMockModule.logger.warn).toHaveBeenCalledOnce(); }); - it('should not log warn if isE2eProject and projectConfiguration.implicitDependencies?.length', (): void => { + it('should not log warn if isE2eProject and implicitDependencies are not empty', (): void => { const configWithImplicitDependencies = { ...projectConfiguration, implicitDependencies, @@ -143,7 +143,7 @@ describe('createProjectConfiguration', (): void => { expect(nxDevkitMockModule.logger.warn).toHaveBeenCalledTimes(0); }); - it('should not log warn if !isE2eProject and projectConfiguration.implicitDependencies?.length', (): void => { + it('should not log warn if !isE2eProject and implicitDependencies are not empty', (): void => { isEnvProjectSpy.mockReturnValue(false); const configWithImplicitDependencies = { ...projectConfiguration, @@ -153,7 +153,7 @@ describe('createProjectConfiguration', (): void => { expect(nxDevkitMockModule.logger.warn).toHaveBeenCalledTimes(0); }); - it('should not log warn if !isE2eProject and !projectConfiguration.implicitDependencies?.length', (): void => { + it('should not log warn if !isE2eProject and implicitDependencies are empty', (): void => { isEnvProjectSpy.mockReturnValue(false); createProjectConfiguration(projectConfiguration, options); expect(nxDevkitMockModule.logger.warn).toHaveBeenCalledTimes(0); @@ -174,6 +174,23 @@ describe('createProjectConfiguration', (): void => { }); }); + it('should generate configuration with correct structure if !isE2eProject and isPublishableProject', (): void => { + const result = createProjectConfiguration(projectConfiguration, options); + expect(result).toMatchObject({ + namedInputs: expect.any(Object), + targets: expect.any(Object), + }); + }); + + it('should generate configuration with correct structure if isE2eProject and !isPublishableProject', (): void => { + isPkgSpy.mockReturnValue(false); + const result = createProjectConfiguration(projectConfiguration, options); + expect(result).toMatchObject({ + namedInputs: expect.any(Object), + targets: expect.any(Object), + }); + }); + it('should generate targets with correct structure if isE2eProject and !isPublishableProject', (): void => { isPkgSpy.mockReturnValue(false); const result = createProjectConfiguration(projectConfiguration, options); @@ -190,14 +207,11 @@ describe('createProjectConfiguration', (): void => { }); }); - it('should generate nameInputs with correct structure and data', (): void => { + it('should generate targets with correct structure if !isE2eProject and isPublishableProject', (): void => { const result = createProjectConfiguration(projectConfiguration, options); - expect(result).toMatchObject({ - namedInputs: expect.any(Object), - targets: { - [TARGET_PACKAGE_PUBLISH]: expect.any(Object), - [TARGET_PACKAGE_INSTALL]: expect.any(Object), - }, + expect(result['targets']).toMatchObject({ + [TARGET_PACKAGE_PUBLISH]: expect.any(Object), + [TARGET_PACKAGE_INSTALL]: expect.any(Object), }); }); From 164cf8fa0b57a6febe5fe85f5c39d5ee37e37f96 Mon Sep 17 00:00:00 2001 From: adrianromanski Date: Fri, 10 Jan 2025 09:53:03 +0100 Subject: [PATCH 20/20] self-review: add regex for executor --- .../src/plugin/targets/package.targets.unit-test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts b/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts index 2741d42..4571a26 100644 --- a/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/targets/package.targets.unit-test.ts @@ -81,7 +81,7 @@ describe('getPkgTargets', (): void => { const result = getPkgTargets(); expect(result[TARGET_PACKAGE_PUBLISH]).toMatchObject({ dependsOn: expect.any(Array), - executor: expect.stringContaining(EXECUTOR_PACKAGE_NPM_PUBLISH), + executor: expect.stringMatching(new RegExp(`.+:${EXECUTOR_PACKAGE_NPM_PUBLISH}`)), options: expect.any(Object), }); }); @@ -90,7 +90,7 @@ describe('getPkgTargets', (): void => { const result = getPkgTargets(); expect(result[TARGET_PACKAGE_INSTALL]).toMatchObject({ dependsOn: expect.any(Array), - executor: expect.stringContaining(EXECUTOR_PACKAGE_NPM_INSTALL), + executor: expect.stringMatching(new RegExp(`.+:${EXECUTOR_PACKAGE_NPM_INSTALL}`)), options: expect.any(Object), }); });