首页 > 解决方案 > ng test 如何导致依赖解析错误?

问题描述

在使用我测试组件时ng test遇到了与此类似的一连串错误

Error: ./node_modules/protractor/built/runner.js
Module not found: Error: Can't resolve 'child_process' in '/<path-to-project>/node_modules/protractor/built'
resolve 'child_process' in '/<path-to-project>/node_modules/protractor/built'
Parsed request is a module using description file: /<path-of-project>/node_modules/protractor/package.json (relative path: ./built)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
<continued with other modules e.g. sauceLabs, selenium-webdriver>

该测试仅测试是否定义了组件的一部分。

const card = element(By.tag('mat-card-content'))
expect(card).toBeDefined()

标签: angulartestingjasmine

解决方案


解决方案是elementng e2e非单元测试中使用。因此我应该使用

const card = fixture.nativeElement.querySelector('mat-card-content')

选择我的元素。如果你的组件不是像 web worker 这样的特殊情况,你可以使用 DOM APIfixture.nativeElement


推荐阅读