Skip to content

Commit

Permalink
fix(main): remove unnecessary applyHooks wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
dnotes committed Nov 11, 2024
1 parent dee9d4b commit 361624a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 42 deletions.
13 changes: 5 additions & 8 deletions packages/main/gherkin-example/example.feature.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions packages/main/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const hookNames: { [key: string]: string } = {
afterStep: 'AfterStep',
};

const applyHooks = async (hooksName: string, state: any): Promise<any> => {
export const applyHooks = async (hooksName: string, state: any): Promise<any> => {
const hooks = allHooks[hooksName];
for (let i = 0; i < hooks.length; i++) {
let hook = hooks[i]
Expand Down Expand Up @@ -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<any> => 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<any> => 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<any> => applyHooks('beforeStep', state);

export const AfterAll = (opts: string | (() => any), f?: () => any): void => { addHook('afterAll', opts, f) };
export const applyAfterAllHooks = (state: any): Promise<any> => 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<any> => 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<any> => applyHooks('afterStep', state);
30 changes: 11 additions & 19 deletions packages/main/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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
Expand Down
13 changes: 5 additions & 8 deletions packages/main/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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)}
`
Expand All @@ -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;
}
Expand Down

0 comments on commit 361624a

Please sign in to comment.