Proper usage of cucumber with typescript?
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(' '),
};
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
Edit: original template mentioned was improved with new version cucumber7-ts-starter
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
- Compile your code
- Run the cucumber-js library
- Run cucumber-js against the features folder
- Run cucumber-js against the features folder with the default profile that you built in your cucumber.js file
Edit: See https://stackoverflow.com/a/55378059/3323395 for how to do this without needing to compile first