Skip to content

Commit 2f337db

Browse files
authoredAug 29, 2022
fix: angular 14.2 mount compilation error (#23593)
1 parent bc9edb4 commit 2f337db

File tree

7 files changed

+959
-1719
lines changed

7 files changed

+959
-1719
lines changed
 

‎npm/angular/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
},
1414
"dependencies": {},
1515
"devDependencies": {
16-
"@angular/common": "^14.0.6",
17-
"@angular/core": "^14.0.6",
18-
"@angular/platform-browser-dynamic": "^14.0.6",
16+
"@angular/common": "^14.2.0",
17+
"@angular/core": "^14.2.0",
18+
"@angular/platform-browser-dynamic": "^14.2.0",
1919
"@cypress/mount-utils": "0.0.0-development",
2020
"typescript": "^4.7.4",
2121
"zone.js": "~0.11.4"

‎npm/angular/src/mount.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import { Component, EventEmitter, Type } from '@angular/core'
1212
import {
1313
ComponentFixture,
1414
getTestBed,
15-
TestBed,
1615
TestModuleMetadata,
16+
TestBed,
1717
} from '@angular/core/testing'
1818
import {
1919
BrowserDynamicTestingModule,
@@ -144,12 +144,12 @@ function initTestBed<T> (
144144

145145
const componentFixture = createComponentFixture(component) as Type<T>
146146

147-
TestBed.configureTestingModule({
147+
getTestBed().configureTestingModule({
148148
...bootstrapModule(componentFixture, configRest),
149149
})
150150

151151
if (providers != null) {
152-
TestBed.overrideComponent(componentFixture, {
152+
getTestBed().overrideComponent(componentFixture, {
153153
add: {
154154
providers,
155155
},
@@ -172,6 +172,8 @@ function createComponentFixture<T> (
172172
component: Type<T> | string,
173173
): Type<T | WrapperComponent> {
174174
if (typeof component === 'string') {
175+
// getTestBed().overrideTemplate is available in v14+
176+
// The static TestBed.overrideTemplate is available across versions
175177
TestBed.overrideTemplate(WrapperComponent, component)
176178

177179
return WrapperComponent
@@ -192,7 +194,7 @@ function setupFixture<T> (
192194
component: Type<T>,
193195
config: MountConfig<T>,
194196
): ComponentFixture<T> {
195-
const fixture = TestBed.createComponent(component)
197+
const fixture = getTestBed().createComponent(component)
196198

197199
fixture.whenStable().then(() => {
198200
fixture.autoDetectChanges(config.autoDetectChanges ?? true)
@@ -309,6 +311,6 @@ getTestBed().initTestEnvironment(
309311

310312
setupHooks(() => {
311313
// Not public, we need to call this to remove the last component from the DOM
312-
TestBed['tearDownTestingModule']()
313-
TestBed.resetTestingModule()
314+
getTestBed()['tearDownTestingModule']()
315+
getTestBed().resetTestingModule()
314316
})

‎npm/cypress-schematic/package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
"test": "mocha -r @packages/ts/register --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json src/**/*.spec.ts"
1010
},
1111
"dependencies": {
12-
"@angular-devkit/architect": "^0.1401.0",
13-
"@angular-devkit/core": "^14.1.0",
14-
"@angular-devkit/schematics": "^14.1.0",
15-
"@schematics/angular": "^14.1.0",
12+
"@angular-devkit/architect": "^0.1402.1",
13+
"@angular-devkit/core": "^14.2.1",
14+
"@angular-devkit/schematics": "^14.2.1",
15+
"@schematics/angular": "^14.2.1",
1616
"jsonc-parser": "^3.0.0",
1717
"rxjs": "~6.6.0"
1818
},
1919
"devDependencies": {
20-
"@angular-devkit/schematics-cli": "^14.1.0",
21-
"@angular/cli": "^14.1.0",
20+
"@angular-devkit/schematics-cli": "^14.2.1",
21+
"@angular/cli": "^14.2.1",
2222
"@types/chai-enzyme": "0.6.7",
2323
"@types/mocha": "8.0.3",
2424
"@types/node": "^18.0.6",

‎npm/cypress-schematic/src/ct.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ const runCommandInProject = (command: string, projectPath: string) => {
2222
return execa(ex, args, { cwd: projectPath, stdio: 'inherit' })
2323
}
2424

25+
// Since the schematic downloads a new version of cypress, the latest changes of
26+
// @cypress/angular won't exist in the tmp project. To fix this, we replace the
27+
// contents of the <project-path>/node_modules/cypress/angular with the latest
28+
// contents of cli/angular
29+
const copyAngularMount = async (projectPath: string) => {
30+
await fs.copy(
31+
path.join(__dirname, '..', '..', '..', 'cli', 'angular'),
32+
path.join(projectPath, 'node_modules', 'cypress', 'angular'),
33+
)
34+
}
35+
2536
const cypressSchematicPackagePath = path.join(__dirname, '..')
2637

2738
const ANGULAR_PROJECTS: ProjectFixtureDir[] = ['angular-13', 'angular-14']
@@ -35,6 +46,7 @@ describe('ng add @cypress/schematic / e2e and ct', function () {
3546

3647
await runCommandInProject(`yarn add @cypress/schematic@file:${cypressSchematicPackagePath}`, projectPath)
3748
await runCommandInProject('yarn ng add @cypress/schematic --e2e --component', projectPath)
49+
await copyAngularMount(projectPath)
3850
await runCommandInProject('yarn ng run angular:ct --watch false --spec src/app/app.component.cy.ts', projectPath)
3951
})
4052
}

‎system-tests/projects/angular-14/package.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
"watch": "ng build --watch --configuration development"
1010
},
1111
"dependencies": {
12-
"@angular/animations": "^14.0.0",
13-
"@angular/common": "^14.0.0",
14-
"@angular/compiler": "^14.0.0",
15-
"@angular/core": "^14.0.0",
16-
"@angular/forms": "^14.0.0",
17-
"@angular/platform-browser": "^14.0.0",
18-
"@angular/platform-browser-dynamic": "^14.0.0",
19-
"@angular/router": "^14.0.0",
12+
"@angular/animations": "^14.2.0",
13+
"@angular/common": "^14.2.0",
14+
"@angular/compiler": "^14.2.0",
15+
"@angular/core": "^14.2.0",
16+
"@angular/forms": "^14.2.0",
17+
"@angular/platform-browser": "^14.2.0",
18+
"@angular/platform-browser-dynamic": "^14.2.0",
19+
"@angular/router": "^14.2.0",
2020
"rxjs": "~7.5.0",
2121
"tslib": "^2.3.0",
2222
"zone.js": "~0.11.4"
2323
},
2424
"devDependencies": {
25-
"@angular-devkit/build-angular": "^14.0.0",
26-
"@angular/cli": "~14.0.0",
27-
"@angular/compiler-cli": "^14.0.0",
25+
"@angular-devkit/build-angular": "^14.2.0",
26+
"@angular/cli": "~14.2.0",
27+
"@angular/compiler-cli": "^14.2.0",
2828
"typescript": "~4.7.2"
2929
},
3030
"projectFixtureDirectory": "angular"

0 commit comments

Comments
 (0)