-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
Context:
- GOOD Playwright Version: 1.19.2
- BAD Playwright Version: 1.20.0
- Operating System: Windows
Code Snippet
https://github.com/henrikstorck/playwright-test
Describe the bug
When running npm test in the above repository, I get the following error message with playwright v1.20:
> npm test
Running 0 test using 0 worker
Error [ERR_REQUIRE_ESM]: require() of ES Module D:\Projects\playwright-test\tests\test.js from D:\Projects\playwright-test\node_modules\@playwright\test\lib\loader.js not supported.
Instead change the require of test.js in D:\Projects\playwright-test\node_modules\@playwright\test\lib\loader.js to a dynamic import() which is available in all CommonJS modules.
at node_modules\@playwright\test\lib\loader.js:244
242 |
243 | if (isModule) return await esmImport();
> 244 | return require(file);
| ^
245 | } catch (error) {
246 | if (error.code === 'ERR_MODULE_NOT_FOUND' && error.message.includes('Did you mean to import')) {
247 | var _$exec;
at Loader._requireOrImport (D:\Projects\playwright-test\node_modules\@playwright\test\lib\loader.js:244:14)
at Loader.loadTestFile (D:\Projects\playwright-test\node_modules\@playwright\test\lib\loader.js:142:18)
at Runner._runFiles (D:\Projects\playwright-test\node_modules\@playwright\test\lib\runner.js:295:44)
at Runner._run (D:\Projects\playwright-test\node_modules\@playwright\test\lib\runner.js:231:23)
at async TimeoutRunner.run (D:\Projects\playwright-test\node_modules\playwright-core\lib\utils\async.js:38:14)
at async raceAgainstTimeout (D:\Projects\playwright-test\node_modules\playwright-core\lib\utils\async.js:85:15)
at async Runner.runAllTests (D:\Projects\playwright-test\node_modules\@playwright\test\lib\runner.js:188:20)
at async runTests (D:\Projects\playwright-test\node_modules\@playwright\test\lib\cli.js:184:18)
at async Command.<anonymous> (D:\Projects\playwright-test\node_modules\@playwright\test\lib\cli.js:79:7)
=================
no tests found.
=================
Since updating to playwright v1.20, playwright appears to no longer detect tests in nested directories as part of ES modules, which causes the test execution to fail because playwright loads the tests with require() instead of import.
When downgrading to v1.19, the test gets detected and executed successfully as expected.
I'm assuming that folderIsModule is broken in v1.20. (possibly caused by #12292)
When debugging the function, it appears as if it is not able to iterate through the parent directories. Because of that it can't find the package.json with the "type": "module" and therefore doesn't detect if the file is part of an ES module.
If my assumption is right, changing path.basename in the folderIsModule function to path.dirname should allow folderIsModule to correctly iterate through it's parent directories and find the package.json.
Reproducable on local Windows machine and ubuntu runner for github action.