-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add BeforeStep and AfterStep hooks (#1416)
- Loading branch information
Showing
15 changed files
with
777 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
Feature: Before and After Step Hooks | ||
|
||
Background: | ||
Given a file named "features/a.feature" with: | ||
""" | ||
Feature: some feature | ||
@this-tag | ||
Scenario: some scenario | ||
Given a step | ||
""" | ||
And a file named "features/step_definitions/cucumber_steps.js" with: | ||
""" | ||
const {Given} = require('@cucumber/cucumber') | ||
Given(/^a step$/, function() {}) | ||
""" | ||
|
||
Scenario: Before and After Hooks work correctly | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const {BeforeStep, AfterStep, BeforeAll, AfterAll} = require('@cucumber/cucumber') | ||
const {expect} = require('chai') | ||
let counter = 1 | ||
BeforeStep(function() { | ||
counter = counter + 1 | ||
}) | ||
AfterStep(function() { | ||
expect(counter).to.eql(2) | ||
counter = counter + 1 | ||
}) | ||
AfterAll(function() { | ||
expect(counter).to.eql(3) | ||
}) | ||
""" | ||
When I run cucumber-js | ||
Then it passes | ||
|
||
Scenario: Failing before step fails the scenario | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const {BeforeStep} = require('@cucumber/cucumber') | ||
BeforeStep(function() { throw 'Fail' }) | ||
""" | ||
When I run cucumber-js | ||
Then it fails | ||
|
||
Scenario: Failing after step fails the scenario | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const {AfterStep} = require('@cucumber/cucumber') | ||
AfterStep(function() { throw 'Fail' }) | ||
""" | ||
When I run cucumber-js | ||
Then it fails | ||
|
||
Scenario: Only run BeforeStep hooks with appropriate tags | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const { BeforeStep } = require('@cucumber/cucumber') | ||
BeforeStep({tags: "@any-tag"}, function() { | ||
throw Error("Would fail if ran") | ||
}) | ||
""" | ||
When I run cucumber-js | ||
Then it passes | ||
|
||
Scenario: Only run BeforeStep hooks with appropriate tags | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const { AfterStep } = require('@cucumber/cucumber') | ||
AfterStep({tags: "@this-tag"}, function() { | ||
throw Error("Would fail if ran") | ||
}) | ||
""" | ||
When I run cucumber-js | ||
Then it fails | ||
|
||
Scenario: after hook parameter can access result status of step | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const { AfterStep, Status } = require('@cucumber/cucumber') | ||
AfterStep(function({result}) { | ||
if (result.status === Status.PASSED) { | ||
return | ||
} else { | ||
throw Error("Result object did not get passed properly to AfterStep Hook.") | ||
} | ||
}) | ||
""" | ||
When I run cucumber-js | ||
Then it passes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
Feature: World in Hooks | ||
|
||
Background: | ||
Given a file named "features/a.feature" with: | ||
""" | ||
Feature: some feature | ||
Scenario: some scenario | ||
Given a step | ||
""" | ||
And a file named "features/step_definitions/cucumber_steps.js" with: | ||
""" | ||
const {Given} = require('@cucumber/cucumber') | ||
Given(/^a step$/, function() {}) | ||
""" | ||
And a file named "features/support/world.js" with: | ||
""" | ||
const {setWorldConstructor} = require('@cucumber/cucumber') | ||
function WorldConstructor() { | ||
return { | ||
isWorld: function() { return true } | ||
} | ||
} | ||
setWorldConstructor(WorldConstructor) | ||
""" | ||
|
||
Scenario: World is this in hooks | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const {After, Before } = require('@cucumber/cucumber') | ||
Before(function() { | ||
if (!this.isWorld()) { | ||
throw Error("Expected this to be world") | ||
} | ||
}) | ||
After(function() { | ||
if (!this.isWorld()) { | ||
throw Error("Expected this to be world") | ||
} | ||
}) | ||
""" | ||
When I run cucumber-js | ||
Then it passes | ||
|
||
Scenario: World is this in BeforeStep hooks | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const {BeforeStep } = require('@cucumber/cucumber') | ||
BeforeStep(function() { | ||
if (!this.isWorld()) { | ||
throw Error("Expected this to be world") | ||
} | ||
}) | ||
""" | ||
When I run cucumber-js | ||
Then it passes | ||
|
||
Scenario: World is this in AfterStep hooks | ||
Given a file named "features/support/hooks.js" with: | ||
""" | ||
const {AfterStep } = require('@cucumber/cucumber') | ||
AfterStep(function() { | ||
if (!this.isWorld()) { | ||
throw Error("Expected this to be world") | ||
} | ||
}) | ||
""" | ||
When I run cucumber-js | ||
Then it passes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { PickleTagFilter } from '../pickle_filter' | ||
import Definition, { | ||
IDefinition, | ||
IGetInvocationDataResponse, | ||
IGetInvocationDataRequest, | ||
IDefinitionParameters, | ||
IHookDefinitionOptions, | ||
} from './definition' | ||
import { messages } from '@cucumber/messages' | ||
|
||
export default class TestStepHookDefinition | ||
extends Definition | ||
implements IDefinition { | ||
public readonly tagExpression: string | ||
private readonly pickleTagFilter: PickleTagFilter | ||
|
||
constructor(data: IDefinitionParameters<IHookDefinitionOptions>) { | ||
super(data) | ||
this.tagExpression = data.options.tags | ||
this.pickleTagFilter = new PickleTagFilter(data.options.tags) | ||
} | ||
|
||
appliesToTestCase(pickle: messages.IPickle): boolean { | ||
return this.pickleTagFilter.matchesAllTagExpressions(pickle) | ||
} | ||
|
||
async getInvocationParameters({ | ||
hookParameter, | ||
}: IGetInvocationDataRequest): Promise<IGetInvocationDataResponse> { | ||
return await Promise.resolve({ | ||
getInvalidCodeLengthMessage: () => | ||
this.buildInvalidCodeLengthMessage('0 or 1', '2'), | ||
parameters: [hookParameter], | ||
validCodeLengths: [0, 1, 2], | ||
}) | ||
} | ||
} |
Oops, something went wrong.