Encountered an error while trying to load C:footer.component.html in Angular when running Jest tests: Uncaught promise rejection

It's been a struggle getting Jest to function properly. My goal is to utilize it for creating unit tests for my Angular webapp. I came across this post, but unfortunately, none of the solutions provided worked for me: Error: Uncaught (in promise): Failed to load login.component.html

Every time I attempt to run jest, I encounter the following error:

 PASS  src/app/services/config.service.spec.ts
 FAIL  src/app/footer/footer.component.spec.ts
  ● Console

    console.error node_modules/jest-environment-jsdom-thirteen/node_modules/jsdom/lib/jsdom/virtual-console.js:29
      Error: Error: Invalid protocol: c:
          at Object.dispatchError (C:\Users\Da\workspace\test-login\node_modules/jest-environment-jsdom-thirteen\node_modules/jsdom/lib/jsdom/living/xhr-utils.js:60:19)
          at EventEmitter.client.on.err (C:\Users\Da\workspace\test-login\node_modules/jest-environment-jsdom-thirteen\node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:674:20)
          ...undefined...

  ● FooterComponent › Test2

    Uncaught (in promise): Failed to load C:footer.component.html

      at resolvePromise (node_modules/zone.js/dist/zone.js:852:31)
      at resolvePromise (node_modules/zone.js/dist/zone.js:809:17)
      ...more details...

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 2 passed, 3 total
Snapshots:   0 total
Time:        3.734s
Ran all test suites.
npm ERR! Test failed.  See above for more details.

footer.component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { FooterComponent } from './footer.component';

describe('FooterComponent', () => {
  let component: FooterComponent;
  let fixture: ComponentFixture<FooterComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ FooterComponent ]
    })
    .compileComponents().then(() => {
      fixture = TestBed.createComponent(FooterComponent);
      component = fixture.componentInstance;
    });
  });

  test('Test2', () => {
    const names = ['a,', 'b', 'c'];
    expect(names).toContain('c');
    expect(component.sum()).toBe('test');
  });

});

footer.component.ts

import { Component, OnInit } from '@angular/core';

@Component({
  moduleId: module.id,
  selector: 'app-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

  sum() {
    return 'test';
  }

}

package.json

{
  "name": "test-login",
  "version": "0.0.1",
  ...all package.json content...
}

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    ...all tsconfig compiler options...
  }
}

Answer №1

You made changes to the globals.ts-jest section in your package.json, but you only replaced it with a partial configuration. To complete the setup, include astTransformers (along with any other original settings) as outlined in the documentation of jest-preset-angular found at this link.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exciting updates revealed upon new additions (angular)

I'm working with three components in my Angular application: app.component, post-create.component, and post-list.component. Let's take a look at the structure of these components: <app-header></app-header> <main> <app-post-c ...

What is the method for importing a JavaScript file into my TypeScript file?

Recently, I've encountered an issue while working with Typescript and angular 2. I have built an EncryptionService that looks like this: import {Injectable} from 'angular2/core'; import './lib/hmac256-enc64'; @Injectable() ...

Utilize SCSS values within TypeScript by applying them on a class level

let changeClassDisplay = document.getElementsByClassName('sidebar'); for (var i = 0; i < changeClassDisplay.length; i += 1) { changeClassDisplay[i].style.display = 'block'; } I encountered an issue with this code whe ...

Encountered a type error during project compilation: "Type '(e: ChangeEvent<{ value: string[] | unknown; }>) => void' error occurred."

I recently started working with react and I'm facing an issue with a CustomMultiSelect component in my project. When I try to call events in another component, I encounter the following error during the project build: ERROR: [BUILD] Failed to compile ...

End the primary division post button activation within Angular 2

Is there a way to close the main div upon clicking a button that is located inside the same div? Below is my code snippet: index.html <div id="main"> <button type="button">Click Me!</button> </div> hello.component.ts import ...

Is there a way to stop a click event on an Angular component's host from triggering?

