Skip to content

Commit c3fe5ec

Browse files
author
vdemedes
committed
precompile helpers and fixtures
1 parent 8171dbc commit c3fe5ec

File tree

6 files changed

+64
-16
lines changed

6 files changed

+64
-16
lines changed

api.js

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

6868
Api.prototype._runFile = function (file, runStatus, execArgv) {
6969
var hash = this.precompiler.precompileFile(file);
70-
var precompiled = {};
70+
var precompiled = objectAssign({}, this._precompiledHelpers);
7171
var resolvedfpath = fs.realpathSync(file);
7272
precompiled[resolvedfpath] = hash;
7373

@@ -139,7 +139,22 @@ Api.prototype._setupPrecompiler = function (files) {
139139
});
140140
};
141141

142+
Api.prototype._precompileHelpers = function () {
143+
var self = this;
144+
145+
this._precompiledHelpers = {};
146+
147+
return new AvaFiles({cwd: this.options.resolveTestsFrom})
148+
.findTestHelpers()
149+
.map(function (file) { // eslint-disable-line array-callback-return
150+
var hash = self.precompiler.precompileFile(file);
151+
self._precompiledHelpers[file] = hash;
152+
});
153+
};
154+
142155
Api.prototype._run = function (files, options) {
156+
var self = this;
157+
143158
options = options || {};
144159

145160
var runStatus = new RunStatus({
@@ -159,20 +174,23 @@ Api.prototype._run = function (files, options) {
159174

160175
this._setupPrecompiler(files);
161176

162-
if (this.options.timeout) {
163-
this._setupTimeout(runStatus);
164-
}
177+
return this._precompileHelpers()
178+
.then(function () {
179+
if (self.options.timeout) {
180+
self._setupTimeout(runStatus);
181+
}
165182

166-
var overwatch;
167-
if (this.options.concurrency > 0) {
168-
var concurrency = this.options.serial ? 1 : this.options.concurrency;
169-
overwatch = this._runWithPool(files, runStatus, concurrency);
170-
} else {
171-
// _runWithoutPool exists to preserve legacy behavior, specifically around `.only`
172-
overwatch = this._runWithoutPool(files, runStatus);
173-
}
183+
var overwatch;
184+
if (self.options.concurrency > 0) {
185+
var concurrency = self.options.serial ? 1 : self.options.concurrency;
186+
overwatch = self._runWithPool(files, runStatus, concurrency);
187+
} else {
188+
// _runWithoutPool exists to preserve legacy behavior, specifically around `.only`
189+
overwatch = self._runWithoutPool(files, runStatus);
190+
}
174191

175-
return overwatch;
192+
return overwatch;
193+
});
176194
};
177195

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

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ See AVA's [TypeScript recipe](docs/recipes/typescript.md) for a more detailed ex
735735

736736
### Transpiling imported modules
737737

738-
AVA currently only transpiles the tests you ask it to run. *It will not transpile modules you `import` from outside of the test.* This may be unexpected but there are workarounds.
738+
AVA currently only transpiles the tests you ask it to run, as well as test helpers (files starting with `_` or in `helpers` directory). *It will not transpile modules you `import` from outside of the test.* This may be unexpected but there are workarounds.
739739

740740
If you use Babel you can use its [require hook](https://babeljs.io/docs/usage/require/) to transpile imported modules on-the-fly. To add it, [configure it in your `package.json`](#configuration).
741741

test/api.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ function generateTests(prefix, apiCreator) {
7676
});
7777
});
7878

79+
test(prefix + 'precompile helpers', function (t) {
80+
t.plan(1);
81+
82+
var api = apiCreator();
83+
84+
return api.run([path.join(__dirname, 'fixture/precompile-helpers/test/test.js')])
85+
.then(function (result) {
86+
t.is(result.passCount, 1);
87+
});
88+
});
89+
7990
test(prefix + 'generators support', function (t) {
8091
t.plan(1);
8192

@@ -719,7 +730,10 @@ function generateTests(prefix, apiCreator) {
719730
test(prefix + 'caching is enabled by default', function (t) {
720731
t.plan(3);
721732
rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules'));
722-
var api = apiCreator();
733+
734+
var api = apiCreator({
735+
resolveTestsFrom: path.join(__dirname, 'fixture/caching')
736+
});
723737

724738
return api.run([path.join(__dirname, 'fixture/caching/test.js')])
725739
.then(function () {
@@ -742,7 +756,11 @@ function generateTests(prefix, apiCreator) {
742756
test(prefix + 'caching can be disabled', function (t) {
743757
t.plan(1);
744758
rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules'));
745-
var api = apiCreator({cacheEnabled: false});
759+
760+
var api = apiCreator({
761+
resolveTestsFrom: path.join(__dirname, 'fixture/caching'),
762+
cacheEnabled: false
763+
});
746764

747765
return api.run([path.join(__dirname, 'fixture/caching/test.js')])
748766
.then(function () {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default async function () {}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default async function () {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import test from '../../../../';
2+
import a from './helpers/a';
3+
import b from './_b';
4+
5+
test(async t => {
6+
await a();
7+
await b();
8+
9+
t.pass();
10+
});

0 commit comments

Comments
 (0)