Error encountered during test execution with Angular 2 template parsing issue

I have a basic component that I am working with:

galery.component.ts

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

@Component({
  selector: 'galery-component',
  templateUrl: 'galery.component.html',
  styleUrls: ['galery.component.css']
})
export class GaleryComponent {
  @Input() userPosts;
}

Within the HTML file of this component, there is a custom tag which is a selector for another component in my module.

galery.comonent.html

 <div class="container">
        <post-details class="post-container" *ngFor="let post of userPosts" [singlePost] = "post">
        </post-details>
    </div>

When attempting to run my test case, it fails at the start with the following error message:

  1. If 'post-details' is an Angular component and it has 'singlePost' input, then verify that it is part of this module.
  2. If 'post-details' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message.

Below is the test code:

test.spec.ts

describe('BannerComponent (inline template)', () => {

  let comp:    GaleryComponent;
  let fixture: ComponentFixture<GaleryComponent>;
  let de:      DebugElement;
  let el:      HTMLElement;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ GaleryComponent ],
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(GaleryComponent); // test fails at this point

    comp = fixture.componentInstance;
    de = fixture.debugElement.query(By.css('post-details'));
    el = de.nativeElement;
  });

I have already added a CUSTOM_ELEMENTS_SCHEMA to my module, but the issue persists. Here is app.module.ts:

app.module.ts

import {NgModule, CUSTOM_ELEMENTS_SCHEMA} from '@angular/core';
import {AppComponent} from './app.component';
import {GaleryComponent} from './Components/galeryComponent/galery.component';
import {PostDetailsComponent} from './Components/postDetailsComponent/post-details-component.component';

@NgModule({
  declarations: [
    AppComponent,
    GaleryComponent,
    PostDetailsComponent
  ],
  exports: [GaleryComponent],
  imports: [
    CommonModule,
    NgbModule.forRoot(),
  ],
  providers: [],
  bootstrap: [AppComponent],
  schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})

export class AppModule {
}

If anyone has any insight into what might be causing this issue, I would greatly appreciate the help. I have looked at similar questions but have not found a solution yet.

Answer №1

It seems like the schema may have been defined in the wrong location.

Here are a couple of suggestions to try:

TestBed.configureTestingModule({
  declarations: [GaleryComponent],
  schemas: [CUSTOM_ELEMENTS_SCHEMA] // Use this if the selector contains a '-'
})

or

TestBed.configureTestingModule({
  declarations: [GaleryComponent],
  schemas: [NO_ERRORS_SCHEMA]
})

For more information, you can also check out:

  • Essential Angular: Testing

Answer №2

It seems like the issue lies within the property binding [singlePost] as it attempts to locate it in the @Input.

Ensure that your property binding is correctly configured.

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

Changing the Value of an Input Element Dynamically in React: A Step-by-Step Guide

