Skip to content

Commit a742eee

Browse files
committed
feat: support current datatable for before and after hooks
1 parent 5ea0d04 commit a742eee

5 files changed

Lines changed: 143 additions & 0 deletions

File tree

‎lib/data/context.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ export default function (context) {
2626
maskSecretInTitle(scenarios)
2727
return new DataScenarioConfig(scenarios)
2828
},
29+
Before(fn) {
30+
data.forEach(dataRow => {
31+
if (!dataRow.skip) {
32+
const wrappedFn = (args) => fn({ ...args, current: dataRow.data })
33+
context.Before(wrappedFn)
34+
}
35+
})
36+
},
37+
After(fn) {
38+
data.forEach(dataRow => {
39+
if (!dataRow.skip) {
40+
const wrappedFn = (args) => fn({ ...args, current: dataRow.data })
41+
context.After(wrappedFn)
42+
}
43+
})
44+
},
2945
only: {
3046
Scenario(title, opts, fn) {
3147
const scenarios = []

‎lib/mocha/hooks.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ class HookConfig {
110110
this.hook.retry(n)
111111
return this
112112
}
113+
114+
inject(obj) {
115+
this.hook.inject = obj
116+
return this
117+
}
113118
}
114119

115120
export {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"tests": "./*_test.dataDrivenHooks.js",
3+
"timeout": 10000,
4+
"output": "./output",
5+
"helpers": {
6+
"FakeDriver": {
7+
"require": "../helper"
8+
}
9+
},
10+
"include": {},
11+
"bootstrap": false,
12+
"mocha": {},
13+
"name": "sandbox"
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Feature('Data-driven Before/After hooks');
2+
3+
import { dataTable as DataTable } from 'codeceptjs';
4+
5+
const hookCalls = [];
6+
7+
const credentials = new DataTable(['username', 'password']);
8+
credentials.add(['alice', 'pass123']);
9+
credentials.add(['bob', 'pass456']);
10+
11+
Data(credentials).Before(({ current }) => {
12+
hookCalls.push(`Before: ${current.username}`);
13+
});
14+
15+
Data(credentials).After(({ current }) => {
16+
hookCalls.push(`After: ${current.username}`);
17+
});
18+
19+
Scenario('test 1', () => {
20+
// Just ensure test runs
21+
});
22+
23+
Scenario('test 2', () => {
24+
// Just ensure test runs
25+
});
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { expect } from 'chai'
2+
import DataTable from '../../../lib/data/table.js'
3+
4+
describe('Data-driven Before/After hooks - data structure', () => {
5+
it('should create DataTable with correct structure', () => {
6+
const accounts = new DataTable(['login', 'password'])
7+
accounts.add(['davert', '123456'])
8+
accounts.add(['admin', '654321'])
9+
10+
expect(accounts.rows).to.have.lengthOf(2)
11+
expect(accounts.array).to.deep.equal(['login', 'password'])
12+
})
13+
14+
it('should have rows property with data and skip', () => {
15+
const accounts = new DataTable(['login', 'password'])
16+
accounts.add(['davert', '123456'])
17+
18+
accounts.rows.forEach(row => {
19+
expect(row).to.have.property('data')
20+
expect(row).to.have.property('skip')
21+
expect(row.data).to.include.keys(['login', 'password'])
22+
})
23+
})
24+
25+
it('first row should have correct data', () => {
26+
const accounts = new DataTable(['login', 'password'])
27+
accounts.add(['davert', '123456'])
28+
accounts.add(['admin', '654321'])
29+
30+
const firstRow = accounts.rows[0]
31+
expect(firstRow.data.login).to.equal('davert')
32+
expect(firstRow.data.password).to.equal('123456')
33+
})
34+
35+
it('second row should have correct data', () => {
36+
const accounts = new DataTable(['login', 'password'])
37+
accounts.add(['davert', '123456'])
38+
accounts.add(['admin', '654321'])
39+
40+
const secondRow = accounts.rows[1]
41+
expect(secondRow.data.login).to.equal('admin')
42+
expect(secondRow.data.password).to.equal('654321')
43+
})
44+
45+
it('should support filtering skipped rows', () => {
46+
const accounts = new DataTable(['login', 'password'])
47+
accounts.add(['davert', '123456'])
48+
accounts.add(['admin', '654321'])
49+
50+
const activeRows = accounts.rows.filter(row => !row.skip)
51+
expect(activeRows).to.have.lengthOf(2)
52+
})
53+
54+
it('should extract current from row data', () => {
55+
const accounts = new DataTable(['login', 'password'])
56+
accounts.add(['davert', '123456'])
57+
58+
const row = accounts.rows[0]
59+
const current = row.data
60+
61+
expect(current.login).to.equal('davert')
62+
expect(current.password).to.equal('123456')
63+
})
64+
65+
it('should support iterating all rows', () => {
66+
const accounts = new DataTable(['login', 'password'])
67+
accounts.add(['davert', '123456'])
68+
accounts.add(['admin', '654321'])
69+
70+
const currentValues = accounts.rows.map(row => row.data)
71+
expect(currentValues).to.have.lengthOf(2)
72+
expect(currentValues[0].login).to.equal('davert')
73+
expect(currentValues[1].login).to.equal('admin')
74+
})
75+
76+
it('should detect when row is skipped', () => {
77+
const accounts = new DataTable(['login', 'password'])
78+
accounts.add(['davert', '123456'])
79+
80+
const firstRow = accounts.rows[0]
81+
expect(firstRow.skip).to.be.false
82+
})
83+
})

0 commit comments

Comments
 (0)