Skip to content

Use vitest-cucumber without feature file

defineFeature works like describeFeature but without feature file. You can use function like Scenario, Background, Given etc just to organize your tests.

ScenarioOutline isn’t available in defineFeature because you don’t have feature file so no Examples.

Maybe it will be addedd in next version.

Other features / checks like same step, same scenario etc are present in defineFeature.

import { describe, expect, vi } from 'vitest'
import { defineFeature } from '../define-feature'
defineFeature('Define feature without Gherkin', (f) => {
let count: number = 0
const testFn = vi.fn()
f.AfterEachScenario(() => {
vi.resetAllMocks()
})
f.BeforeEachScenario(() => {
count += 1
})
f.Background((b) => {
b.Given('I love Background', () => {
testFn()
})
})
f.Scenario('Use Scenario', (s) => {
s.context.details = ''
s.Given('I use scenario', () => {
expect(s.context.details).toEqual('')
})
s.When('I use step', () => {
s.context.details = 'When'
})
s.Then('Code is executed', () => {
expect(count).toEqual(1)
expect(testFn).toHaveBeenCalledTimes(1)
})
})
f.Rule('Vitest', (r) => {
r.context.tests = 1
r.context.testFn = vi.fn()
r.RuleBackground.skip((b) => {
b.Given('I love vitest', () => {
r.context.tests += 1
r.context.testFn()
})
})
})
})