webdriverio icon indicating copy to clipboard operation
webdriverio copied to clipboard

[🐛 Bug]: CJS require of @wdio/cli and @wdio/reporter not implemented

Open vbyjsue opened this issue 3 years ago • 28 comments

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

8.4

Node.js Version

16.13.2

Mode

WDIO Testrunner

Which capabilities are you using?

No response

What happened?

Tests are written in a cjs project with the testrunner started programmatically. When upgrading to v8 some wdio modules are no longer possible to require.

const Launcher = require('@wdio/cli').default
const WDIOReporter = require('@wdio/reporter').default;

fails since they only support esm import. A temporary workaround is to use const { Launcher } = await import('@wdio/cli'); but in the case of @wdio/reporter that is a bit trickier.

What is your expected behavior?

To be able to use

const Launcher = require('@wdio/cli').default
const WDIOReporter = require('@wdio/reporter').default;

How to reproduce the bug.

Use

require('@wdio/cli').default

or

require('@wdio/reporter').default

in v8

Relevant log output

Launcher failed to start the test Error [ERR_REQUIRE_ESM]: require() of ES Module

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • [X] I have searched the existing issues

vbyjsue avatar Mar 01 '23 09:03 vbyjsue

Thanks for reporting!

I would suggest to for the @wdio/cli package to do the same approach as we have for others which is create a cjs directory where we re-export the same interface in CJS by dynamically importing the ESM module.

For @wdio/reporter I don't see a reason why it should be ported back to CJS given that you only need to import it for custom reporter which can easily be transformed to ESM without having to transform all the other code.

Any contributions that resolves the bug are highly appreciated. Please take a look into our contribution guidelines and let us know if you have any questions. Cheers!

christian-bromann avatar Mar 01 '23 15:03 christian-bromann

@christian-bromann I found the same apply to the @wdio/logger. We are using the @wdio/logger as part of a custom service. Do you recommend to wrap the @wdio/logger in a similar fashion than @wdio/cli? I think the main reason why it failed it's because of the top-level await here: https://github.com/webdriverio/webdriverio/blob/c58439ce91910803b53b0337ac01e3b973350925/packages/wdio-logger/src/index.ts#L11

olivier-martin-sf avatar Apr 05 '23 16:04 olivier-martin-sf

The logger would need to have a separate tsconfig.cjs.json that compiles the whole package as CJS artifact. This will be difficult though because we use a top level await in the package. Using a CJS fork or the v7 package would not help because you would end up having two logger packages trying to write to the same files and overwrite each others content. We really recommend to upgrade your tests (not whole project, only e2e tests) to ESM.

christian-bromann avatar Apr 05 '23 16:04 christian-bromann

Thank you! So it would mean that the consumer of our service would have to opt-in their tests to ESM?

olivier-martin-sf avatar Apr 05 '23 16:04 olivier-martin-sf

Users should be able to run CJS tests with WebdriverIO v8 even if all WebdriverIO service packages are written in ESM.

christian-bromann avatar Apr 05 '23 17:04 christian-bromann

I've tried to bump the WebdriverIO dependencies to v8 in the service that I am working on (the only direct dependency that we have is the @wdio/logger). Our WebdriverIO service is written in TypeScript and compiled down as CJS as most of our consumers codebases are still using CJS.

I've then try to use that service in a repo that uses WebdriverIO v8 & CJS and it fails:

2023-04-05T17:10:25.119Z ERROR @wdio/config:ConfigParser: Failed loading configuration file: file:///Users/john.doe/project/packages/integration-wdio/wdio.conf.js: require() of ES Module /Users/john.doe/project/node_modules/@wdio/logger/build/index.js from /Users/john.doe/project/packages/wdio-project-service/build/adapters/mobile-driver-adapter.js not supported.
Instead change the require of index.js in /Users/john.doe/project/packages/wdio-project-service/build/adapters/mobile-driver-adapter.js to a dynamic import() which is available in all CommonJS modules.

That's how TypeScript is compiling the logger import and what's causing the issue:

const logger_1 = __importDefault(require("@wdio/logger"));

Am I missing something? I am talking from the perspective of a custom service author that push a package on npm

olivier-martin-sf avatar Apr 05 '23 17:04 olivier-martin-sf

Our WebdriverIO service is written in TypeScript and compiled down as CJS as most of our consumers codebases are still using CJS.

