Skip to content

Commit 01bfdb2

Browse files
author
DavertMik
committed
refactored mocha classes
1 parent b2d6cf9 commit 01bfdb2

File tree

21 files changed

+314
-142
lines changed

21 files changed

+314
-142
lines changed

lib/codecept.js

Lines changed: 83 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
const { existsSync, readFileSync } = require('fs');
2-
const glob = require('glob');
3-
const fsPath = require('path');
4-
const { resolve } = require('path');
5-
6-
const container = require('./container');
7-
const Config = require('./config');
8-
const event = require('./event');
9-
const runHook = require('./hooks');
10-
const output = require('./output');
11-
const { emptyFolder } = require('./utils');
1+
const { existsSync, readFileSync } = require('fs')
2+
const glob = require('glob')
3+
const fsPath = require('path')
4+
const { resolve } = require('path')
5+
6+
const container = require('./container')
7+
const Config = require('./config')
8+
const event = require('./event')
9+
const runHook = require('./hooks')
10+
const output = require('./output')
11+
const { emptyFolder } = require('./utils')
1212

1313
/**
1414
* CodeceptJS runner
@@ -22,10 +22,10 @@ class Codecept {
2222
* @param {*} opts
2323
*/
2424
constructor(config, opts) {
25-
this.config = Config.create(config);
26-
this.opts = opts;
27-
this.testFiles = new Array(0);
28-
this.requireModules(config.require);
25+
this.config = Config.create(config)
26+
this.opts = opts
27+
this.testFiles = new Array(0)
28+
this.requireModules(config.require)
2929
}
3030

3131
/**
@@ -36,12 +36,12 @@ class Codecept {
3636
requireModules(requiringModules) {
3737
if (requiringModules) {
3838
requiringModules.forEach(requiredModule => {
39-
const isLocalFile = existsSync(requiredModule) || existsSync(`${requiredModule}.js`);
39+
const isLocalFile = existsSync(requiredModule) || existsSync(`${requiredModule}.js`)
4040
if (isLocalFile) {
41-
requiredModule = resolve(requiredModule);
41+
requiredModule = resolve(requiredModule)
4242
}
43-
require(requiredModule);
44-
});
43+
require(requiredModule)
44+
})
4545
}
4646
}
4747

@@ -52,10 +52,10 @@ class Codecept {
5252
* @param {string} dir
5353
*/
5454
init(dir) {
55-
this.initGlobals(dir);
55+
this.initGlobals(dir)
5656
// initializing listeners
57-
container.create(this.config, this.opts);
58-
this.runHooks();
57+
container.create(this.config, this.opts)
58+
this.runHooks()
5959
}
6060

