Skip to content
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
4 changes: 3 additions & 1 deletion docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,14 @@ Available events:
* `event.suite.before(suite)` - *async* before a suite
* `event.suite.after(suite)` - *async* after a suite
* `event.step.before(step)` - *async* when the step is scheduled for execution
* `event.step.after(step)`- *async* after a step
* `event.step.after(step)` - *async* after a step
* `event.step.started(step)` - *sync* when step starts.
* `event.step.passed(step)` - *sync* when step passed.
* `event.step.failed(step, err)` - *sync* when step failed.
* `event.step.finished(step)` - *sync* when step finishes.
* `event.step.comment(step)` - *sync* fired for comments like `I.say`.
* `event.bddStep.before(bddStep)` - *async* when the gherkin step is scheduled for execution
* `event.bddStep.after(bddStep)` - *async* after a gherkin step
* `event.all.before` - before running tests
* `event.all.after` - after running tests
* `event.all.result` - when results are printed
Expand Down
11 changes: 11 additions & 0 deletions lib/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ module.exports = {
finished: 'step.finish', // sync
comment: 'step.comment',
},
/**
* @type {object}
* @constant
* @inner
* @property {'suite.before'} before
* @property {'suite.after'} after
*/
bddStep: {
before: 'bddStep.before',
after: 'bddStep.after',
},
/**
* @type {object}
* @constant
Expand Down
2 changes: 2 additions & 0 deletions lib/interfaces/gherkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module.exports = (text) => {

const runSteps = async (steps) => {
for (const step of steps) {
event.emit(event.bddStep.before, step);
const metaStep = new Step.MetaStep(null, step.text);
metaStep.actor = step.keyword.trim();
const setMetaStep = (step) => {
Expand All @@ -50,6 +51,7 @@ module.exports = (text) => {
} finally {
event.dispatcher.removeListener(event.step.before, setMetaStep);
}
event.emit(event.bddStep.after, step);
}
};

Expand Down
22 changes: 21 additions & 1 deletion test/unit/bdd_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const run = require('../../lib/interfaces/gherkin');
const recorder = require('../../lib/recorder');
const container = require('../../lib/container');
const actor = require('../../lib/actor');
const event = require('../../lib/event');

const text = `
Feature: checkout process
Expand Down Expand Up @@ -168,6 +169,25 @@ describe('BDD', () => {
assert.equal('bird', fn.params[0]);
});

it('should produce step events', (done) => {
const text = `
Feature: Emit step event

Scenario:
Then I emit step events
`;
Then('I emit step events', () => {});
let listeners = 0;
event.dispatcher.addListener(event.bddStep.before, () => listeners++);
event.dispatcher.addListener(event.bddStep.after, () => listeners++);

const suite = run(text);
suite.tests[0].fn(() => {
listeners.should.eql(2);
done();
});
});

it('should use shortened form for step definitions', () => {
let fn;
Given('I am a {word}', params => params[0]);
Expand Down Expand Up @@ -214,7 +234,7 @@ describe('BDD', () => {
Given I have product with price <price>$ in my cart
And discount is 10 %
Then I should see price is "<total>" $

Examples:
| price | total |
| 10 | 9 |
Expand Down