Angular 5 测试返回错误

标签 angular testing webpack angular-material

我在使用 Angular 项目时遇到了一些问题。我只是在学习 Angular 并在此过程中遇到各种错误。

我的项目编译和运行良好,但每当我尝试运行“ng 测试”以查看我的测试是否成功时,我都会返回各种错误。它们看起来像这样:

ERROR in ./node_modules/webdriver-js-extender/node_modules/selenium-webdriver/firefox/extension.js
Module not found: Error: Can't resolve 'fs' in '

ERROR in ./node_modules/tough-cookie/lib/cookie.js
Module not found: Error: Can't resolve 'net' in 

等等。我读到了我应该在 webpack 配置中添加 target: 'node' 的 fs。当我执行 ng Eject 时,webpack 配置文件有这个

...
"node": {
    "fs": "empty",
...

我的测试看起来像这样,我只附上错误开始的部分:

it('should send credentials on submit', () => {
  let fixture = TestBed.createComponent(LoginComponent);
  let component: LoginComponent = fixture.componentInstance;
  let element = fixture.nativeElement;

  fixture.detectChanges();

  element.username = expectedUsername;


  element.query(by.id('login-username')).value = expectedUsername;
  element.query(by.id('login-username')).dispatchEvent(new Event('input'));
  element.query(by.id('login-password')).value = expectedPassword;
  element.query(by.id('login-password')).dispatchEvent(new Event('input'));

最佳答案

我看到的两种潜在可能性:

  1. 您可能错误地导入了模块,使用自动为您导入的 IDE 很容易做到这一点。有关示例,请参阅此线程的最后一条评论:https://github.com/angular/angular-cli/issues/8357

  2. Angular 不支持 fs 模块(请参阅下面的链接),因为它需要使用浏览器无法使用的 native 代码。在您提供的代码中,您没有使用fs。您是否有可能将其导入项目中的某个地方?如果您没有直接导入它,您的某个库可能有。您可能想要查看您为不支持浏览器的库安装了 npm installhttps://github.com/angular/angular-cli/issues/8272

正如您帖子中的评论所暗示的那样,如果您添加更多代码(或链接到 GitHub 存储库),我们可能会提供更多建议。

关于Angular 5 测试返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49812765/

相关文章:

asp.net-mvc - Angular 2 (SystemJS) XHR 错误(未找到 404)/@angular/animations

testing - RSpec:测试 rescue_from

asp.net-mvc - NUnit框架生命周期执行顺序是什么?

javascript - WebPack无法在本地项目中通过命令执行

webpack - 如何使用 nunjucks 文件作为 webpack 的入口点?

angular - 从 Angular 2 应用程序连接到 Google Cloud Endpoints

file - Angular2文件输入onchange

testing - Testcafe 测试未在 MacBook Big Sur 上执行,带有 UnableToAccessScreenRecordingAPIError :

webpack - 如何防止 secret 数据包含在 WebPack bundle 中

node.js - 在 Angular Universal 应用程序中从 Express JS 获取值