Skip to content

bug(mouse): Click triggered with TestHarnesses causes crashes on Vitest using jsdom #32389

@Mikastark

Description

@Mikastark

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When testing click behaviors using Angular's TestHarness (e.g., MatButtonHarness.click()) in a Vitest environment with jsdom, the test fails with the following error:

TypeError: Failed to construct 'PointerEvent': member view is not of type Window.

This issue is caused by the presence of the view: window property in the MouseEvent options, which is not supported in jsdom. This is a known limitation in Vitest (see vitest-dev/vitest#4685).

A temporary solution is to configure Vitest to use the vmThreads pool in the defineConfig:

export default defineConfig({
  test: {
    pool: 'vmThreads',
  }
});

However, this workaround is not ideal, as it is unclear, non-obvious, and may not be recommended for all use cases.

Reproduction

Steps to reproduce:

  1. ng new whatever using @angular/cli@21
  2. ng add @angular/material
  3. Import MatButton in App component
  4. Add the following method
    protected sayHello(): void {
      alert('Hello World!');
    }   
  5. Add matButton element in template
     <button matButton="filled" (click)="sayHello()">Click Me!</button>
  6. Add test case
    it('should open alert with hello world when click me button is clicked', async () => {
      const fixture = TestBed.createComponent(App);
      const loader = TestbedHarnessEnvironment.loader(fixture);
    
      vi.stubGlobal('alert', vi.fn());
    
      const button = await loader.getHarness(
        MatButtonHarness.with({
          text: 'Click Me!',
        })
      );
    
      await button.click();
    
      expect(alert).toHaveBeenCalledWith('Hello World!');
    });
  7. Run ng test
  8. Observe the test failure with the above error.

Expected Behavior

Should not fail

Actual Behavior

TypeError: Failed to construct 'PointerEvent': member view is not of type Window.

Environment

  • Angular: 21.0.0
  • CDK/Material: 21.0.0
  • Node: 24.11.1
  • Operating System: Windows 11

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue needs to be triaged by the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions