Skip to content

Commit

Permalink
Add option to disable power-assert - fixes #1017 (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels authored and sindresorhus committed Aug 30, 2016
1 parent 88f50d2 commit 24a38ac
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion api.js
Expand Up @@ -119,7 +119,7 @@ Api.prototype._run = function (files, _options) {
})) || uniqueTempDir();

self.options.cacheDir = cacheDir;
self.precompiler = new CachingPrecompiler(cacheDir, self.options.babelConfig);
self.precompiler = new CachingPrecompiler(cacheDir, self.options.babelConfig, self.options.powerAssert);
self.fileCount = files.length;

var overwatch;
Expand Down
2 changes: 2 additions & 0 deletions cli.js
Expand Up @@ -64,6 +64,7 @@ var cli = meow([
' --tap, -t Generate TAP output',
' --verbose, -v Enable verbose output',
' --no-cache Disable the transpiler cache',
' --no-power-assert Disable Power Assert',
' --match, -m Only run tests with matching title (Can be repeated)',
' --watch, -w Re-run tests when tests and source files change',
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
Expand Down Expand Up @@ -130,6 +131,7 @@ var api = new Api({
serial: cli.flags.serial,
require: arrify(cli.flags.require),
cacheEnabled: cli.flags.cache !== false,
powerAssert: cli.flags.powerAssert !== false,
explicitTitles: cli.flags.watch,
match: arrify(cli.flags.match),
babelConfig: conf.babel,
Expand Down
7 changes: 4 additions & 3 deletions lib/babel-config.js
Expand Up @@ -77,14 +77,13 @@ var espowerPlugin = lazy(function () {

var defaultPlugins = lazy(function () {
return [
espowerPlugin(),
require('babel-plugin-ava-throws-helper'),
rewritePlugin(),
require('babel-plugin-transform-runtime')
];
});

function build(babelConfig, filePath, code) {
function build(babelConfig, powerAssert, filePath, code) {
babelConfig = validate(babelConfig);

var options;
Expand Down Expand Up @@ -115,7 +114,9 @@ function build(babelConfig, filePath, code) {
ast: false
});

options.plugins = (options.plugins || []).concat(defaultPlugins());
options.plugins = (options.plugins || [])
.concat(powerAssert ? espowerPlugin() : [])
.concat(defaultPlugins());

return options;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/caching-precompiler.js
Expand Up @@ -9,12 +9,13 @@ var packageHash = require('package-hash');
var autoBind = require('auto-bind');
var babelConfigHelper = require('./babel-config');

function CachingPrecompiler(cacheDirPath, babelConfig) {
function CachingPrecompiler(cacheDirPath, babelConfig, powerAssert) {
if (!(this instanceof CachingPrecompiler)) {
throw new TypeError('Class constructor CachingPrecompiler cannot be invoked without \'new\'');
}

this.babelConfig = babelConfigHelper.validate(babelConfig);
this.powerAssert = powerAssert;
this.cacheDirPath = cacheDirPath;
this.fileHashes = {};

Expand Down Expand Up @@ -49,7 +50,7 @@ CachingPrecompiler.prototype._init = function () {
CachingPrecompiler.prototype._transform = function (code, filePath, hash) {
code = code.toString();

var options = babelConfigHelper.build(this.babelConfig, filePath, code);
var options = babelConfigHelper.build(this.babelConfig, this.powerAssert, filePath, code);
var result = this.babel.transform(code, options);

// save source map
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Expand Up @@ -144,6 +144,7 @@ $ ava --help
--tap, -t Generate TAP output
--verbose, -v Enable verbose output
--no-cache Disable the transpiler cache
--no-power-assert Disable Power Assert
--match, -m Only run tests with matching title (Can be repeated)
--watch, -w Re-run tests when tests and source files change
--source, -S Pattern to match source files so tests can be re-run (Can be repeated)
Expand Down Expand Up @@ -223,6 +224,7 @@ All of the CLI options can be configured in the `ava` section of your `package.j
"concurrency": 5,
"failFast": true,
"tap": true,
"powerAssert": false,
"require": [
"babel-register"
],
Expand Down
3 changes: 3 additions & 0 deletions test/api.js
Expand Up @@ -16,6 +16,8 @@ test('must be called with new', function (t) {
});

generateTests('Without Pool: ', function (options) {
options = options || {};
options.powerAssert = true;
return new Api(options);
});

Expand Down Expand Up @@ -60,6 +62,7 @@ test('Without Pool: test files can be forced to run in exclusive mode', function
generateTests('With Pool: ', function (options) {
options = options || {};
options.concurrency = 2;
options.powerAssert = true;
return new Api(options);
});

Expand Down
30 changes: 28 additions & 2 deletions test/babel-config.js
Expand Up @@ -50,7 +50,8 @@ test('uses babelConfig for babel options when babelConfig is an object', functio
var fixturePath = fixture('es2015.js');
var fixtureSource = fs.readFileSync(fixturePath, 'utf8');

var options = babelConfigHelper.build(babelConfig, fixturePath, fixtureSource);
var powerAssert = true;
var options = babelConfigHelper.build(babelConfig, powerAssert, fixturePath, fixtureSource);

t.true('filename' in options);
t.true(options.sourceMaps);
Expand Down Expand Up @@ -79,7 +80,8 @@ test('should reuse existing source maps', function (t) {
var fixturePath = fixture('es2015-source-maps.js');
var fixtureSource = fs.readFileSync(fixturePath, 'utf8');

var options = babelConfigHelper.build(babelConfig, fixturePath, fixtureSource);
var powerAssert = true;
var options = babelConfigHelper.build(babelConfig, powerAssert, fixturePath, fixtureSource);

t.true('filename' in options);
t.true(options.sourceMaps);
Expand All @@ -89,3 +91,27 @@ test('should reuse existing source maps', function (t) {
t.strictDeepEqual(options.plugins, [customPlugin, setup.powerAssert, throwsHelper, setup.rewrite, transformRuntime]);
t.end();
});

test('should disable power-assert when powerAssert is false', function (t) {
var setup = setUp();
var customPlugin = setup.customPlugin;

var babelConfigHelper = proxyquire('../lib/babel-config', {
'babel-plugin-espower/create': setup.createEspowerPlugin,
'babel-plugin-detective/wrap-listener': setup.babelDetectiveWrap
});

var babelConfig = {
presets: ['stage-2', 'es2015'],
plugins: [customPlugin]
};

var fixturePath = fixture('es2015.js');
var fixtureSource = fs.readFileSync(fixturePath, 'utf8');

var powerAssert = false;
var options = babelConfigHelper.build(babelConfig, powerAssert, fixturePath, fixtureSource);

t.strictDeepEqual(options.plugins, [customPlugin, throwsHelper, setup.rewrite, transformRuntime]);
t.end();
});

0 comments on commit 24a38ac

Please sign in to comment.