1

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.

  • You can verify if the same module is present under npm_dependencies in your browserstack.json config file. If it is there maybe reach out to BrowserStack support – Erric Jul 05 '22 at 03:02

2 Answers2

2

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.

0

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:

import '../../../node_modules/cypress-wait-util';

You can explore - https://nextjs.org/examples for examples using nextJS

rlandster
  • 7,294
  • 14
  • 58
  • 96
HomeLander
  • 61
  • 3
  • you can also check these resources - https://medium.com/geekculture/component-testing-next-js-application-with-cypress-28fa311adda6, https://imranhsayed.medium.com/using-cypress-with-next-js-for-end-to-end-testing-cypress-nextjs-example-af100609cf16 – HomeLander Jul 07 '22 at 03:36
  • You may also take reference from this GitHub repository - https://github.com/bahmutov/next-and-cypress-example – HomeLander Jul 11 '22 at 02:29
  • Thank you for the message! I tried without Browserstack, and on my local machine it s working just fine... I have the problem only when I "upload" the tests into browserstack and try to run them from there – Florin Trandafir Jul 11 '22 at 08:11
  • Then I suggest reaching out to BS support; maybe they can help you. – HomeLander Jul 13 '22 at 09:44