Skip to content

Commit 24a38ac

Browse files
jfmengelssindresorhus
authored andcommitted
Add option to disable power-assert - fixes #1017 (#1024)
1 parent 88f50d2 commit 24a38ac

File tree

7 files changed

+43
-8
lines changed

7 files changed

+43
-8
lines changed

api.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Api.prototype._run = function (files, _options) {
119119
})) || uniqueTempDir();
120120

121121
self.options.cacheDir = cacheDir;
122-
self.precompiler = new CachingPrecompiler(cacheDir, self.options.babelConfig);
122+
self.precompiler = new CachingPrecompiler(cacheDir, self.options.babelConfig, self.options.powerAssert);
123123
self.fileCount = files.length;
124124

125125
var overwatch;

cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ var cli = meow([
6464
' --tap, -t Generate TAP output',
6565
' --verbose, -v Enable verbose output',
6666
' --no-cache Disable the transpiler cache',
67+
' --no-power-assert Disable Power Assert',
6768
' --match, -m Only run tests with matching title (Can be repeated)',
6869
' --watch, -w Re-run tests when tests and source files change',
6970
' --source, -S Pattern to match source files so tests can be re-run (Can be repeated)',
@@ -130,6 +131,7 @@ var api = new Api({
130131
serial: cli.flags.serial,
131132
require: arrify(cli.flags.require),
132133
cacheEnabled: cli.flags.cache !== false,
134+
powerAssert: cli.flags.powerAssert !== false,
133135
explicitTitles: cli.flags.watch,
134136
match: arrify(cli.flags.match),
135137
babelConfig: conf.babel,

lib/babel-config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,13 @@ var espowerPlugin = lazy(function () {
7777

7878
var defaultPlugins = lazy(function () {
7979
return [
80-
espowerPlugin(),
8180
require('babel-plugin-ava-throws-helper'),
8281
rewritePlugin(),
8382
require('babel-plugin-transform-runtime')
8483
];
8584
});
8685

87-
function build(babelConfig, filePath, code) {
86+
function build(babelConfig, powerAssert, filePath, code) {
8887
babelConfig = validate(babelConfig);
8988

9089
var options;
@@ -115,7 +114,9 @@ function build(babelConfig, filePath, code) {
115114
ast: false
116115
});
117116

118-
options.plugins = (options.plugins || []).concat(defaultPlugins());
117+
options.plugins = (options.plugins || [])
118+
.concat(powerAssert ? espowerPlugin() : [])
119+
.concat(defaultPlugins());
119120

120121
return options;
121122
}

lib/caching-precompiler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ var packageHash = require('package-hash');
99
var autoBind = require('auto-bind');
1010
var babelConfigHelper = require('./babel-config');
1111

12-
function CachingPrecompiler(cacheDirPath, babelConfig) {
12+
function CachingPrecompiler(cacheDirPath, babelConfig, powerAssert) {
1313
if (!(this instanceof CachingPrecompiler)) {
1414
throw new TypeError('Class constructor CachingPrecompiler cannot be invoked without \'new\'');
1515
}
1616

1717
this.babelConfig = babelConfigHelper.validate(babelConfig);
18+
this.powerAssert = powerAssert;
1819
this.cacheDirPath = cacheDirPath;
1920
this.fileHashes = {};
2021

@@ -49,7 +50,7 @@ CachingPrecompiler.prototype._init = function () {
4950
CachingPrecompiler.prototype._transform = function (code, filePath, hash) {
5051
code = code.toString();
5152

52-
var options = babelConfigHelper.build(this.babelConfig, filePath, code);
53+
var options = babelConfigHelper.build(this.babelConfig, this.powerAssert, filePath, code);
5354
var result = this.babel.transform(code, options);
5455

5556
// save source map

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ $ ava --help
144144
--tap, -t Generate TAP output
145145
--verbose, -v Enable verbose output
146146
--no-cache Disable the transpiler cache
147+
--no-power-assert Disable Power Assert
147148
--match, -m Only run tests with matching title (Can be repeated)
148149
--watch, -w Re-run tests when tests and source files change
149150
--source, -S Pattern to match source files so tests can be re-run (Can be repeated)
@@ -223,6 +224,7 @@ All of the CLI options can be configured in the `ava` section of your `package.j
223224
"concurrency": 5,
224225
"failFast": true,
225226
"tap": true,
227+
"powerAssert": false,
226228
"require": [
227229
"babel-register"
228230
],

test/api.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ test('must be called with new', function (t) {
1616
});
1717

1818
generateTests('Without Pool: ', function (options) {
19+
options = options || {};
20+
options.powerAssert = true;
1921
return new Api(options);
2022
});
2123

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

test/babel-config.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ test('uses babelConfig for babel options when babelConfig is an object', functio
5050
var fixturePath = fixture('es2015.js');
5151
var fixtureSource = fs.readFileSync(fixturePath, 'utf8');
5252

53-
var options = babelConfigHelper.build(babelConfig, fixturePath, fixtureSource);
53+
var powerAssert = true;
54+
var options = babelConfigHelper.build(babelConfig, powerAssert, fixturePath, fixtureSource);
5455

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

82-
var options = babelConfigHelper.build(babelConfig, fixturePath, fixtureSource);
83+
var powerAssert = true;
84+
var options = babelConfigHelper.build(babelConfig, powerAssert, fixturePath, fixtureSource);
8385

8486
t.true('filename' in options);
8587
t.true(options.sourceMaps);
@@ -89,3 +91,27 @@ test('should reuse existing source maps', function (t) {
8991
t.strictDeepEqual(options.plugins, [customPlugin, setup.powerAssert, throwsHelper, setup.rewrite, transformRuntime]);
9092
t.end();
9193
});
94+
95+
test('should disable power-assert when powerAssert is false', function (t) {
96+
var setup = setUp();
97+
var customPlugin = setup.customPlugin;
98+
99+
var babelConfigHelper = proxyquire('../lib/babel-config', {
100+
'babel-plugin-espower/create': setup.createEspowerPlugin,
101+
'babel-plugin-detective/wrap-listener': setup.babelDetectiveWrap
102+
});
103+
104+
var babelConfig = {
105+
presets: ['stage-2', 'es2015'],
106+
plugins: [customPlugin]
107+
};
108+
109+
var fixturePath = fixture('es2015.js');
110+
var fixtureSource = fs.readFileSync(fixturePath, 'utf8');
111+
112+
var powerAssert = false;
113+
var options = babelConfigHelper.build(babelConfig, powerAssert, fixturePath, fixtureSource);
114+
115+
t.strictDeepEqual(options.plugins, [customPlugin, throwsHelper, setup.rewrite, transformRuntime]);
116+
t.end();
117+
});

0 commit comments

Comments
 (0)