diff --git a/index.js b/index.js index 6d51e38..24f4a60 100644 --- a/index.js +++ b/index.js @@ -27,6 +27,15 @@ function defaultIncludePatterns() { ]; } +function defaultHelperPatterns() { + return [ + '**/__tests__/helpers/**/*.js', + '**/__tests__/**/_*.js', + '**/test/helpers/**/*.js', + '**/test/**/_*.js' + ]; +} + function AvaFiles(options) { if (!(this instanceof AvaFiles)) { throw new TypeError('Class constructor AvaFiles cannot be invoked without \'new\''); @@ -58,6 +67,17 @@ AvaFiles.prototype.findTestFiles = function () { }); }; +AvaFiles.prototype.findTestHelpers = function () { + return handlePaths(defaultHelperPatterns(), ['!**/node_modules/**'], { + cwd: this.cwd, + includeUnderscoredFiles: true, + cache: Object.create(null), + statCache: Object.create(null), + realpathCache: Object.create(null), + symlinks: Object.create(null) + }); +}; + function getDefaultIgnorePatterns() { return defaultIgnore.map(function (dir) { return dir + '/**/*'; @@ -257,7 +277,14 @@ function handlePaths(files, excludePatterns, globOptions) { }) .then(flatten) .filter(function (file) { - return file && path.extname(file) === '.js' && path.basename(file)[0] !== '_'; + return file && path.extname(file) === '.js'; + }) + .filter(function (file) { + if (path.basename(file)[0] === '_' && globOptions.includeUnderscoredFiles !== true) { + return false; + } + + return true; }) .map(function (file) { return path.resolve(file); diff --git a/readme.md b/readme.md index 410b482..4a225fb 100644 --- a/readme.md +++ b/readme.md @@ -30,6 +30,10 @@ avaFiles.isSource(filePath); avaFiles.findTestFiles().then(files => { // files is an array of found test files }); + +avaFiles.findTestHelpers().then(files => { + // files is an array of found test helpers +}); ``` @@ -94,6 +98,10 @@ Path to the file. Returns a `Promise` for an `Array` of `string` paths to the found test files. +### avaFiles.findTestHelpers() + +Returns a `Promise` for an `Array` of `string` paths to the found helper files. + ## License diff --git a/test.js b/test.js index b81f040..8a5290d 100644 --- a/test.js +++ b/test.js @@ -139,3 +139,20 @@ test('findFiles - finds the correct files by default', async t => { files.sort(); t.deepEqual(files, expected); }); + +test('findTestHelpers - finds the test helpers', async t => { + const fixtureDir = fixture('default-patterns'); + process.chdir(fixtureDir); + + const expected = [ + 'sub/directory/__tests__/helpers/foo.js', + 'sub/directory/__tests__/_foo.js', + 'test/helpers/test.js', + 'test/_foo-help.js' + ].sort().map(file => path.join(fixtureDir, file)); + + const avaFiles = new AvaFiles(); + + const files = await avaFiles.findTestHelpers(); + t.deepEqual(files.sort(), expected); +});