diff --git a/packages/devkit/src/utils/convert-nx-executor.spec.ts b/packages/devkit/src/utils/convert-nx-executor.spec.ts new file mode 100644 index 0000000000000..f4240aa0dbd8b --- /dev/null +++ b/packages/devkit/src/utils/convert-nx-executor.spec.ts @@ -0,0 +1,100 @@ +import { convertNxExecutor } from './convert-nx-executor'; + +describe('Convert Nx Executor', () => { + it('should convertNxExecutor to builder correctly and produce the same output', async () => { + // ARRANGE + const { schema } = require('@angular-devkit/core'); + const { + TestingArchitectHost, + } = require('@angular-devkit/architect/testing'); + const { Architect } = require('@angular-devkit/architect'); + + const registry = new schema.CoreSchemaRegistry(); + registry.addPostTransform(schema.transforms.addUndefinedDefaults); + const testArchitectHost = new TestingArchitectHost(); + const architect = new Architect(testArchitectHost, registry); + + const convertedExecutor = convertNxExecutor(echoExecutor); + const realBuilder = require('@angular-devkit/architect').createBuilder( + echo + ); + + testArchitectHost.addBuilder('nx:test', convertedExecutor); + testArchitectHost.addBuilder('ng:test', realBuilder); + + const consoleSpy = jest.spyOn(console, 'log'); + + // ACT + const convertedRun = await architect.scheduleBuilder('nx:test', { + name: 'test', + }); + const realRun = await architect.scheduleBuilder('ng:test', { + name: 'test', + }); + + const convertedRunResult = await convertedRun.result; + const realRunResult = await realRun.result; + + // ASSERT + expect(convertedRunResult).toMatchInlineSnapshot(` + { + "error": undefined, + "info": { + "builderName": "nx:test", + "description": "Testing only builder.", + "optionSchema": { + "type": "object", + }, + }, + "success": true, + "target": { + "configuration": undefined, + "project": undefined, + "target": undefined, + }, + } + `); + expect(realRunResult).toMatchInlineSnapshot(` + { + "error": undefined, + "info": { + "builderName": "ng:test", + "description": "Testing only builder.", + "optionSchema": { + "type": "object", + }, + }, + "success": true, + "target": { + "configuration": undefined, + "project": undefined, + "target": undefined, + }, + } + `); + expect(convertedRunResult.success).toEqual(realRunResult.success); + expect(consoleSpy).toHaveBeenCalledTimes(2); + expect(consoleSpy).toHaveBeenNthCalledWith(1, 'Executor ran', { + name: 'test', + }); + expect(consoleSpy).toHaveBeenNthCalledWith(2, 'Executor ran', { + name: 'test', + }); + + expect(convertedExecutor.toString()).toEqual(realBuilder.toString()); + expect(convertedExecutor.handler.toString()).toEqual( + realBuilder.handler.toString() + ); + }); +}); + +function echo(options: { name: string }) { + console.log('Executor ran', options); + return { + success: true, + }; +} + +async function echoExecutor(options: { name: string }) { + return echo(options); +} diff --git a/packages/devkit/src/utils/invoke-nx-generator.spec.ts b/packages/devkit/src/utils/invoke-nx-generator.spec.ts new file mode 100644 index 0000000000000..36188423ae642 --- /dev/null +++ b/packages/devkit/src/utils/invoke-nx-generator.spec.ts @@ -0,0 +1,36 @@ +import type { Tree } from 'nx/src/generators/tree'; +import { convertNxGenerator } from './invoke-nx-generator'; +import { lastValueFrom } from 'rxjs'; + +describe('Convert Nx Generator', () => { + it('should convert an nx generator to angular schematic correctly', async () => { + // ARRANGE + const { + SchematicTestRunner, + UnitTestTree, + } = require('@angular-devkit/schematics/testing'); + const ngSchematicRunner = new SchematicTestRunner( + '@schematics/angular', + require.resolve('@schematics/angular/collection.json') + ); + + const appTree = await ngSchematicRunner.runSchematic('workspace', { + name: 'workspace', + newProjectRoot: 'projects', + version: '6.0.0', + }); + + // ACT + const convertedGenerator = convertNxGenerator(newFileGenerator); + const tree: typeof UnitTestTree = await lastValueFrom( + ngSchematicRunner.callRule(convertedGenerator, appTree) + ); + + // ASSERT + expect(tree.files).toContain(`/my-file.ts`); + }); +}); + +async function newFileGenerator(tree: Tree, options: {}) { + tree.write('my-file.ts', `const hello = "hello world";`); +} diff --git a/packages/nx/src/adapter/ngcli-adapter.spec.ts b/packages/nx/src/adapter/ngcli-adapter.spec.ts index ef66325da024b..cb055967a3ead 100644 --- a/packages/nx/src/adapter/ngcli-adapter.spec.ts +++ b/packages/nx/src/adapter/ngcli-adapter.spec.ts @@ -1,4 +1,9 @@ -import { arrayBufferToString } from './ngcli-adapter'; +import { + arrayBufferToString, + wrapAngularDevkitSchematic, +} from './ngcli-adapter'; +import { createTreeWithEmptyWorkspace } from '../generators/testing-utils/create-tree-with-empty-workspace'; +import { addProjectConfiguration } from '../generators/utils/project-configuration'; describe('ngcli-adapter', () => { it('arrayBufferToString should support large buffers', () => { @@ -8,4 +13,22 @@ describe('ngcli-adapter', () => { expect(result).toBe(largeString); }); + + it('should correctly wrapAngularDevkitSchematics', async () => { + // ARRANGE + const tree = createTreeWithEmptyWorkspace(); + + addProjectConfiguration(tree, 'test', { root: '', sourceRoot: 'src' }); + + const wrappedSchematic = wrapAngularDevkitSchematic( + '@schematics/angular', + 'class' + ); + + // ACT + await wrappedSchematic(tree, { name: 'test', project: 'test' }); + + // ASSERT + expect(tree.exists('src/lib/test.ts')).toBeTruthy(); + }); });