-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): add tests for ng cli adapter methods (#16940)
- Loading branch information
Showing
3 changed files
with
160 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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";`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a29afe9
There was a problem hiding this comment.
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