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

Commit 111ecde

Browse files
author
vdemedes
committed
add findTestHelpers() to find helper files
1 parent f4daaa5 commit 111ecde

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

index.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ function defaultIncludePatterns() {
2727
];
2828
}
2929

30+
function defaultHelperPatterns() {
31+
return [
32+
'**/__tests__/helpers/**/*.js',
33+
'**/__tests__/**/_*.js',
34+
'**/test/helpers/**/*.js',
35+
'**/test/**/_*.js'
36+
];
37+
}
38+
3039
function AvaFiles(options) {
3140
if (!(this instanceof AvaFiles)) {
3241
throw new TypeError('Class constructor AvaFiles cannot be invoked without \'new\'');
@@ -58,6 +67,17 @@ AvaFiles.prototype.findTestFiles = function () {
5867
});
5968
};
6069

70+
AvaFiles.prototype.findTestHelpers = function () {
71+
return handlePaths(defaultHelperPatterns(), ['!**/node_modules/**'], {
72+
cwd: this.cwd,
73+
includeUnderscoredFiles: true,
74+
cache: Object.create(null),
75+
statCache: Object.create(null),
76+
realpathCache: Object.create(null),
77+
symlinks: Object.create(null)
78+
});
79+
};
80+
6181
function getDefaultIgnorePatterns() {
6282
return defaultIgnore.map(function (dir) {
6383
return dir + '/**/*';
@@ -257,7 +277,14 @@ function handlePaths(files, excludePatterns, globOptions) {
257277
})
258278
.then(flatten)
259279
.filter(function (file) {
260-
return file && path.extname(file) === '.js' && path.basename(file)[0] !== '_';
280+
return file && path.extname(file) === '.js';
281+
})
282+
.filter(function (file) {
283+
if (path.basename(file)[0] === '_' && globOptions.includeUnderscoredFiles !== true) {
284+
return false;
285+
}
286+
287+
return true;
261288
})
262289
.map(function (file) {
263290
return path.resolve(file);

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ avaFiles.isSource(filePath);
3030
avaFiles.findTestFiles().then(files => {
3131
// files is an array of found test files
3232
});
33+
34+
avaFiles.findTestHelpers().then(files => {
35+
// files is an array of found test helpers
36+
});
3337
```
3438

3539

@@ -94,6 +98,10 @@ Path to the file.
9498

9599
Returns a `Promise` for an `Array` of `string` paths to the found test files.
96100

101+
### avaFiles.findTestHelpers()
102+
103+
Returns a `Promise` for an `Array` of `string` paths to the found helper files.
104+
97105

98106
## License
99107

test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,20 @@ test('findFiles - finds the correct files by default', async t => {
139139
files.sort();
140140
t.deepEqual(files, expected);
141141
});
142+
143+
test('findTestHelpers - finds the test helpers', async t => {
144+
const fixtureDir = fixture('default-patterns');
145+
process.chdir(fixtureDir);
146+
147+
const expected = [
148+
'sub/directory/__tests__/helpers/foo.js',
149+
'sub/directory/__tests__/_foo.js',
150+
'test/helpers/test.js',
151+
'test/_foo-help.js'
152+
].sort().map(file => path.join(fixtureDir, file));
153+
154+
const avaFiles = new AvaFiles();
155+
156+
const files = await avaFiles.findTestHelpers();
157+
t.deepEqual(files.sort(), expected);
158+
});

0 commit comments

Comments
 (0)