-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
c750eea
Merge pull request #23628 from cypress-io/develop
mike-plummer da0705f
chore: release @cypress/angular-v1.1.0
semantic-release-bot 75da5c7
chore: release @cypress/schematic-v2.1.0
semantic-release-bot 962322a
chore: release @cypress/mount-utils-v2.1.0
semantic-release-bot e581253
chore: release @cypress/react-v6.2.0
semantic-release-bot 8bfcf38
chore: release @cypress/react18-v1.1.0
semantic-release-bot 03a79d3
chore: release @cypress/svelte-v1.0.0
semantic-release-bot c55b0c2
chore: release @cypress/vue-v4.2.0
semantic-release-bot d519375
chore: release @cypress/vue2-v1.1.0
semantic-release-bot 66e3d7c
chore: release @cypress/webpack-dev-server-v2.3.0
semantic-release-bot 3fd56bc
fix(cypress-schematic): suffix template files so they are not ignored…
ZachJW34 2ce0792
chore: release @cypress/schematic-v2.1.1
semantic-release-bot f1a0794
fix: Use tsconfig from build if exists (closes #23673) (#23696)
yusijs af415e0
Merge pull request #23818 from cypress-io/develop
emilyrohrbough 058cf22
feat: add support for generating angular component
w0wka91 60e4ada
feat: skip default test generation
w0wka91 91af0c9
feat: generate ct tests only if component was generated
w0wka91 5537c82
feat: add @cypress/schematic to schematicCollections
w0wka91 a82f793
feat: add documentation
w0wka91 7beb9f1
docs: document component generation
w0wka91 dffdd34
add test
w0wka91 4e8d7d1
Merge branch 'develop' into issue-22976
jordanpowell88 76606cd
Merge branch 'develop' into issue-22976
jordanpowell88 51f28a5
Merge branch 'develop' into issue-22976
jordanpowell88 cd3b253
Merge branch 'develop' into issue-22976
marktnoonan dd2cf7a
fix generate from component with dir
ZachJW34 1d40c3f
Merge branch 'develop' into issue-22976
ZachJW34 4fb5b57
fix CI
astone123 1c93b03
add variable to job defaults
astone123 d9fe5ef
merge in develop
lmiller1990 36d41db
Merge branch 'develop' into issue-22976
jordanpowell88 c3f2d2a
Merge branch 'develop' into issue-22976
jordanpowell88 4ab75a6
Merge branch 'develop' into issue-22976
a3c3e6f
remove v13 support
ZachJW34 97401f5
Merge branch 'develop' into issue-22976
ZachJW34 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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
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 |
---|---|---|
|
@@ -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 | ||
|
60 changes: 60 additions & 0 deletions
60
npm/cypress-schematic/src/schematics/ng-generate/component/index.spec.ts
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,60 @@ | ||
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) | ||
}) | ||
|
||
it('should generate component given a component containing a directory', async () => { | ||
const tree = await schematicRunner.runSchematicAsync('component', { name: 'foo/bar', project: 'sandbox' }, appTree).toPromise() | ||
|
||
expect(tree.files).to.contain('/projects/sandbox/src/app/foo/bar/bar.component.ts') | ||
expect(tree.files).to.contain('/projects/sandbox/src/app/foo/bar/bar.component.html') | ||
expect(tree.files).to.contain('/projects/sandbox/src/app/foo/bar/bar.component.cy.ts') | ||
expect(tree.files).to.contain('/projects/sandbox/src/app/foo/bar/bar.component.css') | ||
}) | ||
}) |
27 changes: 27 additions & 0 deletions
27
npm/cypress-schematic/src/schematics/ng-generate/component/index.ts
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,27 @@ | ||
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 componentName = path.parse(options.name).name | ||
const componentPath = tree.actions.filter((a) => a.path.includes(`${componentName}.component.ts`)) | ||
.map((a) => path.dirname(a.path)) | ||
.at(0) | ||
|
||
return componentPath ? cypressTest({ | ||
...options, | ||
component: true, | ||
path: componentPath, | ||
name: componentName, | ||
}) : noop() | ||
}, | ||
]) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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