Skip to content

feat: support current datatable for before and after hooks - #5792

Merged
DenysKuchma merged 2 commits into
4.xfrom
feat-current-support-for-hoooks
Nov 10, 2026
Merged

DenysKuchma merged 2 commits into
4.xfrom
feat-current-support-for-hoooks

Conversation

@kobenguyent

@kobenguyent kobenguyent commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation/Description of the PR

  • Data-driven Before/After hooks

Files Created/Modified:

  1. Core Implementation (lib/data/context.js)
    - Added Before() and After() methods to Data() wrapper
    - Each data row creates a separate hook with current parameter
  2. HookConfig Enhancement (lib/mocha/hooks.js)
    - Added inject() method to HookConfig class for consistency
  3. Unit Tests (test/unit/data/dataDrivenHooks_test.js)
    - 8 tests verifying DataTable structure and row extraction
    - ✅ All passing
  4. Integration Test (test/data/sandbox/dataDrivenHooks_test.dataDrivenHooks.js)
    - Practical example with credentials data table
    - ✅ 2 scenarios passing
  5. Test Config (test/data/sandbox/codecept.dataDrivenHooks.json)
    - Codecept config for running the integration test

How to Run:

Unit tests

npx mocha test/unit/data/dataDrivenHooks_test.js

Integration test

./bin/codecept.js run --config test/data/sandbox/codecept.dataDrivenHooks.json

Usage Example:

const accounts = new DataTable(['login', 'password'])
accounts.add(['alice', 'pass123'])
accounts.add(['bob', 'pass456'])

Data(accounts).Before(({ current }) => {
// Runs once per data row
console.log(Setting up for ${current.login})
})

Data(accounts).After(({ current }) => {
// Cleanup per row
console.log(Cleaning up ${current.login})
})

Scenario('login works', () => {
// Scenarios get injected 'current' via Data() wrapper
})

Type of change

  • 🔥 Breaking changes
  • 🚀 New functionality
  • 🐛 Bug fix
  • 🧹 Chore
  • 📋 Documentation changes/updates
  • ♨️ Hot fix
  • 🔨 Markdown files fix - not related to source code
  • 💅 Polish code

Checklist:

  • Tests have been added
  • Documentation has been added (Run npm run docs)
  • Lint checking (Run npm run lint)
  • Local tests are passed (Run npm test)

@DenysKuchma

Copy link
Copy Markdown
Collaborator

@kobenguyent Thanks for picking up issue. The hooks currently aren't bound to data iterations: Data(table).Before() registers one suite-level hook per row, and suite-level hooks run before every test in the file — so all rows fire on every run instead of once per row. They should bind to the generated scenario for that row (like Data().Scenario() does .inject({ current }) per test). Also: the integration test accumulates hookCalls but never asserts them; the unit tests cover DataTable (pre-existing code) rather than the new methods; HookConfig.inject() is added but never used.

@DenysKuchma
DenysKuchma merged commit 4a93b11 into 4.x Nov 10, 2026
16 checks passed
@DenysKuchma
DenysKuchma deleted the feat-current-support-for-hoooks branch November 10, 2026 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feature-request] Send current into Before / After hooks in Data Driven Tests

2 participants