Skip to content

Commit

Permalink
Updating recursive testing of JavaScript files.
Browse files Browse the repository at this point in the history
  • Loading branch information
erinesullivan committed Feb 21, 2025
1 parent f443af2 commit 24089f8
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions test/find-specs.spec.js
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;
});
});
});

0 comments on commit 24089f8

Please sign in to comment.