chore(core): add tests for ng cli adapter methods (#16940)

This commit is contained in:
Colum Ferry 2023-05-24 16:32:39 +01:00 committed by GitHub
parent 60dda1af51
commit a29afe98b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 160 additions and 1 deletions

View File

@ -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);
}

View File

@ -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";`);
}

View File

@ -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();
});
});