3

I used to compile e2e protractor tests written in TS before run, but now I figured out how to compile ts files in run-time -- ts-node appeared to be a great tool for that. As many articles say, I registered ts-node in protractor config file, so that I can run my test specs as if they are .ts files and they will be compiled in tun-time. Something like this:

beforeLaunch: () => {
        require('ts-node').register({
            compilerOptions: {
                module: 'commonjs'
            },
            disableWarnings: true,
            fast: true
          });
    },

That's great. But all cases I met included examples with .js config files --> you don't need to compile config files, but all specs can be in TS and will be compiled with ts-node. What I wish to have is having all my files in TypeScript: both configs and specs. How can I run my protractor tests giving config.ts and having specs.ts in there? Something like protractor ts-node config.ts would be just awesome.

  • Can I ask why you want your Protractor config in TS? I don't think the Protractor config file generally needs to use any of the other code in your project (or at least I haven't needed to yet?) and if you just want your editor's language service to get typing information, you can use `/** @type {import("protractor").Config} */` in your (JS) config declaration. – Coderer Oct 02 '20 at 10:22

3 Answers3

3

Just create another .js file that will have following

launcher.js

        require('ts-node').register({
            compilerOptions: {
                module: 'commonjs'
            },
            disableWarnings: true,
            fast: true
        });
        module.export.config = require('./protractor.conf.ts').config;   

Then run protractor and pass launcher as config file:

protractor launcher.js
Xotabu4
  • 3,063
  • 17
  • 29
1

Add the below code in your onPrepare() of config.js

  onPrepare() {
    /* Compile TS files */
    require('ts-node').register({
      project: './tsconfig.json'
    });

Hope it helps you

Madhan Raj
  • 1,404
  • 1
  • 7
  • 13
  • I don't have any config.js in my project, all my config are in typescript and would like to keep them like that. I have many dependencies like helper classes etc. in .ts, so I wouldn't rather consider shift all configs in JavaScript – Kristina Tregubova Feb 17 '20 at 13:40
1

Based on Madhan's answer and the question in Can I access parameters in my protractor configuration file?, my solution is a single launcher JS file that can launch any TS config file passed via command line: --params.tsconfigpath=myfile.ts.

var getTsConfigPath = function() {
  for (var i = 3; i < process.argv.length; i++) {
    var match = process.argv[i].match(/^--params\.([^=]+)=(.*)$/);
    if (match && match[1] === "tsconfigpath") 
      return match[2];
  }
  return "protractor.conf.ts";
};

require("ts-node").register({
    project:  require("path").join(__dirname, "./tsconfig.json")
  });

tscconfigpath = require("path").join(__dirname, getTsConfigPath());
console.info("Starting protractor with TS config file '" + tscconfigpath + "'...");

module.exports.config = require(tscconfigpath).config;