Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 5 Testing returns errors

I am having some troubles with an Angular Project. I am just learning Angular and encountering all sort of errors along the way.

My project is compiling and running fine but whenever I am trying to run an "ng test" to see if my test succeed I am getting all sort of errors back. They look like this:

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

or

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

And so on. I read about the fs that I was supposed to add target: 'node' in the webpack config. When I did an ng Eject the webpack config file had this

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

My test looks something like this, I'm only attaching the part where errors have started:

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'));
like image 261
Vlad N Avatar asked Apr 13 '18 08:04

Vlad N


2 Answers

Two potential possibilities that I see:

  1. You may be importing a module incorrectly, this is very easy to do with IDEs that auto import for you. See the last comment on this thread for an example: https://github.com/angular/angular-cli/issues/8357

  2. The fs module is not supported in Angular (see the link bellow) since it requires the use of native code not available to the browser. In the code you provided you are not using fs. Is it possible you have imported it somewhere in your project? If you haven't directly imported it, it is possible that one of your libraries has. You may want to review the libraries you have npm install'ed for ones that don't support the browser. https://github.com/angular/angular-cli/issues/8272

As the comments on your post suggest, if you add more code (or link to a GitHub repository) we may be able to offer additional suggestions.

like image 188
Pearman Avatar answered Oct 05 '22 03:10

Pearman


You have to just delete the node modules folder inside project and run command 'npm install'. and check whether your problem will be solved or not..

like image 38
Omkar Jadhav Avatar answered Oct 05 '22 03:10

Omkar Jadhav