Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Path Inconsistency in container.js (Introduced in v3.7.2) #4863

@mjalav

Description

@mjalav

Problem Description
Starting from CodeceptJS v3.7.2, there is a path inconsistency issue in node_modules/codeceptjs/lib/container.js, specifically in the function that generates folderPath.

When passing paths = "./automation/**/*.js", the code attempts to generate the folderPath by joining it with global.codecept_dir.
The existing logic:
const folderPath = paths.startsWith('.') ? path.join(global.codecept_dir, paths) : '';
causes an issue on Windows, where it generates a mix of backslashes (\) and forward slashes (/), resulting in:
"D:\\Project/automation/**/*.js"
This breaks globSync, preventing it from finding step definition files.
Version History
✅ v3.6.10 and earlier: This issue did not exist, and step definitions were found correctly.
❌ v3.7.0 and later: The mixed path format prevents globSync from resolving files.

Here is the diff that solved my problem:

diff --git a/node_modules/codeceptjs/lib/container.js b/node_modules/codeceptjs/lib/container.js
index 66c7c16..d986dac 100644
--- a/node_modules/codeceptjs/lib/container.js
+++ b/node_modules/codeceptjs/lib/container.js
@@ -469,8 +469,9 @@ function loadGherkinSteps(paths) {
       loadSupportObject(path, `Step Definition from ${path}`)
     }
   } else {
-    const folderPath = paths.startsWith('.') ? path.join(global.codecept_dir, paths) : ''
+    const folderPath = paths.startsWith('.') ? normalizeAndJoin(global.codecept_dir, paths) : ''
     if (folderPath !== '') {
       globSync(folderPath).forEach(file => {
         loadSupportObject(file, `Step Definition from ${file}`)
       })
@@ -562,3 +563,16 @@ function getHelperModuleName(helperName, config) {
   // built-in helpers
   return `./helper/${helperName}`
 }
+
+function normalizeAndJoin(basePath, subPath) {
+  // Normalize the path (handles extra slashes and resolves `..`)
+  let normalizedBase = path.normalize(basePath);
+  let normalizedSub = path.normalize(subPath);
+
+  // Replace backslashes with forward slashes
+  normalizedBase = normalizedBase.replace(/\\/g, '/');
+  normalizedSub = normalizedSub.replace(/\\/g, '/');
+
+  // Join the paths using POSIX-style
+  return path.posix.join(normalizedBase, normalizedSub);
+}
\ No newline at end of file

This allows configurations like the following to work correctly in codecept.conf.js:

gherkin: {
  features: `./appium/e2e/**/*.feature`, 
  steps: `./appium/e2e/**/*.js`, 
}

Currently, this does not work on Windows because globSync cannot resolve the mixed path format. With this change, step definitions will be found properly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions