Skip to content

Commit d3b1228

Browse files
committed
chore: Start ESM conversion - Phase 1
Convert core library files to ES modules: - Add 'type': 'module' to package.json - Convert lib/codecept.js: import statements, export default - Convert lib/event.js: export default - Convert lib/output.js: import statements, export default - Convert lib/utils.js: import statements, export named exports - Convert lib/utils/mask_data.js: export named exports Changes: - Replace require() with import - Replace module.exports with export/export default - Add .js extensions to local imports - Add __filename/__dirname polyfills using import.meta.url Note: TypeScript errors are expected and will be resolved in later phases. Next: Convert helper files, container, workers, and test files.
1 parent 8d37a99 commit d3b1228

File tree

6 files changed

+34
-32
lines changed

6 files changed

+34
-32
lines changed

lib/codecept.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const { existsSync, readFileSync } = require('fs')
2-
const { globSync } = require('glob')
3-
const shuffle = require('lodash.shuffle')
4-
const fsPath = require('path')
5-
const { resolve } = require('path')
1+
import { existsSync, readFileSync } from 'fs'
2+
import { globSync } from 'glob'
3+
import shuffle from 'lodash.shuffle'
4+
import fsPath from 'path'
5+
import { resolve } from 'path'
6+
import { fileURLToPath } from 'url'
7+
import { dirname } from 'path'
68

79
const __filename = fileURLToPath(import.meta.url)
810
const __dirname = dirname(__filename)
9-
const require = createRequire(import.meta.url)
1011

1112
import Helper from '@codeceptjs/helper'
1213
import container from './container.js'

lib/event.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (typeof process.setMaxListeners === 'function') {
1919
* @alias event
2020
*/
2121

22-
module.exports = {
22+
export default {
2323
/**
2424
* @type {NodeJS.EventEmitter}
2525
* @constant

lib/output.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const colors = require('chalk')
2-
const figures = require('figures')
3-
const { maskData, shouldMaskData, getMaskConfig } = require('./utils/mask_data')
1+
import colors from 'chalk'
2+
import figures from 'figures'
3+
import { maskData, shouldMaskData, getMaskConfig } from './utils/mask_data.js'
44

55
const styles = {
66
error: colors.bgRed.white.bold,
@@ -304,6 +304,8 @@ const output = {
304304
},
305305
}
306306

307+
export default output
308+
307309
function print(...msg) {
308310
if (outputProcess) {
309311
msg.unshift(outputProcess)

lib/utils.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
const fs = require('fs')
2-
const os = require('os')
3-
const path = require('path')
4-
const chalk = require('chalk')
5-
const getFunctionArguments = require('fn-args')
6-
const deepClone = require('lodash.clonedeep')
7-
const { convertColorToRGBA, isColorProperty } = require('./colorUtils')
8-
const Fuse = require('fuse.js')
9-
const { spawnSync } = require('child_process')
1+
import fs from 'fs'
2+
import os from 'os'
3+
import path from 'path'
4+
import { createRequire } from 'module'
5+
import chalk from 'chalk'
6+
import getFunctionArguments from 'fn-args'
7+
import deepClone from 'lodash.clonedeep'
8+
import merge from 'lodash.merge'
9+
import { convertColorToRGBA, isColorProperty } from './colorUtils.js'
10+
import Fuse from 'fuse.js'
11+
import crypto from 'crypto'
12+
import jsBeautify from 'js-beautify'
13+
import { spawnSync } from 'child_process'
1014

1115
function deepMerge(target, source) {
1216
return merge(target, source)
@@ -474,7 +478,7 @@ export const isNotSet = function (obj) {
474478
return false
475479
}
476480

477-
module.exports.emptyFolder = directoryPath => {
481+
export const emptyFolder = directoryPath => {
478482
// Do not throw on non-existent directory, since it may be created later
479483
if (!fs.existsSync(directoryPath)) return
480484
for (const file of fs.readdirSync(directoryPath)) {
@@ -615,7 +619,7 @@ function createCircularSafeReplacer(keysToSkip = []) {
615619
* @param {number} space - Number of spaces for indentation (default: 0)
616620
* @returns {string} JSON string representation
617621
*/
618-
module.exports.safeStringify = function (obj, keysToSkip = [], space = 0) {
622+
export const safeStringify = function (obj, keysToSkip = [], space = 0) {
619623
try {
620624
return JSON.stringify(obj, createCircularSafeReplacer(keysToSkip), space)
621625
} catch (error) {
@@ -624,7 +628,7 @@ module.exports.safeStringify = function (obj, keysToSkip = [], space = 0) {
624628
}
625629
}
626630

627-
module.exports.serializeError = function (error) {
631+
export const serializeError = function (error) {
628632
if (error) {
629633
const { stack, uncaught, message, actual, expected } = error
630634
return { stack, uncaught, message, actual, expected }

lib/utils/mask_data.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { maskSensitiveData } = require('invisi-data')
1+
import { maskSensitiveData } from 'invisi-data'
22

33
/**
44
* Mask sensitive data utility for CodeceptJS
@@ -8,7 +8,7 @@ const { maskSensitiveData } = require('invisi-data')
88
* @param {boolean|object} config - Masking configuration
99
* @returns {string} - Masked string
1010
*/
11-
function maskData(input, config) {
11+
export function maskData(input, config) {
1212
if (!config) {
1313
return input
1414
}
@@ -32,7 +32,7 @@ function maskData(input, config) {
3232
*
3333
* @returns {boolean|object} - Current masking configuration
3434
*/
35-
function getMaskConfig() {
35+
export function getMaskConfig() {
3636
return global.maskSensitiveData || false
3737
}
3838

@@ -41,13 +41,7 @@ function getMaskConfig() {
4141
*
4242
* @returns {boolean} - True if masking is enabled
4343
*/
44-
function shouldMaskData() {
44+
export function shouldMaskData() {
4545
const config = getMaskConfig()
4646
return config === true || (typeof config === 'object' && config.enabled === true)
4747
}
48-
49-
module.exports = {
50-
maskData,
51-
getMaskConfig,
52-
shouldMaskData,
53-
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "codeceptjs",
33
"version": "3.7.5",
4+
"type": "module",
45
"description": "Supercharged End 2 End Testing Framework for NodeJS",
56
"keywords": [
67
"acceptance",

0 commit comments

Comments
 (0)