2

Is it possible to use both in e2e tests? I can use both in Vue. Those js files are in myproject/src/js folder. But in myproject/test/e2e, I know that Nightwatch use node which can only use require syntax, but I can rename the file to mjs, which then support import syntax, but if I try to import mjs file in my other test by using require, it gives Error: Must use import to load ES Module error....I am wondering if this is possible. Just like what I can use in my Vue src/*, where js files have mixed use of require/import everywhere.

I have tried some presets in babel.config.js

module.exports = {
  presets: [
    ["@babel/preset-env"],
    // I tried below as well
    [
      "@vue/cli-plugin-babel/preset",
      {
        useBuiltIns: "entry",
       }
    ],
  ]
}

I have those babel dependencies in package.json

devDependencies {
    "@babel/plugin-proposal-optional-chaining": "^7.2.0",
    "babel-core": "7.0.0-bridge.0",
    "babel-eslint": "^10.0.3",
    "babel-helper-vue-jsx-merge-props": "^2.0.3",
    "babel-jest": "^23.6.0",
    "babel-loader": "^7.1.1",
    "babel-plugin-add-module-exports": "^0.2.1",
    "babel-plugin-dynamic-import-node": "^1.2.0",
    "babel-plugin-syntax-jsx": "^6.18.0",
    "babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-vue-jsx": "^3.5.0",
    "babel-preset-env": "^1.3.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-2": "^6.22.0",
    "babel-register": "^6.22.0",
}

In nightwatch.conf.js

require('babel-register')() // no differences if i add this or not..
module.exports = {
  presets: [
    ["es2015", {"modules": false}] // set modules to true or false dose not help..
  ]
}

I have been struggling with this for days...please help me..

Arst
  • 3,098
  • 1
  • 35
  • 42
  • Does this answer your question https://stackoverflow.com/questions/42007248/how-to-make-es6-import-export-works-in-nightwatch-test – Serhii Yukhnevych Aug 13 '20 at 09:53
  • Unfortunately it dose not...and the post was 2017...it seems now the babel recommends to use "@babel/preset-env".. – Arst Aug 13 '20 at 11:58
  • Here is something similar https://github.com/nightwatchjs/nightwatch/issues/2038 – Serhii Yukhnevych Aug 13 '20 at 12:20
  • Actually I updated my question..I just found if I rename the file to mjs, it can use import. but then it cannot be required by normal js file. get another error... – Arst Aug 13 '20 at 12:22
  • ok, so the issue you are encountering is not with `nightwatch` it's with `node.js` runner. Cause the code the your are running is executed within `node.js` context. IMO it better just to use `require('moduleName')` if you are struggling so much with enabling `import/export` – Serhii Yukhnevych Aug 13 '20 at 14:09

0 Answers0