0

I am trying to test an angular application using Jasmine which I encountered a method having CustomEvent interface as an argument.

 beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [
        HttpClientModule,
        RouterTestingModule,
        FormsModule
      ],
      declarations: [ MyComponent],
      providers: [MyService,CustomEvent]
    })
    .compileComponents();
  });

it('should return void if submitted without selecting user', () => {
    console.log("promise" + fixture.componentInstance.submitForm());// submit form is a method from the MyComponent class
  })

When I run this test case I am getting the below error message. Error: Can't resolve all parameters for CustomEvent: (?).

Also the same occurs for Event as well. Please let me know what to add inorder to resolve this

Vineel Pellella
  • 332
  • 2
  • 4
  • 20
  • You're getting this error most likely because `CustomEvent` has services (or a service because of the error showing one question mark) it depends on and it needs to be provided as well. I would personally mock `CustomEvent` or external services in a unit test. Can you show the constructor of `CustomEvent`? – AliF50 Jan 24 '22 at 13:55
  • CustomEvent is an interface available under node_modules/typescript/lib/lib.dom.d.ts – Vineel Pellella Jan 24 '22 at 13:59
  • Try removing `CustomEvent` from `providers` array. – AliF50 Jan 24 '22 at 14:00
  • we will get the below error if we remove that NullInjectorError: R3InjectorError(CompilerModule)[CustomEvent -> CustomEvent]: NullInjectorError: No provider for CustomEvent! – Vineel Pellella Jan 24 '22 at 14:09
  • I think you're facing a variation of this issue: https://stackoverflow.com/a/39483673/7365461. Also check out the 3rd post there as well. I would remove `HttpClientModule` and use `HttpClientTestingModule`. I think we can't use `HttpClientModule` in a unit test. – AliF50 Jan 24 '22 at 14:15

1 Answers1

0

After some research below approach is working fine

Create event object as below and pass it to the method required var event = new CustomEvent("test", { detail: 'test' });

Vineel Pellella
  • 332
  • 2
  • 4
  • 20