Currently, I'm developing an Angular component that adds extra functionality to a native <button> element. In this component, I would like to mimic the behavior where buttons do not trigger a click event if they are disabled. For instance: < ...

What could be causing the error within the 'react-code-blocks' library?

Currently, I am involved in a project that utilizes Typescript and React. However, I have encountered an issue while working with the 'react-code-blocks' library. The structure of my container component is as follows: interface ICodeBlockProps { ...

Broadening Cypress.config by incorporating custom attributes using Typescript

I'm attempting to customize my Cypress configuration by including a new property using the following method: Cypress.Commands.overwrite('getUser', (originalFn: any) => { const overwriteOptions = { accountPath: `accounts/${opti ...

Exploring the world of publishing Angular 2 applications

I recently created an Angular 2 application using npm, but as a beginner I am unsure of some aspects. For instance, when I publish my application, I typically use npm publish to share it on my npm account online. However, I am wondering if there is a way t ...

The PhpStorm code completion feature is not functioning properly when working with TypeScript code that is distributed through NPM

I am facing an issue with two NPM modules, which we will refer to as A and B. Both modules are written in TypeScript and compile into CommonJS Node-like modules. Module B has a dependency on module A, so I have installed it using the command npm install ...

Driver not found by Browserstack/Selenium

I am facing an issue with BrowserStack while running my tests. The test runs perfectly without any errors when executed locally, and even the sample test provided by BrowserStack passes flawlessly. Below is the error message I encountered: test/silver_au ...

Angular2: Utilizing multiple slots for transcluded content and passing them to child components as parameters

My current setup includes an article component prefixed with b-... <b-article> Inside this component, I am using custom elements: b-title b-photo b-summary Here is an example of how I am using these elements: <b-article-item> <b-ti ...

What is the best way to retain all checkbox selections from a form upon submission?

I have a batch of checkboxes that correspond to the profiles I intend to store in the database. HTML: <tr *ngFor="let profile of profiles | async"> <input type='checkbox' name="profiles" value='{{profile.id}}' ng-model=&apo ...

Navigating through the child elements of a parent element in Aurelia

I am currently using Aurelia 1 to construct my application. Right now, I am in the process of creating a custom toolbar element known as custom-toolbar.ts. Within this toolbar, there will be an unspecified number of child elements referred to as custom-too ...

Jest is unable to handle SVG files when they are imported with an argument

Using @nuxtjs/svg with "vue-svg-loader" mode means importing SVGs like this: import ArrowRight from '~/assets/img/arrow-right.svg?inline' However, Jest encounters a problem with the "?inline" part: Could not find module ~/assets/img/arrow-right. ...

How to Programmatically Assign Bootstrap Class to an Element Using Angular 2

I am working on a page with several input boxes that need to be flagged for certain criteria. <div class="form-group"> <label class="d-block">Allowance Type</label> <div class="input-group"> ...

Issue with reflect metadata in Next.js edge runtime causing functional problems

Currently, I am utilizing a package in my upcoming 13 app that incorporates reflect metadata. Unfortunately, during the next build process, an error occurs for which I haven't been able to find a solution. ../../eshop-sdk-js/node_modules/reflect-metad ...

The installation of npm was unsuccessful due to an error in the postinstall script of the [email protected] package

Looking for help with my Angular application? Check it out on https://github.com/XBITSwitzerland/ngx-admin/tree/ng2-admin I'm trying to run: npm install Encountering the following error (excerpt from output): gyp ERR! build error gyp ERR! stack Er ...

Changing a table cell's value when a button is clicked using Angular

I'm working with a bootstrap table that includes a button and a textbox above it. After entering a number in the textbox and clicking the generate button, I need to update the "Bill Amount" column for each row with the value from the textbox. How can ...

Angular BehaviorSubject is not refreshing quickly enough

After following a tutorial on creating full Angular + JWT Authentication, I encountered some issues when testing the project. In order to notify the AuthGuard that I am connected and can proceed to the next page upon logging in, I needed to send the API re ...