Skip to content

Commit 57f2ac0

Browse files
author
DavertMik
committed
fixed container issues
1 parent 95f3825 commit 57f2ac0

File tree

9 files changed

+219
-215
lines changed

9 files changed

+219
-215
lines changed

lib/actor.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,29 +70,11 @@ class Actor {
7070
* @ignore
7171
*/
7272
module.exports = function (obj = {}) {
73-
if (!store.actor) {
74-
store.actor = new Actor()
75-
}
76-
const actor = store.actor
77-
78-
const translation = container.translation()
79-
80-
if (Object.keys(obj).length > 0) {
81-
Object.keys(obj).forEach((action) => {
82-
const actionAlias = translation.actionAliasFor(action)
83-
84-
const currentMethod = obj[action]
85-
const ms = new MetaStep('I', action)
86-
if (translation.loaded) {
87-
ms.name = actionAlias
88-
ms.actor = translation.I
89-
}
90-
ms.setContext(actor)
91-
actor[action] = actor[actionAlias] = ms.run.bind(ms, currentMethod)
92-
})
93-
}
73+
const actor = container.actor() || new Actor()
9474

95-
container.started().then(() => {
75+
// load all helpers once container initialized
76+
container.started(() => {
77+
const translation = container.translation()
9678
const helpers = container.helpers()
9779

9880
// add methods from enabled helpers
@@ -114,8 +96,33 @@ module.exports = function (obj = {}) {
11496
}
11597
})
11698
})
99+
100+
// add translated custom steps from actor
101+
Object.keys(obj).forEach((key) => {
102+
const actionAlias = translation.actionAliasFor(key)
103+
if (!actor[actionAlias]) {
104+
actor[actionAlias] = actor[key]
105+
}
106+
})
107+
108+
store.actor = actor
109+
container.append({
110+
support: {
111+
[translation.I]: actor,
112+
I: actor,
113+
},
114+
})
117115
})
118116

117+
// add custom steps from actor
118+
Object.keys(obj).forEach((key) => {
119+
const ms = new MetaStep('I', key)
120+
ms.setContext(actor)
121+
actor[key] = ms.run.bind(ms, obj[key])
122+
})
123+
124+
// store.actor = actor;
125+
119126
return actor
120127
}
121128

