Skip to content

Commit 4e28312

Browse files
committed
fix: more UTs
1 parent 024ec5b commit 4e28312

File tree

19 files changed

+193
-158
lines changed

19 files changed

+193
-158
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"arrow-body-style": 0,
4343
"no-loop-func": 0,
4444
"arrow-parens": 0,
45-
"default-param-last": 0
45+
"default-param-last": 0,
46+
"import/extensions": 0
4647
}
4748
}

lib/container.js

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import glob from 'glob';
22
import path from 'path';
33
import { MetaStep } from './step.js';
4-
import { fileExists, isFunction, isAsyncFunction } from './utils.js';
4+
import {fileExists, isFunction, isAsyncFunction, deepMerge} from './utils.js';
55
import Translation from './translation.js';
66
import MochaFactory from './mochaFactory.js';
77
import recorder from './recorder.js';
88
import * as event from './event.js';
99
import WorkerStorage from './workerStorage.js';
1010
import { store } from './store.js';
1111

12+
import actor from "./actor.js";
13+
import { createRequire } from 'module';
14+
const require = createRequire(import.meta.url);
15+
1216
let container = {
1317
helpers: {},
1418
support: {},
@@ -32,7 +36,7 @@ class Container {
3236
* @param {*} config
3337
* @param {*} opts
3438
*/
35-
static create(config, opts) {
39+
static async create(config, opts) {
3640
const mochaConfig = config.mocha || {};
3741
if (config.grep && !opts.grep) {
3842
mochaConfig.grep = config.grep;
@@ -41,9 +45,9 @@ class Container {
4145
container.mocha = MochaFactory.create(mochaConfig, opts || {});
4246
};
4347
this.createMocha();
44-
container.helpers = createHelpers(config.helpers || {});
48+
container.helpers = await createHelpers(config.helpers || {});
4549
container.translation = loadTranslation(config.translation || null, config.vocabularies || []);
46-
container.support = createSupportObjects(config.include || {});
50+
container.support = await createSupportObjects(config.include || {});
4751
container.plugins = createPlugins(config.plugins || {}, opts);
4852
if (config.gherkin) loadGherkinSteps(config.gherkin.steps || []);
4953
if (opts && typeof opts.timeouts === 'boolean') store.timeouts = opts.timeouts;
@@ -117,7 +121,6 @@ class Container {
117121
* @param {Object<string, *>} newContainer
118122
*/
119123
static append(newContainer) {
120-
const deepMerge = require('./utils.js').deepMerge;
121124
container = deepMerge(container, newContainer);
122125
}
123126

@@ -151,7 +154,7 @@ class Container {
151154

152155
export default Container;
153156

154-
function createHelpers(config) {
157+
async function createHelpers(config) {
155158
const helpers = {};
156159
let moduleName;
157160
for (const helperName in config) {
@@ -163,20 +166,25 @@ function createHelpers(config) {
163166
moduleName = config[helperName].require; // plugin helper
164167
}
165168
} else {
166-
moduleName = `./helper/${helperName}`; // built-in helper
169+
moduleName = `./helper/${helperName}.js`; // built-in helper
167170
}
168171

169172
// @ts-ignore
170173
let HelperClass;
171174
// check if the helper is the built-in, use the require() syntax.
172175
if (moduleName.startsWith('./helper/')) {
173-
HelperClass = require(moduleName);
176+
HelperClass = await import(path.resolve('lib', moduleName)).then(name => {
177+
return name.default;
178+
});
174179
} else {
175180
// check if the new syntax export default HelperName is used and loads the Helper, otherwise loads the module that used old syntax export = HelperName.
176-
HelperClass = require(moduleName).default || require(moduleName);
181+
// HelperClass = import(moduleName) //require(moduleName).default || require(moduleName);
182+
HelperClass = await import(path.resolve(moduleName)).then(name => {
183+
return name.default;
184+
});
177185
}
178186

179-
if (HelperClass._checkRequirements) {
187+
if (HelperClass && HelperClass._checkRequirements) {
180188
const requirements = HelperClass._checkRequirements();
181189
if (requirements) {
182190
let install;
@@ -201,15 +209,15 @@ function createHelpers(config) {
201209
return helpers;
202210
}
203211

204-
function createSupportObjects(config) {
212+
async function createSupportObjects(config) {
205213
const objects = {};
206214

207215
for (const name in config) {
208216
objects[name] = {}; // placeholders
209217
}
210218

211219
if (!config.I) {
212-
objects.I = require('./actor')();
220+
objects.I = actor();
213221

214222
if (container.translation.I !== 'I') {
215223
objects[container.translation.I] = objects.I;
@@ -218,8 +226,8 @@ function createSupportObjects(config) {
218226

219227
container.support = objects;
220228

221-
function lazyLoad(name) {
222-
let newObj = getSupportObject(config, name);
229+
async function lazyLoad(name) {
230+
let newObj = await getSupportObject(config, name);
223231
try {
224232
if (typeof newObj === 'function') {
225233
newObj = newObj();
@@ -229,6 +237,7 @@ function createSupportObjects(config) {
229237
} catch (err) {
230238
throw new Error(`Initialization failed for ${name}: ${newObj}\n${err.message}\n${err.stack}`);
231239
}
240+
console.log('----', newObj)
232241
return newObj;
233242
}
234243

@@ -258,7 +267,7 @@ function createSupportObjects(config) {
258267
ownKeys() {
259268
return Reflect.ownKeys(config);
260269
},
261-
get(target, key) {
270+
async get(target, key) {
262271
// configured but not in support object, yet: load the module
263272
if (key in objects && !(key in target)) {
264273
// load default I
@@ -267,7 +276,7 @@ function createSupportObjects(config) {
267276
}
268277

269278
// load new object
270-
const object = lazyLoad(key);
279+
const object = await lazyLoad(key);
271280
// check that object is a real object and not an array
272281
if (Object.prototype.toString.call(object) === '[object Object]') {
273282
return target[key] = Object.assign(objects[key], object);
@@ -306,7 +315,7 @@ function createPlugins(config, options = {}) {
306315
return plugins;
307316
}
308317

309-
function getSupportObject(config, name) {
318+
async function getSupportObject(config, name) {
310319
const module = config[name];
311320
if (typeof module === 'string') {
312321
return loadSupportObject(module, name);
@@ -340,12 +349,14 @@ function loadGherkinSteps(paths) {
340349
delete global.Fail;
341350
}
342351

343-
function loadSupportObject(modulePath, supportObjectName) {
352+
async function loadSupportObject(modulePath, supportObjectName) {
344353
if (modulePath.charAt(0) === '.') {
345354
modulePath = path.join(global.codecept_dir, modulePath);
346355
}
347356
try {
348-
const obj = require(modulePath);
357+
const obj = await import(modulePath).then(name => {
358+
return name.default;
359+
});
349360

350361
if (typeof obj === 'function') {
351362
const fobj = obj();
@@ -422,6 +433,7 @@ function loadTranslation(locale, vocabularies) {
422433
}
423434

424435
let translation;
436+
locale = locale.replace('-', '_');
425437

426438
// check if it is a known translation
427439
if (Translation.langs[locale]) {

lib/helper/Expect.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import output from '../output.js';
22

3-
let expect;
4-
5-
import('chai').then(chai => {
6-
expect = chai.expect;
7-
chai.use(require('chai-string'));
8-
// @ts-ignore
9-
chai.use(require('chai-exclude'));
10-
chai.use(require('chai-match-pattern'));
11-
chai.use(require('chai-json-schema'));
12-
});
3+
import { expect } from 'chai';
4+
import * as chai from 'chai';
5+
import chai_json_schema from "chai-json-schema";
6+
import chai_match_pattern from "chai-match-pattern";
7+
import chai_exclude from "chai-exclude";
8+
import chai_string from "chai-string";
9+
10+
chai.use(chai_string);
11+
// @ts-ignore
12+
chai.use(chai_exclude);
13+
chai.use(chai_match_pattern);
14+
chai.use(chai_json_schema);
1315

1416
/**
1517
* This helper allows performing assertions based on Chai.

lib/interfaces/bdd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CucumberExpression, ParameterTypeRegistry, ParameterType } from '@cucum
22
import Config from '../config.js';
33

44
let steps = {};
5+
const codecept_dir = '';
56

67
const STACK_POSITION = 2;
78

lib/interfaces/gherkin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import Step from '../step.js';
1212
import DataTableArgument from '../data/dataTableArgument.js';
1313
import transform from '../transform.js';
1414

15+
import translations0 from "../translation.js";
16+
1517
const uuidFn = Messages.IdGenerator.uuid();
1618
const builder = new Gherkin.AstBuilder(uuidFn);
1719
const matcher = new Gherkin.GherkinClassicTokenMatcher();
@@ -178,15 +180,15 @@ function addExampleInTable(exampleSteps, placeholders) {
178180
}
179181

180182
function getTranslation(language) {
181-
const translations = Object.keys(require('../../translations'));
183+
const translations = Object.keys(translations0);
182184

183185
for (const availableTranslation of translations) {
184186
if (!language) {
185187
break;
186188
}
187189

188190
if (availableTranslation.includes(language)) {
189-
return require('../../translations')[availableTranslation];
191+
return translations0[availableTranslation];
190192
}
191193
}
192194
}

lib/mochaFactory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import { fileURLToPath } from 'url';
1010
const __filename = fileURLToPath(import.meta.url);
1111
const __dirname = fsPath.dirname(__filename);
1212

13-
const scenarioUi = fsPath.join(__dirname, './ui.js');
13+
import scenarioUi from './ui.js';
14+
//const scenarioUi = fsPath.join(__dirname, './ui.js');
1415

1516
let mocha;
1617

1718
export default class MochaFactory {
1819
static create(config, opts) {
1920
mocha = new Mocha(Object.assign(config, opts));
2021
output.process(opts.child);
21-
console.log(mocha.ui('ui'))
2222
mocha.ui(scenarioUi);
2323

2424
Mocha.Runner.prototype.uncaught = function (err) {

lib/translation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import merge from 'lodash.merge';
22
import path from 'path';
33

4+
import * as translations from "../translations/index.js";
5+
6+
import { createRequire } from 'module';
7+
const require = createRequire(import.meta.url);
8+
49
const defaultVocabulary = {
510
I: 'I',
611
actions: {},
@@ -40,7 +45,7 @@ class Translation {
4045
}
4146

4247
static get langs() {
43-
return require('../translations');
48+
return translations;
4449
}
4550

4651
static createDefault() {

lib/ui.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const setContextTranslation = (context) => {
3535
* @param {Mocha.Suite} suite Root suite.
3636
* @ignore
3737
*/
38-
export default function(suite) {
38+
const suite = (suite) => {
3939
const suites = [suite];
4040
suite.timeout(0);
4141
let afterAllHooks;
@@ -234,4 +234,6 @@ export default function(suite) {
234234
afterAllHooksAreLoaded = true;
235235
}
236236
});
237-
}
237+
};
238+
239+
export default suite;

lib/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import deepClone from 'lodash.clonedeep';
66
import merge from 'lodash.merge';
77
import { createHash } from 'crypto';
88
import { convertColorToRGBA, isColorProperty } from './colorUtils.js';
9+
import format from "js-beautify";
10+
11+
import { createRequire } from "module";
12+
const require = createRequire(import.meta.url);
13+
914
const __dirname = path.resolve();
1015

1116
export function deepMerge(target, source) {
@@ -297,7 +302,6 @@ export const screenshotOutputFolder = function (fileName) {
297302
};
298303

299304
export const beautify = function (code) {
300-
const format = require('js-beautify').js;
301305
return format(code, { indent_size: 2, space_in_empty_paren: true });
302306
};
303307

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
"test:unit:webbapi:webDriver:noSeleniumServer": "mocha test/helper/WebDriver.noSeleniumServer_test.js",
5656
"test:unit:webbapi:webDriver:devtools": "mocha test/helper/WebDriver_devtools_test.js --exit",
5757
"test:unit:webbapi:testCafe": "mocha test/helper/TestCafe_test.js",
58-
"test:unit:expect": "mocha test/helper/Expect_test.js",
58+
"test:unit:expect": "mocha test/helper/Expect_test.js --exit",
5959
"test:unit:mockServer": "mocha test/helper/MockServer_test.js",
6060
"test:plugin": "mocha test/plugin/plugin_test.js",
6161
"def": "./runok.js def",

0 commit comments

Comments
 (0)