Skip to content

Commit

Permalink
chore(core): add tests for ng cli adapter methods (#16940)
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 authored May 24, 2023
1 parent 60dda1a commit a29afe9
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 1 deletion.
100 changes: 100 additions & 0 deletions packages/devkit/src/utils/convert-nx-executor.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
}
36 changes: 36 additions & 0 deletions packages/devkit/src/utils/invoke-nx-generator.spec.ts
Original file line number Diff line number Diff line change
@@ -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";`);
}
25 changes: 24 additions & 1 deletion packages/nx/src/adapter/ngcli-adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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();
});
});

1 comment on commit a29afe9

@vercel
Copy link

@vercel vercel bot commented on a29afe9 May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.