/vitest-cucumber

Use gherkin in your unit tests with vitest

Primary LanguageTypeScript

Overview

vitest-cucumber is an opiniated vitest tools (not a plugin) inspired by jest-cucumber.

Goal is to write unit test using Gherkin feature file and checking scenario name, missing scenario step etc.

Installation

npm install @amiceli/vitest-cucumber -D

It's enough you don't to add or update a config file.

Since v3.4.4 vitest-cucumber required vitest >=2.0.0.

Examples

You can take a look on :

How it works

Scenario function use vitest describe function and all steps function like Given, Then use vitest test function.

So you don't need to use it or test inside Scenario or scenario steps.

Usage

First write your feature file. By example :

Feature: Improve my unit tests
    Scenario: Use vitest-cucumber in my unit tests
        Given Developer using feature file
        And   Using vitest-cucumber
        When  I run my unit tests
        Then  I know if I forgot a scenario

Now you can write unit tests with vitest-cucumber.

Foe example :

import { loadFeature, describeFeature } from '@amiceli/vitest-cucumber'
import { expect } from 'vitest'

const feature = await loadFeature('path/to/my/file.feature')

describeFeature(feature, ({ Scenario }) => {
            
    Scenario('Use vitest-cucumber in my unit tests', ({ Given, When, Then, And }) => {
        Given('Developer using feature file', () => {
            expect(false).toBeFalsy()
        })
        And('sing vitest-cucumber', () => {
            // ...
        })
        When('I run my unit tests', () => {
            // ...
        })
        Then('I know if I forgot a scenario', () => {
            // ...
        })
    })

})

When you run your test with vitest, vitest-cucumber will check :

  • if you forget a Scenario or a Scenario Outline
  • if you use correct Scenario description
  • if you forgot a Scenario step
  • if you use a wrong Scenario step type
  • missing variables value in Scenario Outline
  • missing variables name in Scenario Outline steps

For example, if you forgot to write :

When('I run my unit tests', () => {
    // ...
})

It will throw When I run my unit tests was not called.

Generate spec file from feature file

Since 3.4.1 vitest-cucumber provide a script to generate spec file from feature file.

You can use it like this :

npx @amiceli/vitest-cucumber <path-to-feature> <path-to-spec>

An example :

npx @amiceli/vitest-cucumber features/example.feature src/__tests__/example.spec.ts

You just have to format spec file after this script ;).

Currently it generates TS file, if you need more options open an issue ;).

Doc is maintain in this project vitest-cucumber-docs.

Don't hesitate to open an issue on it if you want more details ;).