Skip to content

Commit

Permalink
Precompile helpers (#1078)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes authored and sindresorhus committed Jan 7, 2017
1 parent 09d23f5 commit 410cb8d
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
44 changes: 31 additions & 13 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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({
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
22 changes: 20 additions & 2 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down
1 change: 1 addition & 0 deletions test/fixture/precompile-helpers/test/_b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default async function () {}
1 change: 1 addition & 0 deletions test/fixture/precompile-helpers/test/helpers/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default async function () {}
10 changes: 10 additions & 0 deletions test/fixture/precompile-helpers/test/test.js
Original file line number Diff line number Diff line change
@@ -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.