我在使用 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'));
最佳答案
我看到的两种潜在可能性:
您可能错误地导入了模块,使用自动为您导入的 IDE 很容易做到这一点。有关示例,请参阅此线程的最后一条评论:https://github.com/angular/angular-cli/issues/8357
Angular 不支持
fs
模块(请参阅下面的链接),因为它需要使用浏览器无法使用的 native 代码。在您提供的代码中,您没有使用fs
。您是否有可能将其导入项目中的某个地方?如果您没有直接导入它,您的某个库可能有。您可能想要查看您为不支持浏览器的库安装了npm install
。 https://github.com/angular/angular-cli/issues/8272
正如您帖子中的评论所暗示的那样,如果您添加更多代码(或链接到 GitHub 存储库),我们可能会提供更多建议。
关于Angular 5 测试返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49812765/