Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Add findTestHelpers() to find helpers and fixtures #8

Merged
merged 1 commit into from
Dec 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\'');
Expand Down Expand Up @@ -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 + '/**/*';
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
```


Expand Down Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});