Skip to content

createHostFactory with declareComponent: false can't find tested component in the host with Vitest test runner #724

@th0r

Description

@th0r

Is this a regression?

No

Description

I have a module ButtonModule with a single non-standalone ButtonComponent exported from it.

As a unit tests runner I use Angular's experimental @angular/build:unit-test builder with the following configuration:

"test": {
  "builder": "@angular/build:unit-test",
  "options": {
    "runner": "vitest",
    "browsers": [
      "chromium"
    ],
    "buildTarget": "::development",
    "tsConfig": "tsconfig.spec.json",
    "codeCoverage": false
  }
}

In the ButtonComponent's spec there is the following code:

import {ButtonComponent} from './button.component';
import {
  createHostFactory,
  SpectatorHost
} from '@ngneat/spectator/vitest';
import {ButtonModule} from './button-module';

describe('ButtonComponent', () => {
  let spectator: SpectatorHost<ButtonComponent>;
  const createComponentHost = createHostFactory({
    component: ButtonComponent,
    imports: [ButtonModule],
    declareComponent: false
  });

  it('should create', () => {
    spectator = createComponentHost('<app-button>');

    expect(spectator.query('button')).toExist();
  });
});

Note declareComponent: false option.
Running npm test leads to the following runtime error:

Error: Cannot find component/directive class _ButtonComponent {
  static \u0275fac = function ButtonComponent_Factory(__ngFactoryType__) {
    return new (__ngFactoryType__ || _ButtonComponent)();
  };
  static \u0275cmp = /* @__PURE__ */ \u0275\u0275defineComponent({ type: _ButtonComponent, selectors: [["app-button"]], standalone: false, ngContentSelectors: _c0, decls: 2, vars: 0, template: function ButtonComponent_Template(rf, ctx) {
    if (rf & 1) {
      \u0275\u0275projectionDef();
      \u0275\u0275elementStart(0, "button");
      \u0275\u0275projection(1);
      \u0275\u0275elementEnd();
    }
  }, encapsulation: 2 });
} in host template 😔

Changing the test config to the default one based on Karma (see below) and changing the import in button.component.spec.ts from @ngneat/spectator/vitest to @ngneat/spectator fixes the problem and the test passes:

"test": {
  "builder": "@angular/build:karma",
  "options": {
    "polyfills": [
      "zone.js",
      "zone.js/testing"
    ],
    "tsConfig": "tsconfig.spec.json",
    "assets": [
      {
        "glob": "**/*",
        "input": "public"
      }
    ],
    "styles": [
      "src/styles.css"
    ]
  }
}

In the reproduction repo run npm i && npm test and you'll see the error.

Please provide a link to a minimal reproduction of the bug

https://github.com/th0r/ng-neat-vitest-host-component-bug

Please provide the exception or error you saw

Error: Cannot find component/directive class _ButtonComponent {
  static \u0275fac = function ButtonComponent_Factory(__ngFactoryType__) {
    return new (__ngFactoryType__ || _ButtonComponent)();
  };
  static \u0275cmp = /* @__PURE__ */ \u0275\u0275defineComponent({ type: _ButtonComponent, selectors: [["app-button"]], standalone: false, ngContentSelectors: _c0, decls: 2, vars: 0, template: function ButtonComponent_Template(rf, ctx) {
    if (rf & 1) {
      \u0275\u0275projectionDef();
      \u0275\u0275elementStart(0, "button");
      \u0275\u0275projection(1);
      \u0275\u0275elementEnd();
    }
  }, encapsulation: 2 });
} in host template 😔

Please provide the environment you discovered this bug in

Angular CLI: 20.2.2
Node: 22.16.0
Package Manager: npm 10.9.3
OS: darwin arm64


Angular: 20.2.4
... common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router

Package                      Version
------------------------------------
@angular-devkit/architect    0.2002.2
@angular-devkit/core         20.2.2
@angular-devkit/schematics   20.2.2
@angular/build               20.2.2
@angular/cli                 20.2.2
@schematics/angular          20.2.2
rxjs                         7.8.2
typescript                   5.9.2
zone.js                      0.15.1

Anything else?

No response

Do you want to create a pull request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions