Cypress版本12组件测试由于服务依赖性问题而无法工作。

huangapple go评论62阅读模式
英文:

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 (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 (http://localhost:8080/__cypress/src/cypress-entry.js:7784:62)
at webpackJsonpCallback (http://localhost:8080/__cypress/src/runtime.js:312:39)
at (http://localhost:8080/__cypress/src/cypress-entry.js:1:67)

英文:

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 &#39;@angular/core&#39;;

import { UtilityService } from &#39;../../services&#39;;

@Component({
  selector: &#39;example-view&#39;,
  templateUrl: &#39;./example.component.html&#39;,
})
export class ExampleComponent {

  constructor(
    utilityService: UtilityService,
  ) { }
}

My component html:

&lt;h2&gt;Example Component&lt;/h2&gt;

My Cypress component test:

import { ExampleComponent } from &#39;./example.component&#39;
import { UtilityService } from &#39;../../services&#39;;

describe(&#39;Example component&#39;, () =&gt; {
  it(&#39;should mount the component&#39;, () =&gt; {
    const utilityService = new UtilityService();

    cy.mount(ExampleComponent, {
      componentProperties: {
        utilityService,
      },
    });

    cy.get(&#39;h2&#39;).should(&#39;exist&#39;);
    cy.get(&#39;h2&#39;).should(&#39;contain&#39;, &#39;Example Component&#39;);
  })
})

My configuration setup:

import { defineConfig } from &quot;cypress&quot;;

export default defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
  },
  chromeWebSecurity: false,
  screenshotsFolder: &quot;cypress/snapshots&quot;,
  trashAssetsBeforeRuns: true,
  viewportWidth: 1400,
  viewportHeight: 1200,
  video: false,
  env: {
    local: &quot;http://localhost:4200/&quot;,
    staging: &quot;hidden&quot;,
    user: {
      email: &quot;hidden&quot;,
      password: &quot;hidden&quot;,
    },
  },
  component: {
    devServer: {
      framework: &quot;angular&quot;,
      bundler: &quot;webpack&quot;,
    },
    specPattern: &quot;**/*.cy.ts&quot;,
  },
});

UtilityService:

import { Injectable } from &#39;@angular/core&#39;;
import { ReplaySubject, BehaviorSubject } from &#39;rxjs&#39;;

import { ErrorModel } from &#39;../models&#39;;

@Injectable()
export class UtilityService {
  private sideMenu: BehaviorSubject&lt;boolean | null&gt; = new BehaviorSubject(false);
  public sideMenu$ = this.sideMenu.asObservable();

  constructor() { }

  sideMenuToggle(isVisible: boolean): void {
    this.sideMenu.next(isVisible);
  }

  sortData(dataSource, propertyToSortOn: any, event: any): Array&lt;any&gt; {
    return [
      ...dataSource.sort((a: any, b: any) =&gt; {
        if (a[propertyToSortOn] &lt; b[propertyToSortOn]) {
          return event === &#39;ASC&#39; ? -1 : 1;
        }
        if (a[propertyToSortOn] &gt; b[propertyToSortOn]) {
          return event === &#39;DESC&#39; ? -1 : 1;
        }
        return 0;
      }),
    ];
  }
}

Error:

Cypress版本12组件测试由于服务依赖性问题而无法工作。

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 &lt;unknown&gt; (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 &lt;unknown&gt; (http://localhost:8080/__cypress/src/cypress-entry.js:7784:62)
    at webpackJsonpCallback (http://localhost:8080/__cypress/src/runtime.js:312:39)
    at &lt;unknown&gt; (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.

huangapple
  • 本文由 发表于 2023年6月1日 01:06:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定