webdriverio-community / wdio-electron-service

WebdriverIO service for testing Electron applications
https://webdriver.io
MIT License
34 stars 15 forks source link

CJS output target depends on `@vitest/spy` which is ESM only #414

Closed christian-bromann closed 8 months ago

christian-bromann commented 8 months ago

Discussed in https://github.com/webdriverio-community/wdio-electron-service/discussions/413

Originally posted by **qwalley** February 5, 2024 Seems that vitest/spy does not enjoy being "require"d. Here's a minimal reproduction using the electron boilerplate project. Environment: Ubuntu 22 Steps: 1. `git clone https://github.com/webdriverio/electron-boilerplate.git` 2. `cd electron-boilerplate && npm install && npm run package` 3. `NODE_ENV=test ./out/WebdriverIO\ +\ Electron\ Boilerplate-linux-x64/WebdriverIO\ +\ Electron\ Boilerplate` Output: ``` A JavaScript error occurred in the main process Uncaught Exception: Error [ERR_REQUIRE_ESM]: require() of ES Module /home/walley/repos/electron-boilerplate/out/WebdriverIO + Electron Boilerplate-linux-x64/resources/app.asar/node_modules/@vitest/spy/dist/index.js from /home/walley/repos/electron-boilerplate/out/WebdriverIO + Electron Boilerplate-linux-x64/resources/app.asar/node_modules/wdio-electron-service/dist/cjs/main.js not supported. Instead change the require of index.js in /home/walley/repos/electron-boilerplate/out/WebdriverIO + Electron Boilerplate-linux-x64/resources/app.asar/node_modules/wdio-electron-service/dist/cjs/main.js to a dynamic import() which is available in all CommonJS modules. at l._load (node:electron/js2c/asar_bundle:2:13642) at Object. (/home/walley/repos/electron-boilerplate/out/WebdriverIO + Electron Boilerplate-linux-x64/resources/app.asar/node_modules/wdio-electron-service/dist/cjs/main.js:30:15) at l._load (node:electron/js2c/asar_bundle:2:13642) at Object. (/home/walley/repos/electron-boilerplate/out/WebdriverIO + Electron Boilerplate-linux-x64/resources/app.asar/dist/app.js:17:5) at l._load (node:electron/js2c/asar_bundle:2:13642) at node:electron/js2c/browser_init:2:120247 at node:electron/js2c/browser_init:2:120456 at node:electron/js2c/browser_init:2:120460 at l._load (node:electron/js2c/asar_bundle:2:13642) ```
MohammedChaibi commented 8 months ago

Hi! I'm facing the same problem with my project, is there any workaround to this problem? Thanks in advance :)

goosewobbler commented 8 months ago

I wonder how this passed the CJS E2Es...

goosewobbler commented 8 months ago

@MohammedChaibi we should have a release out soon to fix this.

MohammedChaibi commented 8 months ago

@goosewobbler That's great, thank you!

goosewobbler commented 8 months ago

@MohammedChaibi just updating here - the solution is likely more complicated than expected, it will take some time. You can work around this by either bundling your app in a similar way to that shown in the repo examples, or by downgrading to v5.4.0 - you will not be able to mock electron APIs if you downgrade.

https://github.com/webdriverio-community/wdio-electron-service/tree/main/example https://github.com/webdriverio-community/wdio-electron-service/tree/main/example-cjs https://github.com/webdriverio-community/wdio-electron-service/tree/main/example-electron-builder

Also you can watch #421 as the solution to the issue progresses.

MohammedChaibi commented 8 months ago

Thank you for the update! I will try your suggestions tomorrow and see what fits best :)