How to setup protractor to import AMD modules with requirejs

后端 未结 3 340
天涯浪人
天涯浪人 2021-01-14 14:01

I\'m trying to import an AMD module (ES6 module transpiled in ES5) in a protractor test. I\'m using the Page Object pattern. And the Page Object is the module I\'m trying to

相关标签:
3条回答
  • 2021-01-14 14:41

    I would recommand not to transpile first and then import, but just use ES6 in protractor. Put following at the top of the protractor.conf.js file. Now, you can just use import statements.

    For Babel 6 version put following at the top (or in onPrepare) of the protractor.conf.js:

    require("babel-core/register")({
        presets: [
            "es2015"
        ]
    });
    
    0 讨论(0)
  • 2021-01-14 14:41

    You can also use amdefine as said Martin, but without prepending each file with
    if (typeof define !== 'function') { var define = require('amdefine')(module) }

    Just include "amdefine": ">=0.1.0" in your devDependencies and add require('amdefine/intercept'); to onPrepare function in your protractor config. It will automatically insert the above snippet in each .js file loaded by Node.

    0 讨论(0)
  • 2021-01-14 14:57

    A solution is to use amdefine as it is described in requirejs.org/docs/node.html#3 the drawback of this solution is that you need to prepend every module by the following line :

    if (typeof define !== 'function') { var define = require('amdefine')(module) }
    

    In my specific case, because I'm using traceur to transpile ES6 files, I chose to use commonjs module instead of AMD for e2e tests. The reason it's different from unit tests executed by Karma (where I can easily use AMD) is the fact that protractor tests are executed by Node.js and not by the browser. So, I changed the traceur modules options for e2e tests only to this:

    {
          "modules": "commonjs",
          "script": false,
          "types": true,
          "annotations": true,
          "memberVariables":true,
          "outputLanguage": "es5"
    }
    
    0 讨论(0)
提交回复
热议问题