I've migrated my angular 21 tests from jasmine to vitest. And for some of my tests I use separate helper classes (like Page object models). For example:

export class SignUpPageObject {
   constructor(fakeVerificationSvc: IUserVerificationService) {
      TestBed.overrideProvider(UserVerificationService, { useValue: fakeVerificationSvc });

      this.fixture = TestBed.createComponent(IlgSignUpForm);
      this.fixture.detectChanges();
      this.component = this.fixture.componentInstance;
      this.loader = TestbedHarnessEnvironment.loader(this.fixture);
      this.docRootLoader = TestbedHarnessEnvironment.documentRootLoader(this.fixture);
   }

   get emailInput(): Promise<MatInputHarness> {
      return this.loader.getHarness(MatInputHarness.with({ label: "Email" }));
   }

   async getErrorDialog(): Promise<MatDialogHarness> {
      const dlgHarnesses = await this.docRootLoader.getAllHarnesses(MatDialogHarness.with({ selector: "#ilgErrorDlg" }));
      return dlgHarnesses[0];
   }

   async getMessageDialog(): Promise<MatDialogHarness> {
      const dlgHarnesses = await this.docRootLoader.getAllHarnesses(MatDialogHarness.with({ selector: "#ilgMessageDlg" }));
      return dlgHarnesses[0];
   }

   async clickSendCodeBtn(): Promise<void> {
      const sendCodeBtn = await this.sendCodeBtn;
      await sendCodeBtn.click();
   }

   async isSendCodeBtnDisabled(): Promise<boolean> {
      return (await this.sendCodeBtn).isDisabled();
   }

   async emailHasErrors(): Promise<boolean> {
      const emailFld = await this.emailFld;
      return await emailFld.hasErrors();
   }

   async getEmailValue(): Promise<string> {
      return (await this.emailInput).getValue();
   }


   // ...


   async verificationCodeHasErrors(): Promise<boolean> {
      return (await this.verificationCodeFld).hasErrors();
   }

   async formErrorsCount(): Promise<number> {
      const errHarnesses = await this.loader.getAllHarnesses(MatErrorHarness);
      for (let errHarness of errHarnesses) {
         console.log(await errHarness.getText());
      }
      return errHarnesses.length;
   }

   async verificationCodeIsChecked(): Promise<boolean> {
      // there should <mat-icon>check</mat-icon> to appear
      return (await (await this.verifiedMatIcon).getName()) === "check";
   }

   readonly component: MyForm;
   readonly fixture: ComponentFixture<MyForm>;

   private readonly code?: number;
   private readonly loader: HarnessLoader;
   private readonly docRootLoader: HarnessLoader;
}

The name of this file is page-object.spec.ts. But when I run tests with vitest, I get an error:

No test suite found in file '...\page-object.spec.ts`

How should I name it so by glancing a name it would be clear that this file is part of the tests? Obviously, the *.spec.ts does not work.

3 Replies 3

If you include the file in your tsconfig.spec.json, does it make any difference? You could also consider refactoring the helper class to the new providersFile and setupFiles options.

vitest expects test suites describe(...) and some tests in the *.spec.ts files. that is what the error is highlighting. can you use different extensions for po files, for example *.spec-po.ts?

I renamed test specific helper files to *.specx.ts. Also, I had to add change tsconfig.app.json:

{
   "extends": "./tsconfig.json",
   "compilerOptions": {
      "outDir": "./out-tsc/app",
      "types": [
         "@angular/localize"
      ]
   },
   "include": [
      "src/**/*.ts"
   ],
   "exclude": [
      "src/**/*.spec.ts",
      "src/**/*.specx.ts"  <===
   ]
}

and tsconfig.spec.json:

{
   "extends": "./tsconfig.json",
   "compilerOptions": {
      "outDir": "./out-tsc/spec",
      "types": [
         "@angular/localize",
         "vitest/globals"
      ]
   },
   "include": [
      "src/**/*.d.ts",
      "src/**/*.spec.ts",
      "src/**/*.specx.ts"  <===
   ]
}

Now it's all good! Thanks.

Your Reply

By clicking “Post Your Reply”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.