Permalink
Browse files

Precompile helpers (#1078)

  • Loading branch information...
1 parent 09d23f5 commit 410cb8d3b1bb70b48dd305b0bc69843579f9205d @vadimdemedes vadimdemedes committed with sindresorhus Jan 7, 2017
View
@@ -67,7 +67,7 @@ module.exports = Api;
Api.prototype._runFile = function (file, runStatus, execArgv) {
var hash = this.precompiler.precompileFile(file);
- var precompiled = {};
+ var precompiled = objectAssign({}, this._precompiledHelpers);
var resolvedfpath = fs.realpathSync(file);
precompiled[resolvedfpath] = hash;
@@ -139,7 +139,22 @@ Api.prototype._setupPrecompiler = function (files) {
});
};
+Api.prototype._precompileHelpers = function () {
+ var self = this;
+
+ this._precompiledHelpers = {};
+
+ return new AvaFiles({cwd: this.options.resolveTestsFrom})
+ .findTestHelpers()
+ .map(function (file) { // eslint-disable-line array-callback-return
+ var hash = self.precompiler.precompileFile(file);
+ self._precompiledHelpers[file] = hash;
+ });
+};
+
Api.prototype._run = function (files, options) {
+ var self = this;
+
options = options || {};
var runStatus = new RunStatus({
@@ -160,20 +175,23 @@ Api.prototype._run = function (files, options) {
this._setupPrecompiler(files);
- if (this.options.timeout) {
- this._setupTimeout(runStatus);
- }
+ return this._precompileHelpers()
+ .then(function () {
+ if (self.options.timeout) {
+ self._setupTimeout(runStatus);
+ }
- var overwatch;
- if (this.options.concurrency > 0) {
- var concurrency = this.options.serial ? 1 : this.options.concurrency;
- overwatch = this._runWithPool(files, runStatus, concurrency);
- } else {
- // _runWithoutPool exists to preserve legacy behavior, specifically around `.only`
- overwatch = this._runWithoutPool(files, runStatus);
- }
+ var overwatch;
+ if (self.options.concurrency > 0) {
+ var concurrency = self.options.serial ? 1 : self.options.concurrency;
+ overwatch = self._runWithPool(files, runStatus, concurrency);
+ } else {
+ // _runWithoutPool exists to preserve legacy behavior, specifically around `.only`
+ overwatch = self._runWithoutPool(files, runStatus);
+ }
- return overwatch;
+ return overwatch;
+ });
};
Api.prototype._computeForkExecArgs = function (files) {
View
@@ -735,7 +735,7 @@ See AVA's [TypeScript recipe](docs/recipes/typescript.md) for a more detailed ex
### Transpiling imported modules
-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.
+AVA currently only transpiles the tests you ask it to run, as well as test helpers (files starting with `_` or in `helpers` directory) inside the test directory. *It will not transpile modules you `import` from outside of the test.* This may be unexpected but there are workarounds.
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).
View
@@ -76,6 +76,17 @@ function generateTests(prefix, apiCreator) {
});
});
+ test(prefix + 'precompile helpers', function (t) {
+ t.plan(1);
+
+ var api = apiCreator();
+
+ return api.run([path.join(__dirname, 'fixture/precompile-helpers/test/test.js')])
+ .then(function (result) {
+ t.is(result.passCount, 1);
+ });
+ });
+
test(prefix + 'generators support', function (t) {
t.plan(1);
@@ -719,7 +730,10 @@ function generateTests(prefix, apiCreator) {
test(prefix + 'caching is enabled by default', function (t) {
t.plan(3);
rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules'));
- var api = apiCreator();
+
+ var api = apiCreator({
+ resolveTestsFrom: path.join(__dirname, 'fixture/caching')
+ });
return api.run([path.join(__dirname, 'fixture/caching/test.js')])
.then(function () {
@@ -742,7 +756,11 @@ function generateTests(prefix, apiCreator) {
test(prefix + 'caching can be disabled', function (t) {
t.plan(1);
rimraf.sync(path.join(__dirname, 'fixture/caching/node_modules'));
- var api = apiCreator({cacheEnabled: false});
+
+ var api = apiCreator({
+ resolveTestsFrom: path.join(__dirname, 'fixture/caching'),
+ cacheEnabled: false
+ });
return api.run([path.join(__dirname, 'fixture/caching/test.js')])
.then(function () {
@@ -0,0 +1 @@
+export default async function () {}
@@ -0,0 +1 @@
+export default async function () {}
@@ -0,0 +1,10 @@
+import test from '../../../../';
+import a from './helpers/a';
+import b from './_b';
+
+test(async t => {
+ await a();
+ await b();
+
+ t.pass();
+});

0 comments on commit 410cb8d

Please sign in to comment.