英文:
Cypress version 12 component test not working due to service dependency issues
问题
I'm using Angular version 15 with Cypress version 12.
我正在使用Angular版本15和Cypress版本12。
I've made a basic example component in order to test the new Cypress component testing.
我已经创建了一个基本的示例组件,以便测试新的Cypress组件测试功能。
But I seem to have issues as soon as I import services e.g. utilityService.
但似乎一旦我导入服务,例如utilityService,就会出现问题。
My component I'm testing:
我正在测试的组件:
import { Component } from '@angular/core';
import { UtilityService } from '../../services';
@Component({
selector: 'example-view',
templateUrl: './example.component.html',
})
export class ExampleComponent {
constructor(
utilityService: UtilityService,
) { }
}
My component html:
我的组件HTML:
Example Component
My Cypress component test:
我的Cypress组件测试:
import { ExampleComponent } from './example.component';
import { UtilityService } from '../../services';
describe('Example component', () => {
it('should mount the component', () => {
const utilityService = new UtilityService();
cy.mount(ExampleComponent, {
componentProperties: {
utilityService,
},
});
cy.get('h2').should('exist');
cy.get('h2').should('contain', 'Example Component');
})
})
My configuration setup:
我的配置设置:
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
chromeWebSecurity: false,
screenshotsFolder: "cypress/snapshots",
trashAssetsBeforeRuns: true,
viewportWidth: 1400,
viewportHeight: 1200,
video: false,
env: {
local: "http://localhost:4200/",
staging: "hidden",
user: {
email: "hidden",
password: "hidden",
},
},
component: {
devServer: {
framework: "angular",
bundler: "webpack",
},
specPattern: "**/*.cy.ts",
},
});
UtilityService:
UtilityService:
import { Injectable } from '@angular/core';
import { ReplaySubject, BehaviorSubject } from 'rxjs';
import { ErrorModel } from '../models';
@Injectable()
export class UtilityService {
private sideMenu: BehaviorSubject<boolean | null> = new BehaviorSubject(false);
public sideMenu$ = this.sideMenu.asObservable();
constructor() { }
sideMenuToggle(isVisible: boolean): void {
this.sideMenu.next(isVisible);
}
sortData(dataSource, propertyToSortOn: any, event: any): Array
return [
...dataSource.sort((a: any, b: any) => {
if (a[propertyToSortOn] < b[propertyToSortOn]) {
return event === 'ASC' ? -1 : 1;
}
if (a[propertyToSortOn] > b[propertyToSortOn]) {
return event === 'DESC' ? -1 : 1;
}
return 0;
}),
];
}
}
Error:
错误:
Not sure why I'm getting a storageService error in the stack trace? The utilityService dependency don't seem linked.
不确定为什么在堆栈跟踪中出现了storageService错误?utilityService依赖似乎没有链接。
Also, let me know please if I can mock the services because I'd hate to have to import lots of services like with unit tests.
另外,请告诉我是否可以模拟这些服务,因为我不想像单元测试那样导入大量的服务。
Note: the test works fine if I remove the utilityService from both the component and component test.
注意:如果我从组件和组件测试中移除utilityService,测试就能正常工作。
Stack trace:
堆栈跟踪:
at Module.StorageService (http://localhost:8080/__cypress/src/spec-0.js:92860:116)
at 49525 (webpack://angular/./src/app/services/http/certificate.http.ts:10:62)
at webpack_require (http://localhost:8080/__cypress/src/runtime.js:23:42)
at 21866 (http://localhost:8080/__cypress/src/spec-0.js:92870:80)
at webpack_require (http://localhost:8080/__cypress/src/runtime.js:23:42)
at 12110 (http://localhost:8080/__cypress/src/spec-0.js:89187:67)
at webpack_require (http://localhost:8080/__cypress/src/runtime.js:23:42)
at 96239 (http://localhost:8080/__cypress/src/spec-0.js:89151:76)
at Function.webpack_require (http://localhost:8080/__cypress/src/runtime.js:23:42)
at _ZoneDelegate.invoke (http://localhost:8080/__cypress/src/polyfills.js:5341:158)
From previous event:
at evalScripts (http://localhost:8080/__cypress/runner/cypress_runner.js:168260:58)
at
From previous event:
at runScriptsFromUrls (http://localhost:8080/__cypress/runner/cypress_runner.js:168269:136)
at Object.runScripts (http://localhost:8080/__cypress/runner/cypress_runner.js:168283:12)
at $Cypress.onSpecWindow (http://localhost:8080/__cypress/runner/cypress_runner.js:156917:75)
at init (http://localhost:8080/__cypress/src/cypress-entry.js:66:11)
at 48839 (http://localhost:8080/__cypress/src/cypress-entry.js:38:3)
at webpack_require (http://localhost:8080/__cypress/src/runtime.js:23:42)
at render (http://localhost:8080/__cypress/src/cypress-entry.js:84:3)
at 42795 (http://localhost:8080/__cypress/src/cypress-entry.js:87:1)
at webpack_require (http://localhost:8080/__cypress/src/runtime.js:23:42)
at webpack_exec (http://localhost:8080/__cypress/src/cypress-entry.js:7783:48)
at
at webpackJsonpCallback (http://localhost:8080/__cypress/src/runtime.js:312:39)
at
英文:
I'm using Angular version 15 with Cypress version 12.
I've made a basic example component in order to test the new Cypress component testing.
But I seem to have issues as soon as I import services e.g. utilityService.
My component I'm testing:
import { Component } from '@angular/core';
import { UtilityService } from '../../services';
@Component({
selector: 'example-view',
templateUrl: './example.component.html',
})
export class ExampleComponent {
constructor(
utilityService: UtilityService,
) { }
}
My component html:
<h2>Example Component</h2>
My Cypress component test:
import { ExampleComponent } from './example.component'
import { UtilityService } from '../../services';
describe('Example component', () => {
it('should mount the component', () => {
const utilityService = new UtilityService();
cy.mount(ExampleComponent, {
componentProperties: {
utilityService,
},
});
cy.get('h2').should('exist');
cy.get('h2').should('contain', 'Example Component');
})
})
My configuration setup:
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
chromeWebSecurity: false,
screenshotsFolder: "cypress/snapshots",
trashAssetsBeforeRuns: true,
viewportWidth: 1400,
viewportHeight: 1200,
video: false,
env: {
local: "http://localhost:4200/",
staging: "hidden",
user: {
email: "hidden",
password: "hidden",
},
},
component: {
devServer: {
framework: "angular",
bundler: "webpack",
},
specPattern: "**/*.cy.ts",
},
});
UtilityService:
import { Injectable } from '@angular/core';
import { ReplaySubject, BehaviorSubject } from 'rxjs';
import { ErrorModel } from '../models';
@Injectable()
export class UtilityService {
private sideMenu: BehaviorSubject<boolean | null> = new BehaviorSubject(false);
public sideMenu$ = this.sideMenu.asObservable();
constructor() { }
sideMenuToggle(isVisible: boolean): void {
this.sideMenu.next(isVisible);
}
sortData(dataSource, propertyToSortOn: any, event: any): Array<any> {
return [
...dataSource.sort((a: any, b: any) => {
if (a[propertyToSortOn] < b[propertyToSortOn]) {
return event === 'ASC' ? -1 : 1;
}
if (a[propertyToSortOn] > b[propertyToSortOn]) {
return event === 'DESC' ? -1 : 1;
}
return 0;
}),
];
}
}
Error:
Not sure why I'm getting a storageService error in the stack trace?
The utilityService dependency don't seem linked.
Also, let me know please if I can mock the services because I'd hate to have to import lots of services like with unit tests.
Note: the test works fine if I remove the utilityService from both the component and component test.
Stack trace:
at Module.StorageService (http://localhost:8080/__cypress/src/spec-0.js:92860:116)
at 49525 (webpack://angular/./src/app/services/http/certificate.http.ts:10:62)
at __webpack_require__ (http://localhost:8080/__cypress/src/runtime.js:23:42)
at 21866 (http://localhost:8080/__cypress/src/spec-0.js:92870:80)
at __webpack_require__ (http://localhost:8080/__cypress/src/runtime.js:23:42)
at 12110 (http://localhost:8080/__cypress/src/spec-0.js:89187:67)
at __webpack_require__ (http://localhost:8080/__cypress/src/runtime.js:23:42)
at 96239 (http://localhost:8080/__cypress/src/spec-0.js:89151:76)
at Function.__webpack_require__ (http://localhost:8080/__cypress/src/runtime.js:23:42)
at _ZoneDelegate.invoke (http://localhost:8080/__cypress/src/polyfills.js:5341:158)
From previous event:
at evalScripts (http://localhost:8080/__cypress/runner/cypress_runner.js:168260:58)
at <unknown> (http://localhost:8080/__cypress/runner/cypress_runner.js:168269:152)
From previous event:
at runScriptsFromUrls (http://localhost:8080/__cypress/runner/cypress_runner.js:168269:136)
at Object.runScripts (http://localhost:8080/__cypress/runner/cypress_runner.js:168283:12)
at $Cypress.onSpecWindow (http://localhost:8080/__cypress/runner/cypress_runner.js:156917:75)
at init (http://localhost:8080/__cypress/src/cypress-entry.js:66:11)
at 48839 (http://localhost:8080/__cypress/src/cypress-entry.js:38:3)
at __webpack_require__ (http://localhost:8080/__cypress/src/runtime.js:23:42)
at render (http://localhost:8080/__cypress/src/cypress-entry.js:84:3)
at 42795 (http://localhost:8080/__cypress/src/cypress-entry.js:87:1)
at __webpack_require__ (http://localhost:8080/__cypress/src/runtime.js:23:42)
at __webpack_exec__ (http://localhost:8080/__cypress/src/cypress-entry.js:7783:48)
at <unknown> (http://localhost:8080/__cypress/src/cypress-entry.js:7784:62)
at webpackJsonpCallback (http://localhost:8080/__cypress/src/runtime.js:312:39)
at <unknown> (http://localhost:8080/__cypress/src/cypress-entry.js:1:67)
答案1
得分: 0
这是因为我在使用一个索引文件导入服务。
解决方法:
import { UtilityService } from '../../services/utility.service';
而不是这样:
import { UtilityService } from '../../services/';
现在我不确定是否可以通过更改配置来使用索引文件的方法。
很奇怪,因为服务文件夹中的索引文件正在导出服务,在Angular应用程序运行时正常工作。
英文:
This was because I was importing services using an index file.
Solution:
import { UtilityService } from '../../services/utility.service';
Instead of this:
import { UtilityService } from '../../services';
Now I'm not fully sure if its possible to use the index file approach by changing the config.
Its weird because the index file in services folder is exporting the service and works fine when the Angular app runs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论