I recommend have the service create artifacts for ESM and CJS so it can be potentially used for v7 CJS users as well as v8 ESM ones. Have a close look how some of our other service do this. You basically write your service in ESM and have a CJS file that imports the ESM package and just re-exports it.

christian-bromann avatar Apr 05 '23 17:04 christian-bromann

Keeping this open as we still should provide a CJS artifact for @wdio/reporter

christian-bromann avatar Apr 14 '23 19:04 christian-bromann

Actually thinking about this another time, it doens't make sense to have a CJS artifact for @wdio/reporter. If you want to build a reporter that works for both v7 and v8 and uses CJS you should build it on top of the last v7 version of @wdio/reporter. Unless there are any other reasons why a CJS version of this package is required I am gonna go close this.

Thanks @DirkoOdendaal for providing a patch for the @wdio/cli package.

christian-bromann avatar Apr 29 '23 19:04 christian-bromann

Hi

Just wondering if any additional changes needed to be made in order to get this to work post the fix? I'm using v8.10.2, but

const Launcher = require('@wdio/cli').default;
...
const wdio = new Launcher( ... );

throws a TypeError: Launcher is not a constructor error for me.

Thanks Ashil

ashil88 avatar May 11 '23 15:05 ashil88

Hi @ashil88 ,

I found a bug that I'm working on fixing, but I doubt that it's the error you are getting. Do you have a sample project where I can see the error?

You could also try: const {Launcher} = require('@wdio/cli');

Don't believe that @wdio/cli has a default export and it only exposes named exports.

DirkoOdendaal avatar May 11 '23 20:05 DirkoOdendaal

And you have tried the proposed workaround? const { Launcher } = await import('@wdio/cli');

vbyjsue avatar May 12 '23 07:05 vbyjsue

Hi @DirkoOdendaal

Thanks, const {Launcher} = require('@wdio/cli'); seems to have moved things on a bit. I can create a new instance of Launcher, but now getting a error with run. In \node_modules\@wdio\cli\build\cjs\index.js it seems that this.#esmLauncher isn't defined for me in the run() function; possibly there's an issue with the import on line 11. Will keep trying things and report back if I find a solution.

Thanks Ashil

ashil88 avatar May 12 '23 17:05 ashil88

Hi @ashil88 , That is exactly the bug I'm working on. This is a timing issue and as a workaround you can add a timeout just before the run(). The issue is that the constructor does an async call, but the constructor can't be awaited. So working on moving the async call for now.

Hope that helps you!

DirkoOdendaal avatar May 15 '23 11:05 DirkoOdendaal

Hello @DirkoOdendaal , I guess i'm facing the same issue with Lancher from @wdio/cli when calling run() ; adding a timeout indeed worked as a workaround ; however is there any update on the bug mentioned ? Could you please give us a link to follow progression ? Thanks a lot ! regards

franck-jude avatar Jun 06 '23 14:06 franck-jude

Hello @DirkoOdendaal , The issue is not fixed yet. The following line of code doesn't launch the runner because of null value for 'esmLauncher'. https://github.com/webdriverio/webdriverio/blob/main/packages/wdio-cli/src/cjs/index.ts#LL20C8-L20C8 As a workaround dynamic import for '@wdio/cli' worked. const { Launcher } = await import('@wdio/cli');

But the issue with the CJS module is not fixed. Can you please give us an update on this ? Thanks.

ShradhaSangtani avatar Jun 20 '23 08:06 ShradhaSangtani

A new PR with a fix was raised in #10607

christian-bromann avatar Jun 22 '23 07:06 christian-bromann

I'm on webdriverio version 8.13.1 and seeing this problem with requiring wdio-video-reporter. It seems like this should already be fixed by that latest release, unless I'm missing something?

kit-johnson avatar Jul 16 '23 05:07 kit-johnson

The @wdio/reporter package is still ESM only.

christian-bromann avatar Jul 16 '23 17:07 christian-bromann

In 7.x I was able to write my config file in TS and import my custom reporter written in TS all using the same options as my tests written in TS thanks ts-node and smart engineering. Now this 'internal' decision breaks two of those at a time. I see authors using sneaky tricks like importing .js files in TS to avoid proper imports. advocating top-level awaits or changing entire project module types in package.js to get things going again. This is a community friction issue that isn't going to go away. 8.0 has been out for a year and you're getting lots of issues.

ForesightImaging avatar Aug 08 '23 21:08 ForesightImaging