How to format Gherkin files?

Benjamin Dumont
Just-Tech-IT
Published in
2 min readMar 24, 2021

This small story is about a tool I discovered recently that save me so much time. There may be different reasons why you have malformed Gherkin files:

  • Developper is to lazy to indent it
  • Lack of tool that helps writing Gherkin
  • Auto-generated files

So, I realize that I spent too much time on this task and I discovered the following tool that helps me.

Ghokin

Ghokin format and apply transformation on gherkin files.

Ghokin is able to format or check if the Gherkin file is well formatted:

  check       Check a file/folder is well formatted
fmt Format a feature file/folder
help Help about any command

So, after testing the library, I decided to write a script that executes this task for me.

Setting up the script

At the root folder of my repository, I created a directory called “tools”:

mkdir tools

Then, I downloaded the binary of the library using this link, renamed the library into “ghokin” and put it in the tools folder.

Now, I can create my script called gherkin-formatter.sh into this folder. Make sure the two files have execution rights using the following command line for instance:

sudo chmod +x gherkin-formatter.sh ghokin

And here’s the content of my bash file:

#!/bin/bashcurrentDirectory=$(pwd)cd $(dirname `which $0`)for file in $(find .. -type f -name *.feature)
do
./ghokin fmt replace ${file}
done
cd ${currentDirectory}

As you can see, I list all the files with a feature extension to apply Ghokin on it.

Where to go from here

This task stays manual since the developper has to run the script. So it’s time to automate it in one of these steps:

  • During build phases or pre-build phases (using tools like Grunt for instance or in compilation phase)
  • Before a commit (using pre-commit hook like I did in this article about Swiftformat)

--

--