Home > Mobile >  Unit test - Error message on mock component in directive. 'component' is declared but its
Unit test - Error message on mock component in directive. 'component' is declared but its

Time:04-27

I'm trying to mock a component to unit test the SetCursorPositionDirective directive, but I'm getting the error 'component' is declared but its value is never read.

import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { SetCursorPositionDirective } from './set-cursor-position.directive';

describe('SetCursorPositionDirective', () => {
  let fixture: ComponentFixture<MockSetCursorPositionComponent>;
  let component: MockSetCursorPositionComponent;
  let input: HTMLInputElement;
  let directive: any;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [SetCursorPositionDirective, MockSetCursorPositionComponent],
      imports: [FormsModule],
    });
    fixture = TestBed.createComponent(MockSetCursorPositionComponent);
    component = fixture.componentInstance;
    input = fixture.debugElement.query(By.css('input')).nativeElement;
    directive = new SetCursorPositionDirective();
  });

  it('should create an instance', () => {
    expect(directive).toBeTruthy();
  });
});

@Component({ template: '<input setCursorPosition>' })
export class MockSetCursorPositionComponent { }

CodePudding user response:

The error you are getting is because your component variable is never actually used anywhere. Its only ever defined and then assigned a value but never used again. You really dont need the variable at all unless you plan to use it in your unit tests.

CodePudding user response:

Thanks for the information. I thought that by assigning a value to the variable, this would not happen.

  • Related