Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pickleStep to step hook function arg #1775

Merged
merged 6 commits into from
Sep 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ See the [migration guide](./docs/migration.md) for details of how to migrate fro

### Added

* `BeforeStep` and `AfterStep` hook functions now have access to the `pickleStep` in their argument object.
* `--config` option to the CLI. It allows you to specify a configuration file other than `cucumber.js`.
See [docs/profiles.md](./docs/profiles.md#using-another-file-than-cucumberjs) for more info.
[#1794](https://github.com/cucumber/cucumber-js/pull/1794)
Expand Down
5 changes: 3 additions & 2 deletions docs/support_files/api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ Defines a hook which is run after each step.
* `tags`: String tag expression used to apply this hook to only specific scenarios. See [cucumber-tag-expressions](https://docs.cucumber.io/tag-expressions/) for more information.
* `timeout`: A hook-specific timeout, to override the default timeout.
* `fn`: A function, defined as follows:
* The first argument will be an object of the form `{pickle, gherkinDocument, result, testCaseStartedId, testStepId}`
* The pickle object comes from the [gherkin](https://github.com/cucumber/cucumber/tree/gherkin/v15.0.2/gherkin) library. See `testdata/good/*.pickles.ndjson` for examples of its structure.
* The first argument will be an object of the form `{pickle, pickleStep, gherkinDocument, result, testCaseStartedId, testStepId}`
* The `pickle` object comes from the [gherkin](https://github.com/cucumber/cucumber/tree/gherkin/v15.0.2/gherkin) library. See `testdata/good/*.pickles.ndjson` for examples of its structure.
* The `pickleStep` is the step in the `pickle` that this hook has been invoked for
* When using the asynchronous callback interface, have one final argument for the callback function.

`options` can also be a string as a shorthand for specifying `tags`.
Expand Down
6 changes: 5 additions & 1 deletion src/runtime/test_case_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,14 @@ export default class TestCaseRunner {

async runStepHooks(
stepHooks: TestStepHookDefinition[],
pickleStep: messages.PickleStep,
stepResult?: messages.TestStepResult
): Promise<messages.TestStepResult[]> {
const stepHooksResult = []
const hookParameter: ITestStepHookParameter = {
gherkinDocument: this.gherkinDocument,
pickle: this.pickle,
pickleStep,
testCaseStartedId: this.currentTestCaseStartedId,
testStepId: this.currentTestStepId,
result: stepResult,
Expand Down Expand Up @@ -295,7 +297,8 @@ export default class TestCaseRunner {

let stepResult
let stepResults = await this.runStepHooks(
this.getBeforeStepHookDefinitions()
this.getBeforeStepHookDefinitions(),
pickleStep
)
if (
getWorstTestStepResult(stepResults).status !==
Expand All @@ -306,6 +309,7 @@ export default class TestCaseRunner {
}
const afterStepHookResults = await this.runStepHooks(
this.getAfterStepHookDefinitions(),
pickleStep,
stepResult
)
stepResults = stepResults.concat(afterStepHookResults)
Expand Down
24 changes: 22 additions & 2 deletions src/runtime/test_case_runner_spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, it } from 'mocha'
import { expect } from 'chai'
import sinon from 'sinon'
import TestCaseRunner from './test_case_runner'
import { EventEmitter } from 'events'
import { IdGenerator } from '@cucumber/messages'
Expand Down Expand Up @@ -434,14 +435,17 @@ describe('TestCaseRunner', () => {

describe('with step hooks', () => {
it('emits the expected envelopes and returns a skipped result', async () => {
const beforeStep = sinon.stub()
const afterStep = sinon.stub()

// Arrange
const supportCodeLibrary = buildSupportCodeLibrary(
({ Given, BeforeStep, AfterStep }) => {
Given('a step', function () {
clock.tick(1)
})
BeforeStep(function () {}) // eslint-disable-line @typescript-eslint/no-empty-function
AfterStep(function () {}) // eslint-disable-line @typescript-eslint/no-empty-function
BeforeStep(beforeStep) // eslint-disable-line @typescript-eslint/no-empty-function
AfterStep(afterStep) // eslint-disable-line @typescript-eslint/no-empty-function
}
)
const {
Expand All @@ -464,6 +468,22 @@ describe('TestCaseRunner', () => {
expect(result).to.eql(
envelopes[2].testStepFinished.testStepResult.status
)
expect(beforeStep).to.have.been.calledOnceWith({
gherkinDocument,
pickle,
pickleStep: pickle.steps[0],
testCaseStartedId: envelopes[1].testStepStarted.testCaseStartedId,
testStepId: envelopes[1].testStepStarted.testStepId,
result: undefined,
})
expect(afterStep).to.have.been.calledOnceWith({
gherkinDocument,
pickle,
pickleStep: pickle.steps[0],
testCaseStartedId: envelopes[2].testStepFinished.testCaseStartedId,
testStepId: envelopes[2].testStepFinished.testStepId,
result: envelopes[2].testStepFinished.testStepResult,
})
})
})
})
Expand Down
1 change: 1 addition & 0 deletions src/support_code_library_builder/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface ITestCaseHookParameter {
export interface ITestStepHookParameter {
gherkinDocument: messages.GherkinDocument
pickle: messages.Pickle
pickleStep: messages.PickleStep
result: messages.TestStepResult
testCaseStartedId: string
testStepId: string
Expand Down