-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve file field correctly for exports mocha interface (#949)
- Loading branch information
Showing
2 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// When using "exports" mocha interface, "file" field is absent on suites, and available on tests only. | ||
// This helper tries to resolve "file" field for suites, drilling down to child tests and using their file field. | ||
|
||
const findTopmostSuite = mochaSuite => { | ||
if (mochaSuite.parent && mochaSuite.parent.root) { | ||
return mochaSuite; | ||
} | ||
|
||
if (!mochaSuite.parent) { | ||
return null; | ||
} | ||
|
||
return findTopmostSuite(mochaSuite.parent); | ||
}; | ||
|
||
const getFile = mochaSuite => { | ||
if (mochaSuite.file) { | ||
return mochaSuite.file; | ||
} | ||
|
||
if (mochaSuite.tests.length > 0 && mochaSuite.tests[0].file) { | ||
return mochaSuite.tests[0].file; | ||
} | ||
|
||
for (const childSuite of mochaSuite.suites) { | ||
const computedFile = getFile(childSuite); | ||
if (computedFile) { | ||
return computedFile; | ||
} | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
const fillSuitesFileField = (mochaSuite, file) => { | ||
mochaSuite.file = file; | ||
|
||
if (mochaSuite.suites) { | ||
for (const childSuite of mochaSuite.suites) { | ||
fillSuitesFileField(childSuite, file); | ||
} | ||
} | ||
}; | ||
|
||
const computeFile = mochaSuite => { | ||
if (mochaSuite.file) { | ||
return mochaSuite.file; | ||
} | ||
|
||
const topmostSuite = findTopmostSuite(mochaSuite); | ||
const file = topmostSuite && getFile(topmostSuite); | ||
|
||
if (topmostSuite && file) { | ||
fillSuitesFileField(topmostSuite, file); | ||
|
||
return file; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
module.exports = { | ||
computeFile, | ||
}; |