Skip to content
Open
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
16 changes: 16 additions & 0 deletions lib/data/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ export default function (context) {
maskSecretInTitle(scenarios)
return new DataScenarioConfig(scenarios)
},
Before(fn) {
data.forEach(dataRow => {
if (!dataRow.skip) {
const wrappedFn = (args) => fn({ ...args, current: dataRow.data })
context.Before(wrappedFn)
}
})
},
After(fn) {
data.forEach(dataRow => {
if (!dataRow.skip) {
const wrappedFn = (args) => fn({ ...args, current: dataRow.data })
context.After(wrappedFn)
}
})
},
only: {
Scenario(title, opts, fn) {
const scenarios = []
Expand Down
5 changes: 5 additions & 0 deletions lib/mocha/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ class HookConfig {
this.hook.retry(n)
return this
}

inject(obj) {
this.hook.inject = obj
return this
}
}

export {
Expand Down
14 changes: 14 additions & 0 deletions test/data/sandbox/codecept.dataDrivenHooks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"tests": "./*_test.dataDrivenHooks.js",
"timeout": 10000,
"output": "./output",
"helpers": {
"FakeDriver": {
"require": "../helper"
}
},
"include": {},
"bootstrap": false,
"mocha": {},
"name": "sandbox"
}
25 changes: 25 additions & 0 deletions test/data/sandbox/dataDrivenHooks_test.dataDrivenHooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature('Data-driven Before/After hooks');

import { dataTable as DataTable } from 'codeceptjs';

const hookCalls = [];

const credentials = new DataTable(['username', 'password']);
credentials.add(['alice', 'pass123']);
credentials.add(['bob', 'pass456']);

Data(credentials).Before(({ current }) => {
hookCalls.push(`Before: ${current.username}`);
});

Data(credentials).After(({ current }) => {
hookCalls.push(`After: ${current.username}`);
});

Scenario('test 1', () => {
// Just ensure test runs
});

Scenario('test 2', () => {
// Just ensure test runs
});
83 changes: 83 additions & 0 deletions test/unit/data/dataDrivenHooks_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { expect } from 'chai'
import DataTable from '../../../lib/data/table.js'

describe('Data-driven Before/After hooks - data structure', () => {
it('should create DataTable with correct structure', () => {
const accounts = new DataTable(['login', 'password'])
accounts.add(['davert', '123456'])
accounts.add(['admin', '654321'])

expect(accounts.rows).to.have.lengthOf(2)
expect(accounts.array).to.deep.equal(['login', 'password'])
})

it('should have rows property with data and skip', () => {
const accounts = new DataTable(['login', 'password'])
accounts.add(['davert', '123456'])

accounts.rows.forEach(row => {
expect(row).to.have.property('data')
expect(row).to.have.property('skip')
expect(row.data).to.include.keys(['login', 'password'])
})
})

it('first row should have correct data', () => {
const accounts = new DataTable(['login', 'password'])
accounts.add(['davert', '123456'])
accounts.add(['admin', '654321'])

const firstRow = accounts.rows[0]
expect(firstRow.data.login).to.equal('davert')
expect(firstRow.data.password).to.equal('123456')
})

it('second row should have correct data', () => {
const accounts = new DataTable(['login', 'password'])
accounts.add(['davert', '123456'])
accounts.add(['admin', '654321'])

const secondRow = accounts.rows[1]
expect(secondRow.data.login).to.equal('admin')
expect(secondRow.data.password).to.equal('654321')
})

it('should support filtering skipped rows', () => {
const accounts = new DataTable(['login', 'password'])
accounts.add(['davert', '123456'])
accounts.add(['admin', '654321'])

const activeRows = accounts.rows.filter(row => !row.skip)
expect(activeRows).to.have.lengthOf(2)
})

it('should extract current from row data', () => {
const accounts = new DataTable(['login', 'password'])
accounts.add(['davert', '123456'])

const row = accounts.rows[0]
const current = row.data

expect(current.login).to.equal('davert')
expect(current.password).to.equal('123456')
})

it('should support iterating all rows', () => {
const accounts = new DataTable(['login', 'password'])
accounts.add(['davert', '123456'])
accounts.add(['admin', '654321'])

const currentValues = accounts.rows.map(row => row.data)
expect(currentValues).to.have.lengthOf(2)
expect(currentValues[0].login).to.equal('davert')
expect(currentValues[1].login).to.equal('admin')
})

it('should detect when row is skipped', () => {
const accounts = new DataTable(['login', 'password'])
accounts.add(['davert', '123456'])

const firstRow = accounts.rows[0]
expect(firstRow.skip).to.be.false
})
})
Loading