CodeceptJS icon indicating copy to clipboard operation
CodeceptJS copied to clipboard

Cannot use global plugins with Typescript

Open jonguenther opened this issue 3 years ago • 2 comments

What are you trying to achieve?

I am trying to use plugins like tryTo globally in Typescript, however, it seems that they are not defined.

What do you get instead?

Typescript is throwing Cannot find name 'tryTo'.ts(2304)

Scenario('login', ({ I }) => {
  tryTo(() => I.amOnPage('/login'); // this fails
  I.seeElement({ name: 'login' });
  // ...
});

A workaround would be

const tryTo = codeceptjs.container.plugins('tryTo');

Scenario('login', ({ I }) => {
  tryTo(() => I.amOnPage('/login'); // this works, but parameters are any
  I.seeElement({ name: 'login' });
  // ...
});

However, tryTo then has no type definition (defined as any). My question would be if it is possible to use plugins like tryTo globally and with a type definition in Typescript (latter one being more important).

See this Stackblitz as example (note that there are a few more type issues).

Details

  • CodeceptJS version: 3.2.1
  • NodeJS Version: 14.17.6
  • Operating System: MacOS
Configuration file
require('ts-node/register');

exports.config = {
  tests: './src/e2e/tests/*_test.ts',
  output: './logs/e2e/',
  helpers: {
    Playwright: {
      url: 'https://localhost:8080/',
      show: !process.env.HEADLESS,
      browser: 'chromium',
    },
  },
  include: {
    I: './steps_file.js',
  },
  bootstrap: null,
  mocha: {},
  name: 'TS-Test',
  plugins: {
    pauseOnFail: {},
    retryFailedStep: {
      enabled: true,
    },
    tryTo: {
      enabled: true,
    },
    screenshotOnFail: {
      enabled: true,
    },
  },
};

jonguenther avatar Nov 23 '21 13:11 jonguenther

Thanks for your reply @emanuelwong , but as you mentioned in your quote, registerGlobal should be enabled by default if tryTo is set to enabled, so Typescript theoretically should pick it up, shouldn't it? 😕

jonguenther avatar Jan 10 '22 10:01 jonguenther

I cannot find any type definitions for the global plugin functions like tryTo in CodeceptJS's d.ts files.

ryanknittel-fdx avatar May 26 '22 18:05 ryanknittel-fdx

any news on this ?

sboudouk avatar Oct 18 '22 08:10 sboudouk

hey @sboudouk hope this https://github.com/codeceptjs/CodeceptJS/pull/3461 will kill the issue.

kobenguyent avatar Oct 18 '22 13:10 kobenguyent

Thanks @PeterNgTr , will look forward to this.

sboudouk avatar Oct 18 '22 15:10 sboudouk