6161
/**
@@ -64,37 +64,37 @@ class Codecept {
6464
* @param {string} dir
6565
*/
6666
initGlobals(dir) {
67-
global.codecept_dir = dir;
68-
global.output_dir = fsPath.resolve(dir, this.config.output);
67+
global.codecept_dir = dir
68+
global.output_dir = fsPath.resolve(dir, this.config.output)
6969

70-
if (this.config.emptyOutputFolder) emptyFolder(global.output_dir);
70+
if (this.config.emptyOutputFolder) emptyFolder(global.output_dir)
7171

7272
if (!this.config.noGlobals) {
73-
global.Helper = global.codecept_helper = require('@codeceptjs/helper');
74-
global.actor = global.codecept_actor = require('./actor');
75-
global.pause = require('./pause');
76-
global.within = require('./within');
77-
global.session = require('./session');
78-
global.DataTable = require('./data/table');
79-
global.locate = locator => require('./locator').build(locator);
80-
global.inject = container.support;
81-
global.share = container.share;
82-
global.secret = require('./secret').secret;
83-
global.codecept_debug = output.debug;
84-
global.codeceptjs = require('./index'); // load all objects
73+
global.Helper = global.codecept_helper = require('@codeceptjs/helper')
74+
global.actor = global.codecept_actor = require('./actor')
75+
global.pause = require('./pause')
76+
global.within = require('./within')
77+
global.session = require('./session')
78+
global.DataTable = require('./data/table')
79+
global.locate = locator => require('./locator').build(locator)
80+
global.inject = container.support
81+
global.share = container.share
82+
global.secret = require('./secret').secret
83+
global.codecept_debug = output.debug
84+
global.codeceptjs = require('./index') // load all objects
8585

8686
// BDD
87-
const stepDefinitions = require('./mocha/bdd');
88-
global.Given = stepDefinitions.Given;
89-
global.When = stepDefinitions.When;
90-
global.Then = stepDefinitions.Then;
91-
global.DefineParameterType = stepDefinitions.defineParameterType;
87+
const stepDefinitions = require('./mocha/bdd')
88+
global.Given = stepDefinitions.Given
89+
global.When = stepDefinitions.When
90+
global.Then = stepDefinitions.Then
91+
global.DefineParameterType = stepDefinitions.defineParameterType
9292

9393
// debug mode
94-
global.debugMode = false;
94+
global.debugMode = false
9595

9696
// mask sensitive data
97-
global.maskSensitiveData = this.config.maskSensitiveData || false;
97+
global.maskSensitiveData = this.config.maskSensitiveData || false
9898
}
9999
}
100100

@@ -103,32 +103,33 @@ class Codecept {
103103
*/
104104
runHooks() {
105105
// default hooks
106-
runHook(require('./listener/steps'));
107-
runHook(require('./listener/artifacts'));
108-
runHook(require('./listener/config'));
109-
runHook(require('./listener/helpers'));
110-
runHook(require('./listener/retry'));
111-
runHook(require('./listener/timeout'));
112-
runHook(require('./listener/exit'));
106+
runHook(require('./listener/store'))
107+
runHook(require('./listener/steps'))
108+
runHook(require('./listener/artifacts'))
109+
runHook(require('./listener/config'))
110+
runHook(require('./listener/helpers'))
111+
runHook(require('./listener/globalTimeout'))
112+
runHook(require('./listener/globalRetry'))
113+
runHook(require('./listener/exit'))
113114

114115
// custom hooks (previous iteration of plugins)
115-
this.config.hooks.forEach(hook => runHook(hook));
116+
this.config.hooks.forEach(hook => runHook(hook))
116117
}
117118

118119
/**
119120
* Executes bootstrap.
120121
*
121122
*/
122123
async bootstrap() {
123-
return runHook(this.config.bootstrap, 'bootstrap');
124+
return runHook(this.config.bootstrap, 'bootstrap')
124125
}
125126

126127
/**
127128
* Executes teardown.
128129
129130
*/
130131
async teardown() {
131-
return runHook(this.config.teardown, 'teardown');
132+
return runHook(this.config.teardown, 'teardown')
132133
}
133134

134135
/**
@@ -139,42 +140,42 @@ class Codecept {
139140
loadTests(pattern) {
140141
const options = {
141142
cwd: global.codecept_dir,
142-
};
143+
}
143144

144-
let patterns = [pattern];
145+
let patterns = [pattern]
145146
if (!pattern) {
146-
patterns = [];
147+
patterns = []
147148

148149
// If the user wants to test a specific set of test files as an array or string.
149150
if (this.config.tests && !this.opts.features) {
150151
if (Array.isArray(this.config.tests)) {
151-
patterns.push(...this.config.tests);
152+
patterns.push(...this.config.tests)
152153
} else {
153-
patterns.push(this.config.tests);
154+
patterns.push(this.config.tests)
154155
}
155156
}
156157

157158
if (this.config.gherkin.features && !this.opts.tests) {
158159
if (Array.isArray(this.config.gherkin.features)) {
159160
this.config.gherkin.features.forEach(feature => {
160-
patterns.push(feature);
161-
});
161+
patterns.push(feature)
162+
})
162163
} else {
163-
patterns.push(this.config.gherkin.features);
164+
patterns.push(this.config.gherkin.features)
164165
}
165166
}
166167
}
167168

168169
for (pattern of patterns) {
169170
glob.sync(pattern, options).forEach(file => {
170-
if (file.includes('node_modules')) return;
171+
if (file.includes('node_modules')) return
171172
if (!fsPath.isAbsolute(file)) {
172-
file = fsPath.join(global.codecept_dir, file);
173+
file = fsPath.join(global.codecept_dir, file)
173174
}
174175
if (!this.testFiles.includes(fsPath.resolve(file))) {
175-
this.testFiles.push(fsPath.resolve(file));
176+
this.testFiles.push(fsPath.resolve(file))
176177
}
177-
});
178+
})
178179
}
179180
}
180181

@@ -185,36 +186,36 @@ class Codecept {
185186
* @returns {Promise<void>}
186187
*/
187188
async run(test) {
188-
await container.started();
189+
await container.started()
189190

190191
return new Promise((resolve, reject) => {
191-
const mocha = container.mocha();
192-
mocha.files = this.testFiles;
192+
const mocha = container.mocha()
193+
mocha.files = this.testFiles
193194
if (test) {
194195
if (!fsPath.isAbsolute(test)) {
195-
test = fsPath.join(global.codecept_dir, test);
196+
test = fsPath.join(global.codecept_dir, test)
196197
}
197-
mocha.files = mocha.files.filter(t => fsPath.basename(t, '.js') === test || t === test);
198+
mocha.files = mocha.files.filter(t => fsPath.basename(t, '.js') === test || t === test)
198199
}
199200
const done = () => {
200-
event.emit(event.all.result, this);
201-
event.emit(event.all.after, this);
202-
resolve();
203-
};
201+
event.emit(event.all.result, this)
202+
event.emit(event.all.after, this)
203+
resolve()
204+
}
204205

205206
try {
206-
event.emit(event.all.before, this);
207-
mocha.run(() => done());
207+
event.emit(event.all.before, this)
208+
mocha.run(() => done())
208209
} catch (e) {
209-
output.error(e.stack);
210-
reject(e);
210+
output.error(e.stack)
211+
reject(e)
211212
}
212-
});
213+
})
213214
}
214215

