-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Angular CT Schematic #24374
Changes from 20 commits
c750eea
da0705f
75da5c7
962322a
e581253
8bfcf38
03a79d3
c55b0c2
d519375
66e3d7c
3fd56bc
2ce0792
f1a0794
af415e0
058cf22
60e4ada
91af0c9
5537c82
a82f793
7beb9f1
dffdd34
4e8d7d1
76606cd
51f28a5
cd3b253
dd2cf7a
1d40c3f
4fb5b57
1c93b03
d9fe5ef
36d41db
c3f2d2a
4ab75a6
a3c3e6f
97401f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,7 @@ export default function (_options: any): Rule { | |
addCtSpecs(_options), | ||
addCypressTestScriptsToPackageJson(), | ||
modifyAngularJson(_options), | ||
addDefaultSchematic(), | ||
])(tree, _context) | ||
} | ||
} | ||
|
@@ -306,6 +307,33 @@ function modifyAngularJson (options: any): Rule { | |
} | ||
} | ||
|
||
function addDefaultSchematic (): Rule { | ||
return (tree: Tree, _: SchematicContext) => { | ||
if (tree.exists('./angular.json')) { | ||
const angularJsonVal = getAngularJsonValue(tree) | ||
const angularSchematic = '@schematics/angular' | ||
const cli = { | ||
...angularJsonVal.cli, | ||
schematicCollections: ['@cypress/schematic', ...angularJsonVal?.cli?.schematicCollections ?? []], | ||
} | ||
|
||
if (cli.schematicCollections.indexOf('@schematics/angular') === -1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
cli.schematicCollections.push(angularSchematic) | ||
} | ||
|
||
return tree.overwrite( | ||
'./angular.json', | ||
JSON.stringify({ | ||
...angularJsonVal, | ||
cli, | ||
}, null, 2), | ||
) | ||
} | ||
|
||
throw new SchematicsException('angular.json not found') | ||
} | ||
} | ||
|
||
export const getCypressConfigFile = (angularJsonVal: any, projectName: string) => { | ||
const project = angularJsonVal.projects[projectName] | ||
const tsConfig = project?.architect?.lint?.options?.tsConfig | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing' | ||
import { expect } from 'chai' | ||
import { join } from 'path' | ||
|
||
describe('ng-generate @cypress/schematic:component', () => { | ||
const schematicRunner = new SchematicTestRunner( | ||
'schematics', | ||
join(__dirname, '../../collection.json'), | ||
) | ||
let appTree: UnitTestTree | ||
|
||
const workspaceOptions = { | ||
name: 'workspace', | ||
newProjectRoot: 'projects', | ||
version: '12.0.0', | ||
} | ||
|
||
const appOptions: Parameters<typeof schematicRunner['runExternalSchematicAsync']>[2] = { | ||
name: 'sandbox', | ||
inlineTemplate: false, | ||
routing: false, | ||
skipTests: false, | ||
skipPackageJson: false, | ||
} | ||
|
||
beforeEach(async () => { | ||
appTree = await schematicRunner.runExternalSchematicAsync('@schematics/angular', 'workspace', workspaceOptions).toPromise() | ||
appTree = await schematicRunner.runExternalSchematicAsync('@schematics/angular', 'application', appOptions, appTree).toPromise() | ||
}) | ||
|
||
it('should create cypress ct alongside the generated component', async () => { | ||
const tree = await schematicRunner.runSchematicAsync('component', { name: 'foo', project: 'sandbox' }, appTree).toPromise() | ||
|
||
expect(tree.files).to.contain('/projects/sandbox/src/app/foo/foo.component.ts') | ||
expect(tree.files).to.contain('/projects/sandbox/src/app/foo/foo.component.html') | ||
expect(tree.files).to.contain('/projects/sandbox/src/app/foo/foo.component.cy.ts') | ||
expect(tree.files).to.contain('/projects/sandbox/src/app/foo/foo.component.css') | ||
expect(tree.files).not.to.contain('/projects/sandbox/src/app/foo/foo.component.spec.ts') | ||
}) | ||
|
||
it('should not generate component which does exist already', async () => { | ||
let tree = await schematicRunner.runSchematicAsync('component', { name: 'foo', project: 'sandbox' }, appTree).toPromise() | ||
|
||
tree = await schematicRunner.runSchematicAsync('component', { name: 'foo', project: 'sandbox' }, appTree).toPromise() | ||
|
||
expect(tree.files.filter((f) => f === '/projects/sandbox/src/app/foo/foo.component.ts').length).to.eq(1) | ||
expect(tree.files.filter((f) => f === '/projects/sandbox/src/app/foo/foo.component.html').length).to.eq(1) | ||
expect(tree.files.filter((f) => f === '/projects/sandbox/src/app/foo/foo.component.cy.ts').length).to.eq(1) | ||
expect(tree.files.filter((f) => f === '/projects/sandbox/src/app/foo/foo.component.css').length).to.eq(1) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { chain, externalSchematic, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics' | ||
import cypressTest from '../cypress-test' | ||
import path = require('path'); | ||
|
||
export default function (options: any): Rule { | ||
return (_: Tree, _context: SchematicContext) => { | ||
return chain([ | ||
externalSchematic('@schematics/angular', 'component', { | ||
...options, | ||
skipTests: true, | ||
}), | ||
(tree: Tree, _context: SchematicContext) => { | ||
const componentPath = tree.actions.filter((a) => a.path.includes(`${options.name}.component.ts`)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic doesn't work when trying to generate a nested component e.g. a = 'components/hello/hello.component.ts'
a.path.includes('components/hello.component.ts') === false Is there a way to get the list of pending changes from the tree? Then we would just have to find a file ending in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I pushed up dd2cf7a which fixes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this is failing on CI, taking a look... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this supposed to generate a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's passing on Angular-14 but not on Angular-13. I believe it is due to a problem with module resolution, similar to what we've seen with system-tests where it is resolving Angular deps from within the monorepo and not the scaffolded project. |
||
.map((a) => path.dirname(a.path)) | ||
.at(0) | ||
|
||
return componentPath ? cypressTest({ | ||
...options, | ||
component: true, | ||
path: componentPath, | ||
}) : noop() | ||
}, | ||
]) | ||
} | ||
} |
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.
Quick q, it is a reasonable assumption that
angular.json
exists in this location? I made a new project with Nx using Angular and they put Cypress tests in a separate project directory to the main App, meaningangular.json
might be somewhere else.cc @jordanpowell88, I think we talked about something similar to this before
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.
@lmiller1990 This generator is for angular CLI projects (which all have an
angular.json
file). In your Nx example they already have this functionality