Skip to content

CodeceptJS 4.x: Custom helper type definition created by codeceptjs def doesn't work (Property does not exist on type 'I')Β #5318

@mirao

Description

@mirao

The problem started with CodeceptJS 4.x because a helper in CodeceptJS needs to be exported with export default MyHelper (ESM) instead of export = MyHelper (CommonJS)

config codecept.conf.ts:

export const config: CodeceptJS.MainConfig = {
  tests: "./*_test.ts",
  output: "./output",
  helpers: {
    Playwright: {
      browser: "chromium",
      url: "http://localhost",
      show: true,
    },
    MyHelper: {
      require: "./myhelper_helper.ts",
    },
  },
  name: "esm",
  require: ["tsx/esm"],
};

myhelper_helper.ts:

import Helper from "@codeceptjs/helper";

class MyHelper extends Helper {
  openPage(url: string): Promise<void> {
    return this.helpers.Playwright.amOnPage(url);
  }
}

export default MyHelper;

steps.d.ts (created by codeceptjs def after adding of the MyHelper config into codecept.conf.ts)

/// <reference types='codeceptjs' />
type MyHelper = import("./myhelper_helper");

declare namespace CodeceptJS {
  interface SupportObject { I: I, current: any }
  interface Methods extends Playwright, MyHelper {}
  interface I extends WithTranslation<Methods> {}
  namespace Translation {
    interface Actions {}
  }
}

test

Feature("My");

const {I} = inject();

Scenario("test something",  () => {
    I.openPage("http://codecept.io");
});

Expected result:

  • TS check and IDE IntelliSense works well

Actual result:

  • It fails
Image
mirao@rog:~/workspace/codeceptjs/esm$ npx tsc --noEmit
My_test.ts:6:7 - error TS2339: Property 'openPage' does not exist on type 'I'.

6     I.openPage("http://codecept.io");
        ~~~~~~~~


Found 1 error in My_test.ts:6

Note that the test passes because tsx doesn't do a TS check. But if you run it with require: ["ts-node/esm"] it fails (ts-node does a TS check).

AI suggested me this fix (and it works).

type MyHelper = InstanceType<typeof import("./myhelper_helper").default>;

instead of

type MyHelper = import("./myhelper_helper");

Used SW:

  • CodeceptJS 4.0.1-beta.12
  • Playwright 1.56.1

Metadata

Metadata

Assignees

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