Skip to content

Commit

Permalink
Adding BeforeAll and AfterAll hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
VidhiRambhia committed Nov 6, 2023
1 parent ad66c3d commit ba4dc23
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 13 deletions.
23 changes: 14 additions & 9 deletions features/issues/758.feature
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
# https://github.com/badeball/cypress-cucumber-preprocessor/issues/758

Feature: visualizing hook with filter
Scenario: visualizing hook with filter
Feature: beforeAll and afterAll hooks
@fav
Scenario: beforeAll and afterAll hooks
Given a file named "cypress/e2e/a.feature" with:
"""
@foo
Feature: a feature x
Scenario: a scenario a
Scenario: a scenario b
Given a step
Feature: a feature x
Scenario: a scenario b
Given a step
@foo
Scenario: a scenario c
Given a step
"""
And a file named "cypress/support/step_definitions/steps.js" with:
"""
const { BeforeAll, AfterAll, Given } = require("@badeball/cypress-cucumber-preprocessor");
BeforeAll(() => {})
let counter = 0
BeforeAll(() => {
expect(counter++, "Expect counter to be 0").to.equal(0)
})
Given("a step", function() {
})
AfterAll(() => {})
AfterAll(() => {
expect(counter++, "Expect counter to be 1").to.equal(1)
})
"""
When I run cypress
Then it passes
61 changes: 59 additions & 2 deletions lib/browser-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import DataTable from "./data_table";

