UNPKG

@amiceli/vitest-cucumber

Version:

vitest tools to use Gherkin feature in unit tests

64 lines (63 loc) 2.31 kB
import { StepTypes } from '../parser/models/step'; import { defineSharedStep } from './describe/define-step-test'; function getDefaultConfiguration() { return { language: 'en', includeTags: [], excludeTags: ['ignore'], onStepError: ({ error }) => { }, predefinedSteps: [], mappedExamples: {}, }; } let globalConfiguration = { predefinedSteps: [], mappedExamples: {}, }; export const getVitestCucumberConfiguration = (options) => { const defaultConfiguration = getDefaultConfiguration(); // @ts-ignore if (typeof window !== 'undefined') { defaultConfiguration.includeTags?.push( // @ts-ignore ...(import.meta.env.VITEST_INCLUDE_TAGS?.split(' ') || [])); defaultConfiguration.excludeTags?.push( // @ts-ignore ...(import.meta.env.VITEST_EXCLUDE_TAGS?.split(' ') || [])); } else { defaultConfiguration.includeTags?.push(...(process.env.VITEST_INCLUDE_TAGS?.split(' ') || [])); defaultConfiguration.excludeTags?.push(...(process.env.VITEST_EXCLUDE_TAGS?.split(' ') || [])); } const mergedOptions = { ...defaultConfiguration, ...globalConfiguration, ...(options || {}), }; return mergedOptions; }; export const setVitestCucumberConfiguration = (options) => { globalConfiguration = options; }; export function resetDefinedSteps() { globalConfiguration.predefinedSteps = []; } export const defineSteps = (defineStepsCallback) => { defineStepsCallback({ Given: (name, callback) => { globalConfiguration.predefinedSteps.push(defineSharedStep(StepTypes.GIVEN, name, callback)); }, And: (name, callback) => { globalConfiguration.predefinedSteps.push(defineSharedStep(StepTypes.AND, name, callback)); }, Then: (name, callback) => { globalConfiguration.predefinedSteps.push(defineSharedStep(StepTypes.THEN, name, callback)); }, When: (name, callback) => { globalConfiguration.predefinedSteps.push(defineSharedStep(StepTypes.WHEN, name, callback)); }, But: (name, callback) => { globalConfiguration.predefinedSteps.push(defineSharedStep(StepTypes.BUT, name, callback)); }, }); };