Skip to content

Commit 1ca2470

Browse files
author
DavertMik
committed
added hook config
1 parent cfc95c6 commit 1ca2470

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

lib/mocha/hooks.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Hook {
44
constructor(context, error) {
55
this.suite = context.suite
66
this.test = context.test
7-
this.runnable = context.ctx.test
7+
this.runnable = context?.ctx?.test
88
this.ctx = context.ctx
99
this.error = error
1010
}
@@ -17,6 +17,10 @@ class Hook {
1717
return this.toString() + '()'
1818
}
1919

20+
retry(n) {
21+
// must be implemented for each hook
22+
}
23+
2024
get title() {
2125
return this.ctx?.test?.title || this.name
2226
}
@@ -26,13 +30,29 @@ class Hook {
2630
}
2731
}
2832

29-
class BeforeHook extends Hook {}
33+
class BeforeHook extends Hook {
34+
retry(n) {
35+
this.suite.opts['retryBefore'] = n
36+
}
37+
}
3038

31-
class AfterHook extends Hook {}
39+
class AfterHook extends Hook {
40+
retry(n) {
41+
this.suite.opts['retryAfter'] = n
42+
}
43+
}
3244

33-
class BeforeSuiteHook extends Hook {}
45+
class BeforeSuiteHook extends Hook {
46+
retry(n) {
47+
this.suite.opts['retryBeforeSuite'] = n
48+
}
49+
}
3450

35-
class AfterSuiteHook extends Hook {}
51+
class AfterSuiteHook extends Hook {
52+
retry(n) {
53+
this.suite.opts['retryAfterSuite'] = n
54+
}
55+
}
3656

3757
function fireHook(eventType, suite, error) {
3858
const hook = suite.ctx?.test?.title?.match(/"([^"]*)"/)[1]
@@ -54,10 +74,22 @@ function fireHook(eventType, suite, error) {
5474
}
5575
}
5676

77+
class HookConfig {
78+
constructor(hook) {
79+
this.hook = hook
80+
}
81+
82+
retry(n) {
83+
this.hook.retry(n)
84+
return this
85+
}
86+
}
87+
5788
module.exports = {
5889
BeforeHook,
5990
AfterHook,
6091
BeforeSuiteHook,
6192
AfterSuiteHook,
6293
fireHook,
94+
HookConfig,
6395
}

lib/mocha/scenarioConfig.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ class ScenarioConfig {
8181
if (typeof obj === 'function') {
8282
if (isAsyncFunction(obj)) {
8383
obj(this.test).then(res => (this.test.config[helper] = res))
84-
} else {
85-
obj = obj(this.test)
84+
return this
8685
}
87-
return this
86+
obj = obj(this.test)
8887
}
8988

9089
this.test.config[helper] = obj

lib/mocha/ui.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const FeatureConfig = require('./featureConfig')
55
const addDataContext = require('../data/context')
66
const { createTest } = require('./test')
77
const { createSuite } = require('./suite')
8+
const { HookConfig, AfterSuiteHook, AfterHook, BeforeSuiteHook, BeforeHook } = require('./hooks')
89

910
const setContextTranslation = context => {
1011
const container = require('../container')
@@ -118,18 +119,22 @@ module.exports = function (suite) {
118119

119120
context.BeforeSuite = function (fn) {
120121
suites[0].beforeAll('BeforeSuite', scenario.injected(fn, suites[0], 'beforeSuite'))
122+
return new HookConfig(new BeforeSuiteHook({ suite: suites[0] }))
121123
}
122124

123125
context.AfterSuite = function (fn) {
124126
afterAllHooks.unshift(['AfterSuite', scenario.injected(fn, suites[0], 'afterSuite')])
127+
return new HookConfig(new AfterSuiteHook({ suite: suites[0] }))
125128
}
126129

127130
context.Background = context.Before = function (fn) {
128131
suites[0].beforeEach('Before', scenario.injected(fn, suites[0], 'before'))
132+
return new HookConfig(new BeforeHook({ suite: suites[0] }))
129133
}
130134

131135
context.After = function (fn) {
132136
afterEachHooks.unshift(['After', scenario.injected(fn, suites[0], 'after')])
137+
return new HookConfig(new AfterHook({ suite: suites[0] }))
133138
}
134139

135140
/**

0 commit comments

Comments
 (0)