import {
assignRegistry,
freeRegistry,
freeRegistry, getRegistry,
IHook,
MissingDefinitionError,
Registry,
Expand Down Expand Up @@ -268,12 +268,17 @@ function createFeature(context: CompositionContext, feature: messages.Feature) {
describe(feature.name || "<unamed feature>", () => {
before(function () {
beforeHandler.call(this, context);
beforeAllHandler.call(this, context);
});

beforeEach(function () {
beforeEachHandler.call(this, context);
});

after(function () {
afterAllHandler.call(this, context);
});

afterEach(function () {
afterEachHandler.call(this, context);
});
Expand Down Expand Up @@ -715,6 +720,31 @@ function beforeHandler(context: CompositionContext) {
taskSpecEnvelopes(context);
}

function beforeAllHandler(this: Mocha.Context, context: CompositionContext) {
const { registry } = context;
let beforeAllHooks = registry.resolveBeforeAllHooks();
for(const beforeAllHook of beforeAllHooks) {
if (beforeAllHook) {
const hook = beforeAllHook;
cy.then(() => {
const start = createTimestamp();
return cy.wrap(start, { log: false });
})
.then((start) => {
runStepWithLogGroup({
fn: () => registry.runHook(this, hook),
keyword: "BeforeAll"
});

return cy.wrap(start, { log: false });
})
.then((start) => {
const end = createTimestamp();
});
}
}
}

function beforeEachHandler(context: CompositionContext) {
assignRegistry(context.registry);
}
Expand Down Expand Up @@ -946,6 +976,30 @@ function afterEachHandler(this: Mocha.Context, context: CompositionContext) {
});
}

function afterAllHandler(this: Mocha.Context, context: CompositionContext) {
const { registry } = context;
let afterAllHooks = registry.resolveAfterAllHooks();
for(const afterAllHook of afterAllHooks) {
if (afterAllHook) {
const hook = afterAllHook;
cy.then(() => {
const start = createTimestamp();
return cy.wrap(start, { log: false });
})
.then((start) => {
runStepWithLogGroup({
fn: () => registry.runHook(this, hook),
keyword: "AfterAll"
});

return cy.wrap(start, { log: false });
})
.then((start) => {
const end = createTimestamp();
});
}
}
}
export default function createTests(
registry: Registry,
seed: number,
Expand Down Expand Up @@ -999,7 +1053,8 @@ export default function createTests(
const tags = collectTagNames(pickle.tags);
const beforeHooks = registry.resolveBeforeHooks(tags);
const afterHooks = registry.resolveAfterHooks(tags);

const beforeAllHooks = registry.resolveBeforeAllHooks();
const afterAllHooks = registry.resolveAfterAllHooks();
const hooksToStep = (hook: IHook): messages.TestStep => {
return {
id: createTestStepId({
Expand Down Expand Up @@ -1035,9 +1090,11 @@ export default function createTests(
id: pickle.id,
pickleId: pickle.id,
testSteps: [
...beforeAllHooks.map(hooksToStep),
...beforeHooks.map(hooksToStep),
...pickle.steps.map(pickleStepToTestStep),
...afterHooks.map(hooksToStep),
...afterAllHooks.map(hooksToStep)
],
};
});
Expand Down
24 changes: 24 additions & 0 deletions lib/entrypoint-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,28 @@ function defineAfterStep(
}
}

function defineBeforeAll(fn: IHookBody): void;
function defineBeforeAll(
maybeFn?: IHookBody
) {
if (typeof maybeFn === "function") {
getRegistry().defineBeforeAll(maybeFn);
} else {
throw new Error("Unexpected argument for BeforeAll hook");
}
}

function defineAfterAll(fn: IHookBody): void;
function defineAfterAll(
maybeFn?: IHookBody
) {
if (typeof maybeFn === "function") {
getRegistry().defineAfterAll(maybeFn);
} else {
throw new Error("Unexpected argument for AfterAll hook");
}
}

function createStringAttachment(
data: string,
mediaType: string,
Expand Down Expand Up @@ -205,6 +227,8 @@ export {
defineAfter as After,
defineBeforeStep as BeforeStep,
defineAfterStep as AfterStep,
defineBeforeAll as BeforeAll,
defineAfterAll as AfterAll
};

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/entrypoint-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,20 @@ export function AfterStep(
throw createUnimplemented();
}

export function BeforeAll(fn: IStepHookBody): void;
export function BeforeAll(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
maybeFn?: IStepHookBody
) {
throw createUnimplemented();
}

export function AfterAll(fn: IStepHookBody): void;
export function AfterAll(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
maybeFn?: IStepHookBody
) {
throw createUnimplemented();
}

export { default as DataTable } from "./data_table";
18 changes: 16 additions & 2 deletions lib/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class MissingDefinitionError extends CypressCucumberError {}

export class MultipleDefinitionsError extends CypressCucumberError {}

export type ScenarioHookKeyword = "Before" | "After";
export type ScenarioHookKeyword = "Before" | "After" | "BeforeAll" | "AfterAll";

export type StepHookKeyword = "BeforeStep" | "AfterStep";

Expand Down Expand Up @@ -97,7 +97,6 @@ export class Registry {
this.runStepDefininition = this.runStepDefininition.bind(this);
this.defineParameterType = this.defineParameterType.bind(this);
this.defineBefore = this.defineBefore.bind(this);
this.defineAfter = this.defineAfter.bind(this);

this.parameterTypeRegistry = new ParameterTypeRegistry();
}
Expand Down Expand Up @@ -204,6 +203,14 @@ export class Registry {
this.defineStepHook("AfterStep", options, fn);
}

public defineBeforeAll(fn: IHookBody) {
this.defineHook("BeforeAll", {}, fn);
}

public defineAfterAll(fn: IHookBody) {
this.defineHook("AfterAll", {}, fn);
}

public getMatchingStepDefinitions(text: string) {
return this.stepDefinitions.filter((stepDefinition) =>
stepDefinition.expression.match(text)
Expand Down Expand Up @@ -293,6 +300,13 @@ export class Registry {
return this.resolveStepHooks("AfterStep", tags);
}

public resolveBeforeAllHooks() {
return this.resolveHooks("BeforeAll", []);
}

public resolveAfterAllHooks() {
return this.resolveHooks("AfterAll", []);
}
public runStepHook(
world: Mocha.Context,
hook: IStepHook,
Expand Down
10 changes: 10 additions & 0 deletions test-d/entrypoint-browser.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
After,
BeforeStep,
AfterStep,
BeforeAll,
AfterAll,
DataTable,
} from "../lib/entrypoint-browser";

Expand Down Expand Up @@ -138,6 +140,10 @@ defineParameterType({
},
});

BeforeAll(function () {
expectType<Mocha.Context>(this);
});

Before(function () {
expectType<Mocha.Context>(this);
});
Expand All @@ -162,6 +168,10 @@ After({ tags: "foo" }, function () {
expectType<Mocha.Context>(this);
});

AfterAll(function () {
expectType<Mocha.Context>(this);
});

BeforeStep(function ({
pickle,
pickleStep,
Expand Down
2 changes: 2 additions & 0 deletions test-d/entrypoint-node.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import {
After,
BeforeStep,
AfterStep,
BeforeAll,
AfterAll,
DataTable,
} from "../lib/entrypoint-node";

Expand Down

0 comments on commit ba4dc23

Please sign in to comment.