generated from mlibrary/ruby-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating recursive testing of JavaScript files.
- Loading branch information
1 parent
f443af2
commit 24089f8
Showing
1 changed file
with
25 additions
and
21 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,31 @@ | ||
import { existsSync, readdirSync } from 'fs'; | ||
import { extname, join } from 'path'; | ||
import { expect } from 'chai'; | ||
import fs from 'fs'; | ||
|
||
const deepSearch = (path) => { | ||
fs.readdirSync(path).forEach((listing) => { | ||
const listingPath = `${path}/${listing}`; | ||
// Keep searching until the current listing is not a directory | ||
if (!listing.includes('.')) { | ||
deepSearch(listingPath); | ||
} | ||
// Check if current listing is a JavaScript file | ||
if (listing.endsWith('.js')) { | ||
// Convert current listing path into matching spec path | ||
let specPath = path.split('/'); | ||
specPath[0] = 'test'; | ||
specPath = `${specPath.join('/')}/${listing.slice(0, -3)}.spec.js`; | ||
// Return test to see if the spec exists | ||
it(`'${listingPath}' has a spec file ('${specPath}')`, function () { | ||
expect(fs.existsSync(`${specPath}`)).to.be.true; | ||
}); | ||
const getJsFilesRecursively = (directory = 'assets/scripts') => { | ||
let jsFiles = []; | ||
// Read the directory contents | ||
const entries = readdirSync(directory, { withFileTypes: true }); | ||
|
||
for (const entry of entries) { | ||
const fullPath = join(directory, entry.name); | ||
if (entry.isDirectory()) { | ||
// If the entry is a directory, recursively search it | ||
jsFiles = jsFiles.concat(getJsFilesRecursively(fullPath)); | ||
} else if (entry.isFile() && extname(entry.name) === '.js') { | ||
// If the entry is a .js file, add it to the list | ||
jsFiles.push(fullPath); | ||
} | ||
}); | ||
} | ||
|
||
return jsFiles; | ||
}; | ||
|
||
describe('checks if component files have associated specs', function () { | ||
deepSearch('assets/scripts'); | ||
describe('spec files exist', function () { | ||
it('should have a spec file for every JavaScript file', function () { | ||
getJsFilesRecursively().forEach((filePath) => { | ||
const testFile = filePath.replace('assets/', 'test/').replace('.js', '.spec.js'); | ||
expect(existsSync(testFile), `\`${filePath}\` should have a spec file located at: \`${testFile}\``).to.be.true; | ||
}); | ||
}); | ||
}); |