Browserstack:Next.js Cypress E2E测试无法解析相对导入
我正在使用Cypress'NextJS插件,当我尝试通过Browserstack运行任何测试时,尝试通过相对路径导入模块时会出现以下错误。
Oops...we found an error preparing this test file:
> cypress/integration/happy-paths/modules/promo-card-page-automation-checks.spec.js
The error was:
Error: Webpack Compilation Error
./cypress/integration/happy-paths/modules/promo-card-page-automation-checks.spec.js
Module not found: Error: Can't resolve '../../../../services/i18n' in '/e2e/cypress/integration/happy-paths/modules'
Looked for and couldn't find the file at the following paths:
[/e2e/services/i18n]
[/e2e/services/i18n.js]
[/e2e/services/i18n.json]
[/e2e/services/i18n.jsx]
[/e2e/services/i18n.mjs]
[/e2e/services/i18n.coffee]
[/e2e/services/i18n/index]
[/e2e/services/i18n/index.js]
[/e2e/services/i18n/index.json]
[/e2e/services/i18n/index.jsx]
[/e2e/services/i18n/index.mjs]
[/e2e/services/i18n/index.coffee]
@ ./cypress/integration/happy-paths/modules/promo-card-page-automation-checks.spec.js 5:35-71
我尝试修改插件/index.js文件,以便在全球导入I18N,但没有成功。 导入模块的指定相对路径是正确的,因为当我对本地进行测试时,它们可以正常工作,但是当我尝试在browserstack中运行它们时,它们会失败,而上面的错误。
I’m using Cypress’ nextjs plugin, and when I try to run any test via Browserstack, I get the following error when trying to import modules via relative paths.
Oops...we found an error preparing this test file:
> cypress/integration/happy-paths/modules/promo-card-page-automation-checks.spec.js
The error was:
Error: Webpack Compilation Error
./cypress/integration/happy-paths/modules/promo-card-page-automation-checks.spec.js
Module not found: Error: Can't resolve '../../../../services/i18n' in '/e2e/cypress/integration/happy-paths/modules'
Looked for and couldn't find the file at the following paths:
[/e2e/services/i18n]
[/e2e/services/i18n.js]
[/e2e/services/i18n.json]
[/e2e/services/i18n.jsx]
[/e2e/services/i18n.mjs]
[/e2e/services/i18n.coffee]
[/e2e/services/i18n/index]
[/e2e/services/i18n/index.js]
[/e2e/services/i18n/index.json]
[/e2e/services/i18n/index.jsx]
[/e2e/services/i18n/index.mjs]
[/e2e/services/i18n/index.coffee]
@ ./cypress/integration/happy-paths/modules/promo-card-page-automation-checks.spec.js 5:35-71
I’ve tried to modify my plugins/index.js file in order to import i18n globally, with no success.
The specified relative path of the imported module is correct, because when I ran tests on local they work, but when I try to run them in Browserstack, they fail with the error from above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Browserstack支持团队的回应回来了,该团队解决了上述问题。
browserstack.json配置文件应包括项目本身需要构建和运行的所有npm_deppentecies,因此,我添加了来自DevDepentencies的所有软件包,以及从依赖项中添加了一些来自browserstack.json配置中的npm_depentencies。摆脱错误。
Note :请注意,您的项目所需的任何包都必须包含在您的browserstack.json配置文件中的NPM_Depentencies中,以便在启动测试之前将它们安装在我们的终端机器中。
Coming back with a response from Browserstack support team that solved the above issue.
The browserstack.json config file should include all the npm_dependecies that the project itself needs to build and run, therefore, I added all the packages from devDependencies and a few from the dependencies to the npm_dependencies in browserstack.json config file and I was able to get rid of the error.
Note: Please note that whatever package is required by your project has to be included in the npm_dependencies in your browserstack.json config file so that all of them are installed in our terminal machine before the test is initiated.
请在无浏览的情况下尝试,如果您也会得到与插件相关的相同错误。
另外,您可以尝试为Node_Modules中安装的软件包指定正确的URL。喜欢:
您可以探索 - https://nextjs.org/examples 使用nextjs示例
Please try without Browserstack, if then also you get the same error it's related to the plugin.
Also you can try specify the correct URL for the package installed inside node_modules. like:
You can explore - https://nextjs.org/examples for examples using nextJS