Skip to content

Precompile helpers #1078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use .map if you're not actually going to map anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using it as a way to async iterate the items. Is there another bluebird method that does that?

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 @@ -159,20 +174,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();
});