Writing Expressive Tests with Gherkin and Vitest in TypeScript
When writing tests, clarity is key. Behaviour-Driven Development (BDD) helps us write tests that read like specifications, making them easier to understand and maintain. In this post, we’ll explore how to write expressive tests using Gherkin syntax with Vitest and TypeScript, and see how AI coding assistants like Cursor can understand and help us write these tests more efficiently.
What is Gherkin?
Gherkin is a plain-text language that uses a simple set of keywords to describe software behaviour. The most common keywords are:
Given: Set up the initial context
When: Describe an action
Then: Assert the expected outcome
Here are some practical examples of Gherkin syntax in action:
Basic Example
Example with Background
Example with Scenario Outline
These Gherkin scenarios can be translated into Vitest tests using descriptive comments that follow the same pattern. This makes our tests both human-readable and AI-friendly, as tools like Cursor can understand the intent behind each step.
Setting Up Our Test Environment
First, let’s set up a simple project with Vitest and TypeScript. We’ll need to install the necessary dependencies including Vitest, TypeScript, and the Node.js type definitions. Once set up, we can start writing our tests using the Gherkin syntax.
Create a vitest.config.ts:
Writing Our First Gherkin-Style Test
To demonstrate how to write Gherkin-style tests, we’ll create a simple shopping basket calculator. This example will show how to structure tests that are both readable and maintainable. The key is to use clear, descriptive comments that follow the Given-When-Then pattern.
Adding More Complex Scenarios
As our applications grow, we often need to test more complex scenarios. We’ll extend our shopping basket example to handle discounts, showing how Gherkin-style tests can clearly describe business rules and edge cases.
Here are our tests for the discount functionality:
Working with Tabular Data
Gherkin’s Scenario Outline feature is particularly useful when testing multiple variations of the same behavior. Let’s extend our shopping basket example to handle bulk discounts using tabular data:
Here’s how we can implement this test using Vitest:
And here’s the corresponding implementation:
This approach makes it easy to test multiple scenarios without duplicating code. The tabular format clearly shows the relationship between inputs and expected outputs, making the tests more maintainable and easier to update when business rules change.
How Cursor Helps
The beauty of writing tests in this way is that AI coding assistants like Cursor can understand the context and flow of your tests. When you write comments using Gherkin syntax, Cursor can:
Suggest appropriate test implementations based on your Given-When-Then statements
Help complete test scenarios based on similar patterns
Understand the business logic and suggest edge cases you might have missed
Best Practices
When writing Gherkin-style tests:
Keep scenarios focused and specific
Use clear, business-oriented language
Maintain consistency in your Given-When-Then structure
Add context through descriptive test names
Group related scenarios in describe blocks
Conclusion
Using Gherkin syntax in your Vitest tests not only makes them more readable and maintainable but also helps AI tools like Cursor understand your testing intentions better. This leads to more efficient test writing and better collaboration between developers and AI assistants.
Remember, the goal is to write tests that serve as living documentation of your system’s behaviour. By following this approach, you create tests that are easy to understand, maintain and extend.