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

Unable to import ESM-only modules in a test #6853

Closed
mkochel-shell opened this issue Feb 7, 2022 · 10 comments · Fixed by #7411
Closed

Unable to import ESM-only modules in a test #6853

mkochel-shell opened this issue Feb 7, 2022 · 10 comments · Fixed by #7411
Assignees
Labels
FREQUENCY: level 2 STATE: No updates No updates are available at this point. TYPE: enhancement The accepted proposal for future implementation.

Comments

@mkochel-shell
Copy link

What is your Scenario?

Importing ESM-only modules in a test throws ERR_REQUIRE_ESM error.

What is the Current behavior?

An error is thrown:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: node_modules\node-fetch\src\index.js
require() of ES modules is not supported.
require() of node_modules\node-fetch\src\index.js from test.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 node_modules\node-fetch\package.json.

    at Object.<anonymous> (test.js:3:1)

What is the Expected behavior?

Test executes correctly.

What is your public website URL? (or attach your complete example)

Please see the isolated test code below.

What is your TestCafe test code?

// Make sure node-fetch 3.2.0 is installed.
// It is an ESM-only module.
import fetch from 'node-fetch' // The test will fail at this line.

fixture`ESM-only module import error`

test('Will not run', () => {
  console.log('This code will not run')
})

Your complete configuration file

No specific configuration file.

Your complete test report

Please refer to the error message above.

Screenshots

Not applicable.

Steps to Reproduce

Please install node-fetch 3.2.0 and run the attached test code.

TestCafe version

1.18.3

Node.js version

14.17.1

Command-line arguments

testcafe chrome test.js

Browser name(s) and version(s)

chrome

Platform(s) and version(s)

Windows 11

Other

The test code runs with node-fetch 2.6.7, which supports require(). It seems that import statements are transpiled to require calls, which fails for all ESM-only modules.

@mkochel-shell mkochel-shell added the TYPE: bug The described behavior is considered as wrong (bug). label Feb 7, 2022
@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Feb 7, 2022
@AlexKamaev
Copy link
Contributor

The test code runs with node-fetch 2.6.7, which supports require(). It seems that import statements are transpiled to require calls, which fails for all ESM-only modules.

You are right. TestCafe transpiles imports to require calls and this leads to the issue. Thank you for reporting this.

@need-response-app need-response-app bot removed the STATE: Need response An issue that requires a response or attention from the team. label Feb 8, 2022
@AlexKamaev AlexKamaev added TYPE: enhancement The accepted proposal for future implementation. FREQUENCY: level 1 and removed TYPE: bug The described behavior is considered as wrong (bug). labels Feb 8, 2022
@puneetpunj
Copy link

Any update on this issue? this is happening with @percy/testcafe package as well.

@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Apr 6, 2022
@AlexKamaev AlexKamaev added STATE: No updates No updates are available at this point. and removed STATE: Need response An issue that requires a response or attention from the team. labels Apr 6, 2022
@testcafe-build-bot
Copy link
Collaborator

No updates yet. Once we get any results, we will post them in this thread.

@josephgroark
Copy link

Any updates on this issue?

@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Apr 12, 2022
@thelazurite-cell
Copy link

Created bug reproduction project:
tc-esm.zip

@Aleksey28
Copy link
Collaborator

Thank you for the example. We do not have news on this. We will update this thread once we have any progress.

@need-response-app need-response-app bot removed the STATE: Need response An issue that requires a response or attention from the team. label Apr 13, 2022
@mineshh
Copy link

mineshh commented Apr 13, 2022

I've recently witnessed the same. Although I think this was working for me before...

@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Apr 13, 2022
@Aleksey28 Aleksey28 removed the STATE: Need response An issue that requires a response or attention from the team. label Apr 15, 2022
@smellai
Copy link

smellai commented May 24, 2022

I also have this issue

@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label May 24, 2022
@Aleksey28 Aleksey28 removed the STATE: Need response An issue that requires a response or attention from the team. label May 25, 2022
@bvklingeren
Copy link

bvklingeren commented Jun 24, 2022

We are experiencing this issue as well. I have just upgraded our Angular project from 12 to 13 and we are using the Angular Material Harness functionality in our e2e tests, so we would directly use the Material library in ESM format, which is not possible now.

I understand fixing this issue might take a considerable amount of time, therefore I would like to know if the issue is currently being investigated/developed or not, so I can make a decision on how to go forward..

@need-response-app need-response-app bot added the STATE: Need response An issue that requires a response or attention from the team. label Jun 24, 2022
@AlexKamaev
Copy link
Contributor

@bvklingeren,
You are right, a fix for this issue might take significant time. We did not look into it yet. We have plans to research it, but I cannot give time estimates right now, unfortunately.

@need-response-app need-response-app bot removed the STATE: Need response An issue that requires a response or attention from the team. label Jun 27, 2022
Aleksey28 added a commit that referenced this issue Jan 13, 2023
<!--
Thank you for your contribution.

Before making a PR, please read our contributing guidelines at

https://github.com/DevExpress/testcafe/blob/master/CONTRIBUTING.md#code-contribution

We recommend creating a *draft* PR, so that you can mark it as 'ready
for review' when you are done.
-->

## Purpose
Add the possibility to run tests in ESM mode

## Approach
- [X] Research ways to implement running ESM files consistently
- [X] Research ways to compile test files on the run
- [X] Add loader hooks for ESM
- [X] Run tests with dynamic import
- [x] Fix tests
- [X] Add handling error `ERR_REQUIRE_ESM`
- [X] Add workflow for running tests in ESM
- [x] Add additional tests

## References
Closes #6853
PR in `bin-v8-flags-filter`
DevExpress/bin-v8-flags-filter#1
PR in `callsite-record`
inikulin/callsite-record#28
Temp `bin-v8-flags-filter` assembling
[bin-v8-flags-filter-1.2.0.zip](https://github.com/DevExpress/testcafe/files/10175237/bin-v8-flags-filter-1.2.0.zip)
Temp `callsite-record` assembling
[callsite-record-4.1.3.zip](https://github.com/DevExpress/testcafe/files/10175241/callsite-record-4.1.3.zip)

## Pre-Merge TODO
- [x] Write tests for your proposed changes
- [x] Make sure that existing tests do not fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FREQUENCY: level 2 STATE: No updates No updates are available at this point. TYPE: enhancement The accepted proposal for future implementation.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants