Step definitions and support files can be written in syntax/language that compiles to JavaScript, and just-in-time compiled when you run Cucumber.
For example, you might want to use Babel:
- In a configuration file
{ requireModule: ['@babel/register'] }
- On the CLI
$ cucumber-js --require-module @babel/register
This would mean any support code loaded with the require
option would be transpiled first.
Your tsconfig.json
should have these compilerOptions
on:
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
Other than that, a pretty standard TypeScript setup should work as expected.
You'll also need to specify where your support code is, since .ts
files won't be picked up by default.
If you are using ts-node:
- In a configuration file
{ requireModule: ['ts-node/register'], require: ['step-definitions/**/*.ts'] }
- On the CLI
$ cucumber-js --require-module ts-node/register --require 'step-definitions/**/*.ts'
For ESM projects, you can use ts-node
's ESM loader and then import
your TypeScript files:
$ NODE_OPTIONS="--loader ts-node/esm" cucumber-js --import 'step-definitions/**/*.ts'
Don't forget to set your tsconfig.json
to emit JavaScript with import
and export
statements:
{
"compilerOptions": {
"module": "esnext"
}
}
If you are using babel with @babel/preset-typescript:
- In a configuration file
{ requireModule: ['@babel/register'], require: ['step-definitions/**/*.ts'] }
- On the CLI
$ cucumber-js --require-module @babel/register --require 'step-definitions/**/*.ts'
See ESM for general advice on using loaders for transpilation in ESM projects.