Skip to content

Commit 9303025

Browse files
author
vdemedes
committed
precompile helpers and fixtures
1 parent 3ea2ba1 commit 9303025

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

api.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports = Api;
6666

6767
Api.prototype._runFile = function (file, runStatus, execArgv) {
6868
var hash = this.precompiler.precompileFile(file);
69-
var precompiled = {};
69+
var precompiled = objectAssign({}, this._precompiledHelpers);
7070
precompiled[file] = hash;
7171

7272
var options = objectAssign({}, this.options, {
@@ -134,7 +134,22 @@ Api.prototype._setupPrecompiler = function (files) {
134134
});
135135
};
136136

137+
Api.prototype._precompileHelpers = function () {
138+
var self = this;
139+
140+
this._precompiledHelpers = {};
141+
142+
return new AvaFiles({cwd: this.options.resolveTestsFrom})
143+
.findTestHelpers()
144+
.map(function (file) { // eslint-disable-line array-callback-return
145+
var hash = self.precompiler.precompileFile(file);
146+
self._precompiledHelpers[file] = hash;
147+
});
148+
};
149+
137150
Api.prototype._run = function (files, options) {
151+
var self = this;
152+
138153
options = options || {};
139154

140155
var runStatus = new RunStatus({
@@ -154,20 +169,23 @@ Api.prototype._run = function (files, options) {
154169

155170
this._setupPrecompiler(files);
156171

157-
if (this.options.timeout) {
158-
this._setupTimeout(runStatus);
159-
}
172+
return this._precompileHelpers()
173+
.then(function () {
174+
if (self.options.timeout) {
175+
self._setupTimeout(runStatus);
176+
}
160177

161-
var overwatch;
162-
if (this.options.concurrency > 0) {
163-
var concurrency = this.options.serial ? 1 : this.options.concurrency;
164-
overwatch = this._runWithPool(files, runStatus, concurrency);
165-
} else {
166-
// _runWithoutPool exists to preserve legacy behavior, specifically around `.only`
167-
overwatch = this._runWithoutPool(files, runStatus);
168-
}
178+
var overwatch;
179+
if (self.options.concurrency > 0) {
180+
var concurrency = self.options.serial ? 1 : self.options.concurrency;
181+
overwatch = self._runWithPool(files, runStatus, concurrency);
182+
} else {
183+
// _runWithoutPool exists to preserve legacy behavior, specifically around `.only`
184+
overwatch = self._runWithoutPool(files, runStatus);
185+
}
169186

170-
return overwatch;
187+
return overwatch;
188+
});
171189
};
172190

173191
Api.prototype._computeForkExecArgs = function (files) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
"array-uniq": "^1.0.2",
9292
"arrify": "^1.0.0",
9393
"auto-bind": "^0.1.0",
94-
"ava-files": "^0.2.0",
94+
"ava-files": "avajs/ava-files#find-test-helpers",
9595
"ava-init": "^0.1.0",
9696
"babel-code-frame": "^6.16.0",
9797
"babel-core": "^6.17.0",

test/api.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,9 +705,16 @@ function generateTests(prefix, apiCreator) {
705705
test(prefix + 'caching is enabled by default', function (t) {
706706
t.plan(3);
707707
rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules'));
708+
709+
const prevCwd = process.cwd();
710+
process.chdir(path.join(__dirname, 'fixture/caching'));
711+
708712
var api = apiCreator();
709713

710714
return api.run([path.join(__dirname, 'fixture/caching/test.js')])
715+
.finally(function () {
716+
process.chdir(prevCwd);
717+
})
711718
.then(function () {
712719
var files = fs.readdirSync(path.join(__dirname, 'fixture/caching/node_modules/.cache/ava'));
713720
t.is(files.length, 2);
@@ -728,9 +735,16 @@ function generateTests(prefix, apiCreator) {
728735
test(prefix + 'caching can be disabled', function (t) {
729736
t.plan(1);
730737
rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules'));
738+
739+
const prevCwd = process.cwd();
740+
process.chdir(path.join(__dirname, 'fixture/caching'));
741+
731742
var api = apiCreator({cacheEnabled: false});
732743

733744
return api.run([path.join(__dirname, 'fixture/caching/test.js')])
745+
.finally(function () {
746+
process.chdir(prevCwd);
747+
})
734748
.then(function () {
735749
t.false(fs.existsSync(path.join(__dirname, 'fixture/caching/node_modules/.cache/ava')));
736750
t.end();

0 commit comments

Comments
 (0)