Skip to content

Commit

Permalink
fix after hook run order
Browse files Browse the repository at this point in the history
closes #744
resolves #743
  • Loading branch information
charlierudolph committed Jan 28, 2017
1 parent 54f4a1f commit 89b17cb
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
60 changes: 60 additions & 0 deletions features/multiple_hooks.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Feature: Multiple Hooks

Scenario: before hooks run in the order of definition, after hooks in reverse order of definition
Given a file named "features/a.feature" with:
"""
@foo
Feature:
Scenario:
Given a step
Scenario:
Given a step
"""
And a file named "features/step_definitions/world.js" with:
"""
import {defineSupportCode} from 'cucumber'
defineSupportCode(({setWorldConstructor}) => {
setWorldConstructor(function() {
this.value = 0
})
})
"""
And a file named "features/step_definitions/my_steps.js" with:
"""
import assert from 'assert'
import {defineSupportCode} from 'cucumber'
defineSupportCode(({Given}) => {
Given('a step', function() {})
})
"""
And a file named "features/step_definitions/hooks.js" with:
"""
import {defineSupportCode} from 'cucumber'
import assert from 'assert'
defineSupportCode(({After, Before}) => {
Before(function() {
assert.equal(this.value, 0)
this.value += 1
})
After(function() {
assert.equal(this.value, 3)
})
Before({tags: '@foo'}, function() {
assert.equal(this.value, 1)
this.value += 1
})
After({tags: '@foo'}, function() {
assert.equal(this.value, 2)
this.value += 1
})
})
"""
When I run cucumber.js
Then it passes
2 changes: 1 addition & 1 deletion src/runtime/scenario_runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default class ScenarioRunner {

async runAfterHooks() {
await this.runHooks({
hookDefinitions: this.supportCodeLibrary.afterHookDefinitions.reverse(),
hookDefinitions: this.supportCodeLibrary.afterHookDefinitions,
hookKeyword: Hook.AFTER_STEP_KEYWORD
})
}
Expand Down
1 change: 1 addition & 0 deletions src/support_code_library/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function build({cwd, fns}) {
.flatten()
.value()
})
options.afterHookDefinitions.reverse()
return options
}

Expand Down
36 changes: 36 additions & 0 deletions src/support_code_library/builder_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ describe('SupportCodeLibraryBuilder', function () {
expect(this.options.afterHookDefinitions[0].code).to.eql(this.hook)
})
})

describe('multiple', function() {
beforeEach(function() {
this.hook1 = function hook1() {}
this.hook2 = function hook2() {}
const fn = ({After}) => {
After(this.hook1) // eslint-disable-line babel/new-cap
After(this.hook2) // eslint-disable-line babel/new-cap
}
this.options = SupportCodeLibraryBuilder.build({cwd: 'path/to/project', fns: [fn]})
})

it('adds the hook definitions in the reverse order of definition', function() {
expect(this.options.afterHookDefinitions).to.have.lengthOf(2)
expect(this.options.afterHookDefinitions[0].code).to.eql(this.hook2)
expect(this.options.afterHookDefinitions[1].code).to.eql(this.hook1)
})
})
})

describe('this.Before', function() {
Expand Down Expand Up @@ -126,5 +144,23 @@ describe('SupportCodeLibraryBuilder', function () {
expect(this.options.beforeHookDefinitions[0].code).to.eql(this.hook)
})
})

describe('multiple', function() {
beforeEach(function() {
this.hook1 = function hook1() {}
this.hook2 = function hook2() {}
const fn = ({Before}) => {
Before(this.hook1) // eslint-disable-line babel/new-cap
Before(this.hook2) // eslint-disable-line babel/new-cap
}
this.options = SupportCodeLibraryBuilder.build({cwd: 'path/to/project', fns: [fn]})
})

it('adds the hook definitions in the order of definition', function() {
expect(this.options.beforeHookDefinitions).to.have.lengthOf(2)
expect(this.options.beforeHookDefinitions[0].code).to.eql(this.hook1)
expect(this.options.beforeHookDefinitions[1].code).to.eql(this.hook2)
})
})
})
})

0 comments on commit 89b17cb

Please sign in to comment.