215216
static version() {
216-
return JSON.parse(readFileSync(`${__dirname}/../package.json`, 'utf8')).version;
217+
return JSON.parse(readFileSync(`${__dirname}/../package.json`, 'utf8')).version
217218
}
218219
}
219220

220-
module.exports = Codecept;
221+
module.exports = Codecept
File renamed without changes.
File renamed without changes.

lib/listener/store.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const event = require('../event')
2+
const store = require('../store')
3+
4+
module.exports = function () {
5+
event.dispatcher.on(event.test.before, test => {
6+
store.currentTest = test
7+
})
8+
9+
event.dispatcher.on(event.test.finished, test => {
10+
store.currentTest = null
11+
})
12+
}

lib/mocha/featureConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class FeatureConfig {
6060
if (tagName[0] !== '@') tagName = `@${tagName}`
6161
if (!this.suite.tags) this.suite.tags = []
6262
this.suite.tags.push(tagName)
63+
this.suite.title = `${this.suite.title.trim()} ${tagName}`
6364
return this
6465
}
6566
}

lib/mocha/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const Suite = require('mocha/lib/suite')
2+
const Test = require('mocha/lib/test')
3+
const { BeforeHook, AfterHook, BeforeSuiteHook, AfterSuiteHook } = require('./hooks')
4+
5+
module.exports = {
6+
Suite,
7+
Test,
8+
BeforeHook,
9+
AfterHook,
10+
BeforeSuiteHook,
11+
AfterSuiteHook,
12+
}

lib/mocha/scenario.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ module.exports.test = test => {
4040
return test
4141
}
4242

43-
test.steps = []
4443
test.timeout(0)
4544
test.async = true
4645

0 commit comments

Comments
 (0)