Proper usage of cucumber with typescript?

人走茶凉 提交于 2020-03-21 16:03:14

问题


I was following a tutorial but the setup is really bad. Basically it uses typescript to convert .ts files to .js. So basically pollutes your whole source code with .js files around.

So as soon as you import your .ts file from source code, all dependencies are duplicated with a .js file.

Do you know how to do proper typescript cucumber tests?

A hacky solution: Copy all features and all files to another temp folder, run from there. I would expect cucumber to be a bit more mature than this, hence my question here?

Or change the configuration of cucumber to look in the build folder from ts.

Thank you

Why just using typescript won't work:

Code structure:

  • tests
    • a.feature
    • stepDefinitions.ts

Now you will compile the typescript and have this structure:

  • tests
    • a.feature
    • stepDefinitions.ts
  • build
    • tests
      • stepDefinitions.js

Now you can see that stepDefinitions.js has no idea where to find a.feature. If you run cucumber on the build/test folder it won't find any step feature to run... because well, they are in the tests folder. So the hacky way to fix it is to copy over the features files resulting this structure:

  • tests
    • a.feature
    • stepDefinitions.ts
  • build
    • tests
      • a.feature
      • stepDefinitions.js

Now it will work but is hacky, I don't like it.


回答1:


Updated answer based on more info provided:

The first thing you should do is separate your features and steps into their own folders

 tests
  features
    a.feature
    b.feature
  stepDefinitions
    aStep.ts

Next, create a cucumber.js file which will be the cucumber profile. I use the following profile but it's up to you what you want to do

    var common = [
  `--format ${
    process.env.CI || !process.stdout.isTTY ? 'progress' : 'progress-bar'
    }`,
  '--format json:./reports/cucumber-json-reports/report.json',
  '--format rerun:@rerun.txt',
  '--format usage:usage.txt',
  '--parallel 20',
  '--require ./build/tests/stepDefinitions/**/*.js',
  '--require ./build/tests/stepDefinitions/*.js',
  '--require ./build/tests/support/*.js'
].join(' ');

module.exports = {
  default: common,
};

The above tells cucumber where your steps are. Now you can run something like the following from the root of the project

tsc && ./node_modules/.bin/cucumber-js ./tests/features/ -p default

This will

  1. Compile your code
  2. Run the cucumber-js library
  3. Run cucumber-js against the features folder
  4. Run cucumber-js against the features folder with the default profile that you built in your cucumber.js file



回答2:


You do not need to compile your code with tsc and then run cucumber on the compiled files, as suggested by Raymond Kelly. You can just run cucumber with the typescript transpiler using --require-module ts-node/register. Full example courtesy of hdorgeval at cucumber-ts-starter




回答3:


Putting cucumber tests in src and compiling it with tsc is not a really good practice, because it pollutes build directory which is not required for the software to run, especially in production.

Instead ts-node can be used. Run npm install ts-node --save-dev. Here is an example of cucumber-js configuration file (all .feature and step definition files are in /features folder in my setup):

module.exports = {
  default: [
    '--require-module ts-node/register',
    '--require ./features/**/*.ts',
    '--require ./features/*.ts',
  ].join(' '),
};



回答4:


I'm not following your question? Cucumber has nothing to do with compiling .ts files to .js files, that is typescript doing that. If you don't want the .ts and .js files in the same folder then you can add the following to your tsconfig.json file.

"outDir": "typeScript"

This will output the javascript files to the folder "typeScript" at the root of your project. I personally like to keep them together as it can be easier to debug. There is no way to get away from the .js files being created as they are what are used when running javascript.



来源:https://stackoverflow.com/questions/52940221/proper-usage-of-cucumber-with-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!