lib/codecept.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ class Codecept {
185185
* @returns {Promise<void>}
186186
*/
187187
async run(test) {
188+
await container.started()
189+
188190
return new Promise((resolve, reject) => {
189191
const mocha = container.mocha()
190192
mocha.files = this.testFiles

lib/command/workers/runTests.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,14 @@ const codecept = new Codecept(config, options)
3939
codecept.init(testRoot)
4040
codecept.loadTests()
4141
const mocha = container.mocha()
42-
filterTests()
42+
filterTests();
4343

44-
;(async function () {
44+
// run tests
45+
(async function () {
4546
if (mocha.suite.total()) {
4647
await runTests()
4748
}
48-
})()
49+
})();
4950

5051
async function runTests() {
5152
try {

lib/container.js

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const glob = require('glob')
22
const path = require('path')
3-
const { isPromise } = require('util/types')
43
const { MetaStep } = require('./step')
5-
const { fileExists, isFunction, isAsyncFunction, installedLocally } = require('./utils')
4+
const { methodsOfObject, fileExists, isFunction, isAsyncFunction, installedLocally } = require('./utils')
65
const Translation = require('./translation')
76
const MochaFactory = require('./mochaFactory')
87
const recorder = require('./recorder')
@@ -11,12 +10,13 @@ const WorkerStorage = require('./workerStorage')
1110
const store = require('./store')
1211
const ai = require('./ai')
1312

14-
let asyncHelperPromise = Promise.resolve()
13+
let asyncHelperPromise
1514

1615
let container = {
1716
helpers: {},
1817
support: {},
1918
plugins: {},
19+
actor: null,
2020
/**
2121
* @type {Mocha | {}}
2222
* @ignore
@@ -37,24 +37,25 @@ class Container {
3737
* @param {*} opts
3838
*/
3939
static create(config, opts) {
40-
const mochaConfig = config.mocha || {}
41-
if (config.grep && !opts.grep) {
42-
mochaConfig.grep = config.grep
43-
}
44-
this.createMocha = () => {
45-
container.mocha = MochaFactory.create(mochaConfig, opts || {})
46-
}
47-
this.createMocha()
40+
asyncHelperPromise = Promise.resolve()
41+
4842
container.helpers = createHelpers(config.helpers || {})
4943
container.translation = loadTranslation(config.translation || null, config.vocabularies || [])
5044
container.support = createSupportObjects(config.include || {})
5145
container.plugins = createPlugins(config.plugins || {}, opts)
5246

47+
createMocha(config, opts)
48+
createActor()
49+
5350
if (opts && opts.ai) ai.enable(config.ai) // enable AI Assistant
5451
if (config.gherkin) loadGherkinSteps(config.gherkin.steps || [])
5552
if (opts && typeof opts.timeouts === 'boolean') store.timeouts = opts.timeouts
5653
}
5754

55+
static actor() {
56+
return container.actor
57+
}
58+
5859
/**
5960
* Get all plugins
6061
*
@@ -125,6 +126,7 @@ class Container {
125126
static append(newContainer) {
126127
const deepMerge = require('./utils').deepMerge
127128
container = deepMerge(container, newContainer)
129+
container.actor = container.support.I
128130
}
129131

130132
/**
@@ -139,9 +141,14 @@ class Container {
139141
container.support = newSupport || {}
140142
container.plugins = newPlugins || {}
141143
container.translation = loadTranslation()
144+
asyncHelperPromise = Promise.resolve()
145+
store.actor = null
142146
}
143147

144-
static async started() {
148+
static async started(fn = null) {
149+
if (fn) {
150+
asyncHelperPromise = asyncHelperPromise.then(fn)
151+
}
145152
return asyncHelperPromise
146153
}
147154

@@ -259,14 +266,6 @@ function createSupportObjects(config) {
259266
objects[name] = {} // placeholders
260267
}
261268

262-
if (!config.I) {
263-
objects.I = require('./actor')()
264-
265-
if (container.translation.I !== 'I') {
266-
objects[container.translation.I] = objects.I
267-
}
268-
}
269-
270269
container.support = objects
271270

272271
function lazyLoad(name) {
@@ -361,6 +360,14 @@ function createPlugins(config, options = {}) {
361360
return plugins
362361
}
363362

363+
function createActor() {
364+
const actor = require('./actor')
365+
container.support.I = container.support.I || actor()
366+
367+
container.actor = container.support.I
368+
if (container.translation.I !== 'I') container.support[container.translation.I] = container.actor
369+
}
370+
364371
function getSupportObject(config, name) {
365372
const module = config[name]
366373
if (typeof module === 'string') {
@@ -399,23 +406,12 @@ function loadSupportObject(modulePath, supportObjectName) {
399406
if (modulePath.charAt(0) === '.') {
400407
modulePath = path.join(global.codecept_dir, modulePath)
401408
}
409+
402410
try {
403411
const obj = require(modulePath)
404412

405-
if (typeof obj === 'function') {
406-
const fobj = obj()
407-
408-
if (fobj.constructor.name === 'Actor') {
409-
const methods = getObjectMethods(fobj)
410-
Object.keys(methods).forEach((key) => {
411-
fobj[key] = methods[key]
412-
})
413-
414-
return methods
415-
}
416-
}
417413
if (typeof obj !== 'function' && Object.getPrototypeOf(obj) !== Object.prototype && !Array.isArray(obj)) {
418-
const methods = getObjectMethods(obj)
414+
const methods = methodsOfObject(obj)
419415
Object.keys(methods)
420416
.filter((key) => !key.startsWith('_'))
421417
.forEach((key) => {
@@ -452,22 +448,6 @@ function loadSupportObject(modulePath, supportObjectName) {
452448
/**
453449
* Method collect own property and prototype
454450
*/
455-
function getObjectMethods(obj) {
456-
const methodsSet = new Set()
457-
let protoObj = Reflect.getPrototypeOf(obj)
458-
do {
459-
if (protoObj.constructor.prototype !== Object.prototype) {
460-
const keys = Reflect.ownKeys(protoObj)
461-
keys.forEach((k) => methodsSet.add(k))
462-
}
463-
} while ((protoObj = Reflect.getPrototypeOf(protoObj)))
464-
Reflect.ownKeys(obj).forEach((k) => methodsSet.add(k))
465-
const methods = {}
466-
for (const key of methodsSet.keys()) {
467-
if (key !== 'constructor') methods[key] = obj[key]
468-
}
469-
return methods
470-
}
471451

472452
function loadTranslation(locale, vocabularies) {
473453
if (!locale) {
@@ -509,3 +489,11 @@ function getHelperModuleName(helperName, config) {
509489
// built-in helpers
510490
return `./helper/${helperName}`
511491
}
492+
493+
function createMocha(config, opts) {
494+
const mochaConfig = config.mocha || {}
495+
if (config.grep && !opts.grep) {
496+
mochaConfig.grep = config.grep
497+
}
498+
container.mocha = MochaFactory.create(mochaConfig, opts || {})
499+
}

lib/interfaces/inject.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const parser = require('../parser')
2+
3+
const getInjectedArguments = (fn, test) => {
4+
const container = require('../container')
5+
const testArgs = {}
6+
const params = parser.getParams(fn) || []
7+
const objects = container.support()
8+
for (const key of params) {
9+
testArgs[key] = {}
10+
if (test && test.inject && test.inject[key]) {
11+
// @FIX: need fix got inject
12+
testArgs[key] = test.inject[key]
13+
continue
14+
}
15+
if (!objects[key]) {
16+
throw new Error(`Object of type ${key} is not defined in container`)
17+
}
18+
testArgs[key] = container.support(key)
19+
}
20+
21+
return testArgs
22+
}
23+
24+
module.exports.getInjectedArguments = getInjectedArguments

0 commit comments

Comments
 (0)