Skip to content

Commit b54d2c9

Browse files
committed
Deterministic precompilation with extension property
1 parent 84a884e commit b54d2c9

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

api.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ class Api extends EventEmitter {
4343
const apiOptions = this.options;
4444
runtimeOptions = runtimeOptions || {};
4545

46+
const doNotCompileExtensions = apiOptions.extensions || [];
47+
const babelExtensions = apiOptions.babelConfig.extensions || [];
48+
49+
// Combine all extensions possible for testing.
50+
const allExts = [...doNotCompileExtensions, ...babelExtensions];
51+
4652
// Each run will have its own status. It can only be created when test files
4753
// have been found.
4854
let runStatus;
@@ -81,7 +87,7 @@ class Api extends EventEmitter {
8187
}
8288

8389
// Find all test files.
84-
return new AvaFiles({cwd: apiOptions.resolveTestsFrom, files}).findTestFiles()
90+
return new AvaFiles({cwd: apiOptions.resolveTestsFrom, files, extensions: allExts}).findTestFiles()
8591
.then(files => {
8692
runStatus = new RunStatus({
8793
runOnlyExclusive: runtimeOptions.runOnlyExclusive,
@@ -125,21 +131,22 @@ class Api extends EventEmitter {
125131
// helpers from within the `resolveTestsFrom` directory. Without
126132
// arguments this is the `projectDir`, else it's `process.cwd()`
127133
// which may be nested too deeply.
128-
return new AvaFiles({cwd: this.options.resolveTestsFrom}).findTestHelpers().then(helpers => {
129-
return {
130-
cacheDir: precompilation.cacheDir,
131-
map: files.concat(helpers).reduce((acc, file) => {
132-
try {
133-
const realpath = fs.realpathSync(file);
134-
const hash = precompilation.precompiler.precompileFile(realpath);
135-
acc[realpath] = hash;
136-
} catch (err) {
137-
throw Object.assign(err, {file});
138-
}
139-
return acc;
140-
}, {})
141-
};
142-
});
134+
return new AvaFiles({cwd: this.options.resolveTestsFrom, extensions: allExts})
135+
.findTestHelpers().then(helpers => {
136+
return {
137+
cacheDir: precompilation.cacheDir,
138+
map: files.concat(helpers).reduce((acc, file) => {
139+
try {
140+
const realpath = fs.realpathSync(file);
141+
const hash = precompilation.precompiler.precompileFile(realpath, doNotCompileExtensions);
142+
acc[realpath] = hash;
143+
} catch (err) {
144+
throw Object.assign(err, {file});
145+
}
146+
return acc;
147+
}, {})
148+
};
149+
});
143150
})
144151
.then(precompilation => {
145152
// Resolve the correct concurrency value.

lib/ava-files.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ const defaultExcludePatterns = () => [
7979
'!**/helpers/**'
8080
];
8181

82-
const defaultIncludePatterns = () => [
83-
'test.js',
84-
'test-*.js',
85-
'test',
86-
'**/__tests__',
87-
'**/*.test.js'
82+
const defaultIncludePatterns = exts => [
83+
'test.' + exts,
84+
'test-*.' + exts,
85+
'test', // Directory
86+
'**/__tests__', // Directory
87+
'**/*.test.' + exts
8888
];
8989

90-
const defaultHelperPatterns = () => [
91-
'**/__tests__/helpers/**/*.js',
92-
'**/__tests__/**/_*.js',
93-
'**/test/helpers/**/*.js',
94-
'**/test/**/_*.js'
90+
const defaultHelperPatterns = exts => [
91+
'**/__tests__/helpers/**/*.' + exts,
92+
'**/__tests__/**/_*.' + exts,
93+
'**/test/helpers/**/*.' + exts,
94+
'**/test/**/_*.' + exts
9595
];
9696

9797
const getDefaultIgnorePatterns = () => defaultIgnore.map(dir => `${dir}/**/*`);
@@ -118,6 +118,7 @@ class AvaFiles {
118118
files = defaultIncludePatterns();
119119
}
120120

121+
this.extensions = options.extensions ? `{${options.extensions.join(',')}}` : '{js}';
121122
this.excludePatterns = defaultExcludePatterns();
122123
this.files = files;
123124
this.sources = options.sources || [];
@@ -141,7 +142,7 @@ class AvaFiles {
141142
}
142143

143144
findTestHelpers() {
144-
return handlePaths(defaultHelperPatterns(), ['!**/node_modules/**'], Object.assign({
145+
return handlePaths(defaultHelperPatterns(this.extensions), ['!**/node_modules/**'], Object.assign({
145146
cwd: this.cwd,
146147
includeUnderscoredFiles: true,
147148
expandDirectories: false,
@@ -151,7 +152,7 @@ class AvaFiles {
151152

152153
isSource(filePath) {
153154
let mixedPatterns = [];
154-
const defaultIgnorePatterns = getDefaultIgnorePatterns();
155+
const defaultIgnorePatterns = getDefaultIgnorePatterns(this.options.extensions);
155156
const overrideDefaultIgnorePatterns = [];
156157

157158
let hasPositivePattern = false;

lib/babel-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function validate(conf) {
2222
return {testOptions: {}};
2323
}
2424

25-
if (!conf || typeof conf !== 'object' || !conf.testOptions || typeof conf.testOptions !== 'object' || Array.isArray(conf.testOptions) || Object.keys(conf).length > 1) {
25+
if (!conf || typeof conf !== 'object' || !conf.testOptions || typeof conf.testOptions !== 'object' || Array.isArray(conf.testOptions)) {
2626
throw new Error(`${colors.error(figures.cross)} Unexpected Babel configuration for AVA. See ${chalk.underline('https://github.com/avajs/ava/blob/master/docs/recipes/babel.md')} for allowed values.`);
2727
}
2828

lib/caching-precompiler.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ class CachingPrecompiler {
3030
this.transform = this._createTransform();
3131
}
3232

33-
precompileFile(filePath) {
33+
precompileFile(filePath, excludeExtensions) {
34+
if (excludeExtensions.includes(path.extname(filePath))) {
35+
return filePath;
36+
}
37+
3438
if (!this.fileHashes[filePath]) {
3539
const source = stripBomBuf(fs.readFileSync(filePath));
3640
this.transform(source, filePath);

lib/cli.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ exports.run = () => {
152152
compileEnhancements: conf.compileEnhancements !== false,
153153
explicitTitles: conf.watch,
154154
match: arrify(conf.match),
155+
extensions: conf.extensions,
155156
babelConfig: babelConfigHelper.validate(conf.babel),
156157
resolveTestsFrom: cli.input.length === 0 ? projectDir : process.cwd(),
157158
projectDir,

0 commit comments

Comments
 (0)