In a scenario where I have a component that takes an element, such as <input />, and I need to update its value programmatically after 15 seconds. Initially, I had the following approach in mind: const MyComponent = (myInput: JSX.Element) => { ...

Concealing the value of the variable within the state function

CODE USED: /*Code snippet from another page with specific URL*/ .state("main/handler/location/userSso",{ url:"/main/handler/:location/:", templateUrl:"component/common/main.html", controller: 'MainContro ...

The Angular BehaviorSubject observable is returning a null value

I am experiencing an issue where I pass data through components using behavior subject. However, when I try to retrieve it with subscribe, it shows null even though the service returns a real value. To reproduce the issue, you can check out the code here: ...

Angular 2 Route offers a blueprint for creating a Component template

Is it possible to assign a specific template to a component within a route in Angular 2? I am working on a component that has the same controller functionality across three different views. My goal is to use the same component for all three views, but wit ...

Tips for sidestepping the need for casting the class type of an instance

Looking to develop a function that can accept an argument containing a service name and return an instance of that particular service, all while ensuring proper typing. Casting the instance seems necessary in order to achieve the desired result. To illust ...

What are the methods to alter validation for a Formfield based on the input from other Formfields?

My aim is to create a Form where input fields are required only if one or more of them are filled out. If none of the fields have been filled, then no field should be mandatory. I came across a suggestion on a website that recommended using "valueChanges" ...

What Are the Possible Use Cases for Template Engine in Angular 2?

For the development of a large single page application (SPA) with Angular 2.0, I have decided to utilize a template engine like JADE/PUG in order to enhance clarity and clean up the code. My goal is to achieve optimal performance for the application. Th ...

The most effective method for monitoring updates to an array of objects in React

Imagine a scenario where an array of objects is stored in state like the example below: interface CheckItem { label: string; checked: boolean; disabled: boolean; } const [checkboxes, setCheckboxes] = useState<CheckItem[] | undefined>(undefined ...

The Angular build process was successful on a local machine, however, the Angular build failed when

I am facing a challenge with my Angular project that I want to deploy on Gitlab Pages. Initially, when I execute: ng build --prod locally, the build is successful. Below is my .gitlab-ci.yaml: image: node:8.12.0 pages: cache: paths: - node_mo ...

Creating a Class in REACT

Hello fellow coding enthusiasts, I am facing a minor issue. I am relatively new to REACT and Typescript, which is why I need some assistance with the following code implementation. I require the code to be transformed into a class for reusability purposes ...

Utilize the identical element

Incorporating the JwPaginationComponent into both my auction.component and auctiongroup.component has become a necessity. To achieve this, I have created a shared.module.ts: import { NgModule } from '@angular/core'; import { JwPaginationCompon ...

Show a notification if the search bar returns no results

I am facing an issue with my search functionality. When a user searches for something not in the list, an error message should be displayed. However, in my case, if I search for "panadol" in my list, it displays the list containing that word and shows an e ...

Highlighting in Coda on MacOS now supports TypeScript

Can anyone help me with getting my Coda editor to properly highlight TypeScript? I checked this page and it says that TypeScript is supported: But in my up-to-date version of Coda, the list of supported languages seems different. Is there a way to make Ty ...

Identifying whether an Angular component has been loaded through a template or a route: Ways to distinguish

How can I determine how an Angular component was loaded? I have a component that can be accessed through a route or loaded within another component's template, and I want to be able to identify the loading method for the purpose of animations. ...

The verifyTrue(boolean) method is not recognized for this type of data

Hey there, everyone! I'm a beginner here, so please bear with me! In my current class, I am attempting to check if the text 'Fictitious Test Company' exists anywhere on the page. If it does, I want to click and delete that company. Otherwi ...

Arrange items in a particular order

I need help sorting the object below by name. The desired order is 1)balloon 2)term 3)instalment. loanAdjustmentList = [ { "description": "Restructure Option", "name": "instalment", " ...

Is there a way to logout when the select event occurs using the key?

I am trying to figure out how to log out the key value when the select event is triggered. Specifically, I want to access the key={localState.id} within the <select></select> element in my HTML. This key value is crucial for a conditional stat ...

Every day, I challenge myself to build my skills in react by completing various tasks. Currently, I am facing a particular task that has me stumped. Is there anyone out there who could offer

Objective:- Input: Ask user to enter a number On change: Calculate the square of the number entered by the user Display each calculation as a list in the Document Object Model (DOM) in real-time If Backspace is pressed: Delete the last calculated resul ...

What could cause a member variable to be uninitialized in the ngInit method in Ionic/Angular, even though it was initially set in the constructor

Despite setting the modal form to be bound to an instance of "Foo" during its construction, I encountered a strange issue where, post-constructor, this.foo became undefined. Even after verifying this through breakpoints and console.log, the problem persist ...

Having trouble finding an element with just two attributes

View the requirement screenshot here I'm trying to identify this element in my script, but I've tested all of the following possibilities with no success: 1.//*[@class ='tabnavbar_subnav_level2_link' and contains(text(),'transact ...