Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"codeceptjs gt" fails if config contains a custom helper: Could not load helper MyHelper from module './helper/Playwright' #3512

Closed
mirao opened this issue Dec 2, 2022 · 3 comments · Fixed by #3611

Comments

@mirao
Copy link
Contributor

mirao commented Dec 2, 2022

It's a regression in 3.3.7.
It works well in 3.3.6

What are you trying to achieve?

npx codeceptjs gt should work

What do you get instead?

When you set a feature name and a file name, it fails

$ npx codeceptjs gt
Creating a new test...
----------------------
? Feature which is being tested (ex: account, login, etc) My2
? Filename of a test My2_test.ts

Could not load helper MyHelper from module './helper/Playwright':
The "path" argument must be of type string. Received undefined
TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at new NodeError (internal/errors.js:322:7)
    at validateString (internal/validators.js:124:11)
    at Object.resolve (path.js:1074:7)
    at createHelpers (/home/mirao/tmp/node_modules/codeceptjs/lib/container.js:161:29)
    at Function.create (/home/mirao/tmp/node_modules/codeceptjs/lib/container.js:44:25)
    at /home/mirao/tmp/node_modules/codeceptjs/lib/command/generate.js:60:15
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Command.<anonymous> (/home/mirao/tmp/node_modules/codeceptjs/bin/codecept.js:9:5)

Error: 
    at createHelpers (/home/mirao/tmp/node_modules/codeceptjs/lib/container.js:184:13)
    at Function.create (/home/mirao/tmp/node_modules/codeceptjs/lib/container.js:44:25)
    at /home/mirao/tmp/node_modules/codeceptjs/lib/command/generate.js:60:15
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Command.<anonymous> (/home/mirao/tmp/node_modules/codeceptjs/bin/codecept.js:9:5)

myhelper_helper.js

const Helper = require('@codeceptjs/helper');

class MyHelper extends Helper {

  // before/after hooks
  /**
   * @protected
   */
  _before() {
    // remove if not used
  }

  /**
   * @protected
   */
  _after() {
    // remove if not used
  }

  // add custom methods here
  // If you need to access other helpers
  // use: this.helpers['helperName']

}

module.exports = MyHelper;

Details

  • CodeceptJS version: 3.3.7
  • NodeJS Version: 14.21.1
  • Operating System: Ubuntu 22.04
  • Playwright 1.28.1
  • Configuration file:

codecept.conf.ts

import { setHeadlessWhen, setCommonPlugins } from '@codeceptjs/configure';
// turn on headless mode when running with HEADLESS=true environment variable
// export HEADLESS=true && npx codeceptjs run
setHeadlessWhen(process.env.HEADLESS);

// enable all common plugins https://github.com/codeceptjs/configure#setcommonplugins
setCommonPlugins();

export const config: CodeceptJS.MainConfig = {
  tests: './*_test.ts',
  output: './output',
  helpers: {
    Playwright: {
      url: 'http://localhost',
      show: true,
      browser: 'chromium'
    },
     MyHelper: {
    require: './myhelper_helper.js',
  }
  },
  include: {
    I: './steps_file'
  },
  name: 'tmp'
}
@mirao
Copy link
Contributor Author

mirao commented Dec 2, 2022

For the reported case (helper in JavaScript), it seems to be fixed by a8c2cb2

However it fails for helper in TypeScript, this time with another error:

Could not load helper MyHelper from module '/home/mirao/tmp/myhelper_helper.ts':
Helper is not defined
ReferenceError: Helper is not defined
    at Object.<anonymous> (/home/mirao/tmp/myhelper_helper.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Module.m._compile (/home/mirao/tmp/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Object.require.extensions.<computed> [as .ts] (/home/mirao/tmp/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at createHelpers (/home/mirao/tmp/node_modules/codeceptjs/lib/container.js:170:33)

Error: 
    at createHelpers (/home/mirao/tmp/node_modules/codeceptjs/lib/container.js:186:19)
    at Function.create (/home/mirao/tmp/node_modules/codeceptjs/lib/container.js:46:29)
    at /home/mirao/tmp/node_modules/codeceptjs/lib/command/generate.js:60:15
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async Command.<anonymous> (/home/mirao/tmp/node_modules/codeceptjs/bin/codecept.js:9:5)

myhelper_helper.ts

class MyHelper extends Helper {

    // before/after hooks
    /**
     * @protected
     */
    _before() {
        // remove if not used
    }

    /**
     * @protected
     */
    _after() {
        // remove if not used
    }

    // add custom methods here
    // If you need to access other helpers
    // use: this.helpers['helperName']

}

export = MyHelper;

codecept.conf.ts

import { setHeadlessWhen, setCommonPlugins } from '@codeceptjs/configure';
// turn on headless mode when running with HEADLESS=true environment variable
// export HEADLESS=true && npx codeceptjs run
setHeadlessWhen(process.env.HEADLESS);

// enable all common plugins https://github.com/codeceptjs/configure#setcommonplugins
setCommonPlugins();

export const config: CodeceptJS.MainConfig = {
    tests: './*_test.ts',
    output: './output',
    helpers: {
        Playwright: {
            url: 'http://localhost',
            show: true,
            browser: 'chromium'
        },
        MyHelper: {
            require: './myhelper_helper.ts',
        },
    },
    include: {
        I: './steps_file'
    },
    name: 'tmp',
    fullPromiseBased: true
}

It works well with CodeceptJS 3.3.6, with TS helper.

@mirao
Copy link
Contributor Author

mirao commented Dec 3, 2022

However it fails for helper in TypeScript, this time with another error

It fails only if const Helper = require('@codeceptjs/helper'); is missing before definition of class in a custom TS helper.
But it's a real issue because helpers such as codeceptjs-resemblehelper or codeceptjs-chai don't have defined Helper explicitly.

It seems that 3.3.7 is missing a global definition of "Helper" so that you don't need to specify it in every custom helper ?
Note that tests work well even without const Helper = require('@codeceptjs/helper');. Only npx codeceptjs gt is problematic.

@mirao
Copy link
Contributor Author

mirao commented Feb 17, 2023

A possible fix is mentioned in #3514 (comment).
Not sure how correct it is, but it works for me.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant