codeceptjs / CodeceptJS

Supercharged End 2 End Testing Framework for NodeJS
http://codecept.io
MIT License
4.11k stars 723 forks source link

Cannot run tests on local machine using @wdio/selenium-standalone-service because of ERR_REQUIRE_ESM #3574

Closed esther-86 closed 9 months ago

esther-86 commented 1 year ago

What are you trying to achieve?

I would like to run tests locally using @wdio/selenium-standalone-service

What do you get instead?

Could not load plugin wdio from module './plugin/wdio':

Provide console output if related. Use --verbose mode for more details.

Could not load plugin wdio from module './plugin/wdio':
Couldn't initialise "@wdio/selenium-standalone-service".
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/@wdio/selenium-standalone-service/build/index.js
require() of ES modules is not supported.
require() of /Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/@wdio/selenium-standalone-service/build/index.js from /Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/codeceptjs/lib/plugin/wdio.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/@wdio/selenium-standalone-service/package.json.

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1015:13)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at safeRequire (/Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/codeceptjs/lib/plugin/wdio.js:246:12)
    at module.exports (/Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/codeceptjs/lib/plugin/wdio.js:104:21)
    at createPlugins (/Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/codeceptjs/lib/container.js:291:44)
    at Function.create (/Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/codeceptjs/lib/container.js:47:25)
    at Codecept.init (/Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/codeceptjs/lib/codecept.js:56:15)
Error: Couldn't initialise "@wdio/selenium-standalone-service".
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/@wdio/selenium-standalone-service/build/index.js
require() of ES modules is not supported.
require() of /Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/@wdio/selenium-standalone-service/build/index.js from /Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/codeceptjs/lib/plugin/wdio.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users//Desktop/Repositories/PoCs/codecept_poc/node_modules/@wdio/selenium-standalone-service/package.json.

Provide test source code if related

Feature('login');

Scenario('test something', async ({ I }) => {
    await I.amOnPage('/')
});

Details

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: {
    WebDriver: {
      url: 'http://www.google.com',
      browser: 'chrome'
    }
  },
  include: {
    I: './steps_file'
  },
  name: 'codecept-js-huong',
  fullPromiseBased: true,
  plugins: {
        pauseOnFail: {},
        retryFailedStep: {
            enabled: true
        },
        tryTo: {
            enabled: true
        },
        screenshotOnFail: {
            enabled: true
        },
        wdio: {
            enabled: true,
            services: ['selenium-standalone']
        }
    }
}
esther-86 commented 1 year ago

ESM version: https://www.youtube.com/watch?v=sLW4YEl5IIA wdio/selenium-standalone-service uses ESM: https://openbase.com/js/@wdio/selenium-standalone-service (version 8)

Solution: Use "@wdio/selenium-standalone-service": "^7.30.0", "webdriverio": "^7.30.0"

esther-86 commented 1 year ago

I'm re-opening this because the above solution is actually just a workaround. I want to be able to use the latest wdio/selenium-standalone-service with webdriverio helper. I already tried to create a CodeceptJS project with TypeScript and set "type":"module" in package.json but I'm still getting this error (when I did something similar for webdriverio project without Codecept it was able to run)

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users//Desktop/Repositories/PoCs/codecept-ts-huong/codecept.conf.ts
require() of ES modules is not supported.
require() of /Users//Desktop/Repositories/PoCs/codecept-ts-huong/codecept.conf.ts from /Users//Desktop/Repositories/PoCs/codecept-ts-huong/node_modules/codeceptjs/lib/config.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users//Desktop/Repositories/PoCs/codecept-ts-huong/package.json.

    at createErrRequireEsm (/Users//Desktop/Repositories/PoCs/codecept-ts-huong/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/Users//Desktop/Repositories/PoCs/codecept-ts-huong/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/Users//Desktop/Repositories/PoCs/codecept-ts-huong/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Function.Module._load (node:internal/modules/cjs/loader:922:12)
    at Module.require (node:internal/modules/cjs/loader:1105:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at loadConfigFile (/Users//Desktop/Repositories/PoCs/codecept-ts-huong/node_modules/codeceptjs/lib/config.js:156:26)
    at Function.load (/Users//Desktop/Repositories/PoCs/codecept-ts-huong/node_modules/codeceptjs/lib/config.js:95:16)
    at module.exports.getConfig (/Users//Desktop/Repositories/PoCs/codecept-ts-huong/node_modules/codeceptjs/lib/command/utils.js:15:33)

I also took a look at these resources, and others, but am not understanding the problem, maybe because I am new to Javascript

https://stackoverflow.com/questions/69081410/error-err-require-esm-require-of-es-module-not-supported https://stackoverflow.com/questions/61401475/why-is-type-module-in-package-json-file https://codecept.discourse.group/t/selenium-standalone-service-plugin-can-not-working/236/2 https://github.com/standard-things/esm/issues/855

kobenguyent commented 1 year ago

ESM version: https://www.youtube.com/watch?v=sLW4YEl5IIA wdio/selenium-standalone-service uses ESM: https://openbase.com/js/@wdio/selenium-standalone-service (version 8)

Solution: Use "@wdio/selenium-standalone-service": "^7.30.0", "webdriverio": "^7.30.0"

"@wdio/selenium-standalone-service": "^7.30.0",
"webdriverio": "^8.3.5"

this combination also works

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 90 days with no activity.

vcua-mobify commented 9 months ago

Could this be reviewed? As of it's latest release @wdio/selenium-standalone-service is no longer being maintained. This is because WebdriverIO has moved to eliminate driver services as of WebdriverIO 8.14.

Instructions for setting up Webdriver should probably be updated to no longer use @wdio/selenium-standalone-service

kobenguyent commented 9 months ago

thank you @vcua-mobify once this https://github.com/codeceptjs/CodeceptJS/pull/4123 is merged, this won't be an issue anymore.