diff --git a/packages/main/gherkin-example/example.feature.js b/packages/main/gherkin-example/example.feature.js index d84e68b..beaa645 100644 --- a/packages/main/gherkin-example/example.feature.js +++ b/packages/main/gherkin-example/example.feature.js @@ -2,10 +2,7 @@ import { test, describe, beforeAll, afterAll } from 'vitest'; import { gherkinStep, - applyBeforeAllHooks, - applyBeforeHooks, - applyAfterAllHooks, - applyAfterHooks, + applyHooks, getWorldConstructor, } from 'quickpickle'; @@ -14,15 +11,15 @@ let World = getWorldConstructor() const common = {}; beforeAll(async () => { - await applyBeforeAllHooks(common); + await applyHooks('beforeAll', common); }); afterAll(async () => { - await applyAfterAllHooks(common); + await applyHooks('afterAll', common); }); const afterScenario = async(state) => { - await applyAfterHooks(state); + await applyHooks('after', state); } const initScenario = async(context, scenario, tags, steps) => { @@ -32,7 +29,7 @@ const initScenario = async(context, scenario, tags, steps) => { state.info.feature = 'Feature: QuickPickle\'s Comprehensive Gherkin Syntax Example'; state.info.scenario = scenario; state.info.tags = [...tags]; - await applyBeforeHooks(state); + await applyHooks('before', state); await gherkinStep(`a common precondition`, state, 10, -1); await gherkinStep(`another common precondition`, state, 11, -2); return state; diff --git a/packages/main/src/hooks.ts b/packages/main/src/hooks.ts index 962911b..5b12145 100644 --- a/packages/main/src/hooks.ts +++ b/packages/main/src/hooks.ts @@ -29,7 +29,7 @@ const hookNames: { [key: string]: string } = { afterStep: 'AfterStep', }; -const applyHooks = async (hooksName: string, state: any): Promise => { +export const applyHooks = async (hooksName: string, state: any): Promise => { const hooks = allHooks[hooksName]; for (let i = 0; i < hooks.length; i++) { let hook = hooks[i] @@ -60,19 +60,13 @@ const addHook = (hooksName: string, opts: string | Hook | ((state:any) => any), export const BeforeAll = (opts: string | (() => any), f?: () => any): void => { addHook('beforeAll', opts, f) }; -export const applyBeforeAllHooks = (state: any): Promise => applyHooks('beforeAll', state); export const Before = (opts: string | Hook | ((state:any) => any), f?: (state:any) => any): void => { addHook('before', opts, f) }; -export const applyBeforeHooks = (state: any): Promise => applyHooks('before', state); export const BeforeStep = (opts: string | Hook | ((state:any) => any), f?: (state:any) => any): void => { addHook('beforeStep', opts, f) }; -export const applyBeforeStepHooks = (state: any): Promise => applyHooks('beforeStep', state); export const AfterAll = (opts: string | (() => any), f?: () => any): void => { addHook('afterAll', opts, f) }; -export const applyAfterAllHooks = (state: any): Promise => applyHooks('afterAll', state); export const After = (opts: string | Hook | ((state:any) => any), f?: (state:any) => any): void => { addHook('after', opts, f) }; -export const applyAfterHooks = (state: any): Promise => applyHooks('after', state); export const AfterStep = (opts: string | Hook | ((state:any) => any), f?: (state:any) => any): void => { addHook('afterStep', opts, f) }; -export const applyAfterStepHooks = (state: any): Promise => applyHooks('afterStep', state); diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index e997d4e..74ef7bb 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts @@ -2,12 +2,13 @@ import { addStepDefinition, findStepDefinitionMatch } from './steps'; import { get, defaultsDeep } from 'lodash-es'; import { Plugin, ResolvedConfig as ViteResolvedConfig } from 'vite' import { - BeforeAll, applyBeforeAllHooks, - Before, applyBeforeHooks, - AfterAll, applyAfterAllHooks, - After, applyAfterHooks, - BeforeStep, applyBeforeStepHooks, - AfterStep, applyAfterStepHooks, + BeforeAll, + Before, + AfterAll, + After, + BeforeStep, + AfterStep, + applyHooks, } from './hooks'; import { explodeTags, tagsMatch, renderGherkin } from './render'; import { DataTable } from '@cucumber/cucumber'; @@ -16,21 +17,12 @@ import { normalizeTags } from './tags'; export { setWorldConstructor, getWorldConstructor, QuickPickleWorld, QuickPickleWorldInterface } from './world'; export { DocString, DataTable } -export { explodeTags, tagsMatch, normalizeTags } +export { explodeTags, tagsMatch, normalizeTags, applyHooks } const featureRegex = /\.feature(?:\.md)?$/; export { BeforeAll, Before, AfterAll, After, BeforeStep, AfterStep }; -export { - applyBeforeAllHooks, - applyBeforeHooks, - applyAfterAllHooks, - applyAfterHooks, - applyBeforeStepHooks, - applyAfterStepHooks, -}; - export const Given = addStepDefinition; export const When = addStepDefinition; export const Then = addStepDefinition; @@ -79,7 +71,7 @@ export const gherkinStep = async (step: string, state: any, line: number, stepId } try { - await applyBeforeStepHooks(state); + await applyHooks('beforeStep', state); try { await stepDefinitionMatch.stepDefinition.f(state, ...stepDefinitionMatch.parameters, data); } @@ -100,7 +92,7 @@ export const gherkinStep = async (step: string, state: any, line: number, stepId if (state.isComplete || !state.tagsMatch(state.config.softFailTags)) throw e } finally { - await applyAfterStepHooks(state); + await applyHooks('afterStep', state); } } catch(e:any) { @@ -120,7 +112,7 @@ export const gherkinStep = async (step: string, state: any, line: number, stepId // The After hook is usually run in the rendered file, at the end of the rendered steps. // But, if the tests have failed, then it should run here, since the test is halted. - await applyAfterHooks(state) + await applyHooks('after', state) // Otherwise throw the error throw e diff --git a/packages/main/src/render.ts b/packages/main/src/render.ts index ae934d8..eab33b2 100644 --- a/packages/main/src/render.ts +++ b/packages/main/src/render.ts @@ -26,10 +26,7 @@ export function renderGherkin(src:string, config:QuickPickleConfig, isMarkdown?: import { test, describe, beforeAll, afterAll } from 'vitest'; import { gherkinStep, - applyBeforeAllHooks, - applyBeforeHooks, - applyAfterAllHooks, - applyAfterHooks, + applyHooks, getWorldConstructor, } from 'quickpickle'; @@ -38,15 +35,15 @@ let World = getWorldConstructor() const common = {}; beforeAll(async () => { - await applyBeforeAllHooks(common); + await applyHooks('beforeAll', common); }); afterAll(async () => { - await applyAfterAllHooks(common); + await applyHooks('afterAll', common); }); const afterScenario = async(state) => { - await applyAfterHooks(state); + await applyHooks('after', state); } ${renderFeature(gherkinDocument.feature!, config)} ` @@ -71,7 +68,7 @@ const initScenario = async(context, scenario, tags, steps) => { state.info.feature = '${featureName}'; state.info.scenario = scenario; state.info.tags = [...tags]; - await applyBeforeHooks(state); + await applyHooks('before', state); ${backgroundSteps} return state; }