-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Added support for babel config under package.json/ava key (Fixes #448) #573
Changes from all commits
33b842d
c664aa1
1742a3f
bc49364
0044f9a
9a62bed
d833f52
eb6d024
8517681
c052757
03850ad
f167ea3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,7 @@ | |
"update-notifier": "^0.6.0" | ||
}, | ||
"devDependencies": { | ||
"babel-cli": "^6.6.4", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this dependency? |
||
"cli-table2": "^0.1.9", | ||
"coveralls": "^2.11.4", | ||
"delay": "^1.3.0", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,7 +154,8 @@ All of the CLI options can be configured in the `ava` section of your `package.j | |
"tap": true, | ||
"require": [ | ||
"babel-register" | ||
] | ||
], | ||
"babel": "inherit" | ||
} | ||
} | ||
``` | ||
|
@@ -423,6 +424,61 @@ AVA comes with builtin support for ES2015 through [Babel 6](https://babeljs.io). | |
|
||
AVA includes typings for TypeScript. You have to setup transpilation yourself. When you set `module` to `commonjs` in your `tsconfig.json` file, TypeScript will automatically find the type definitions for AVA. You should set `target` to `es2015` to use Promises and async functions. | ||
|
||
### Babel Configuration for Test Scripts | ||
|
||
If you want to customize the babel transpiler for test files, you can do so by adding a `"babel"` key to the `ava` section in your `package.json` file. | ||
|
||
```json | ||
{ | ||
"ava": { | ||
"babel": { | ||
"presets": [ | ||
"es2015", | ||
"stage-0", | ||
"react" | ||
] | ||
} | ||
}, | ||
} | ||
``` | ||
|
||
In addition to specifying a custom Babel config, you can also use the special `"inherit"` keyword. When you do this, AVA will allow tests to be transpiled using the configuration defined in your `.babelrc` file or in package.json/babel. This way, your test files will be transpiled using the same options as your source files, but you won't have to define the options twice. | ||
|
||
```json | ||
{ | ||
"babel": { | ||
"presets": [ | ||
"es2015", | ||
"stage-0", | ||
"react" | ||
] | ||
}, | ||
"ava": { | ||
"babel": "inherit", | ||
}, | ||
} | ||
``` | ||
|
||
Note: When configuring Babel for tests manually, the espower and transform-runtime plugins will be | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't include transform-runtime, users will have to |
||
added for you. | ||
|
||
## Default Babel Configuration for Test Scripts | ||
|
||
If you don't explicitly configure Babel for your tests using the `"babel"` key in package.json, your tests will be transpiled using AVA's default Babel configuration, which is as follows: | ||
|
||
```json | ||
{ | ||
"presets": [ | ||
"es2015", | ||
"stage-0", | ||
], | ||
"plugins": [ | ||
"espower", | ||
"transform-runtime" | ||
] | ||
} | ||
``` | ||
|
||
#### 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.* While there are valid reasons for taking this approach, it may not be what you expect! | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ var fs = require('fs'); | |
var path = require('path'); | ||
var test = require('tap').test; | ||
var uniqueTempDir = require('unique-temp-dir'); | ||
var sinon = require('sinon'); | ||
var babel = require('babel-core'); | ||
var transformRuntime = require('babel-plugin-transform-runtime'); | ||
|
||
var CachingPrecompiler = require('../lib/caching-precompiler'); | ||
|
||
|
@@ -18,24 +21,26 @@ function endsWithMap(filename) { | |
return /\.js$/.test(filename); | ||
} | ||
|
||
sinon.spy(babel, 'transform'); | ||
|
||
test('creation with new', function (t) { | ||
var tempDir = uniqueTempDir(); | ||
var precompiler = new CachingPrecompiler(tempDir); | ||
var precompiler = new CachingPrecompiler(tempDir, null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should pass |
||
t.is(precompiler.cacheDir, tempDir); | ||
t.end(); | ||
}); | ||
|
||
test('must be called with new', function (t) { | ||
t.throws(function () { | ||
var cachingPrecompiler = CachingPrecompiler; | ||
cachingPrecompiler(uniqueTempDir()); | ||
cachingPrecompiler(uniqueTempDir(), null); | ||
}, {message: 'Class constructor CachingPrecompiler cannot be invoked without \'new\''}); | ||
t.end(); | ||
}); | ||
|
||
test('adds files and source maps to the cache directory as needed', function (t) { | ||
var tempDir = uniqueTempDir(); | ||
var precompiler = new CachingPrecompiler(tempDir); | ||
var precompiler = new CachingPrecompiler(tempDir, null); | ||
|
||
t.false(fs.existsSync(tempDir), 'cache directory is not created before it is needed'); | ||
|
||
|
@@ -48,3 +53,67 @@ test('adds files and source maps to the cache directory as needed', function (t) | |
t.is(files.filter(endsWithMap).length, 1, 'one .map file is saved to the cache'); | ||
t.end(); | ||
}); | ||
|
||
test('uses default babel options when babelConfig === "default"', function (t) { | ||
var tempDir = uniqueTempDir(); | ||
var precompiler = new CachingPrecompiler(tempDir, 'default'); | ||
babel.transform.reset(); | ||
|
||
precompiler.precompileFile(fixture('es2015.js')); | ||
|
||
t.true(babel.transform.calledOnce); | ||
var options = babel.transform.firstCall.args[1]; | ||
|
||
t.true('filename' in options); | ||
t.true(options.sourceMaps); | ||
t.false(options.ast); | ||
t.true('inputSourceMap' in options); | ||
t.false(options.babelrc); | ||
t.true(Array.isArray(options.presets)); | ||
t.true(Array.isArray(options.plugins)); | ||
t.end(); | ||
}); | ||
|
||
test('allows babel config from package.json/babel when babelConfig === "inherit"', function (t) { | ||
var tempDir = uniqueTempDir(); | ||
var precompiler = new CachingPrecompiler(tempDir, 'inherit'); | ||
babel.transform.reset(); | ||
|
||
precompiler.precompileFile(fixture('es2015.js')); | ||
|
||
t.true(babel.transform.calledOnce); | ||
var options = babel.transform.firstCall.args[1]; | ||
|
||
t.true('filename' in options); | ||
t.true(options.sourceMaps); | ||
t.false(options.ast); | ||
t.true('inputSourceMap' in options); | ||
t.true(options.babelrc); | ||
t.end(); | ||
}); | ||
|
||
test('uses babelConfig for babel options when babelConfig is an object', function (t) { | ||
var tempDir = uniqueTempDir(); | ||
var customPlugin = sinon.stub().returns({visitor: {}}); | ||
var powerAssert = sinon.stub().returns({visitor: {}}); | ||
var precompiler = new CachingPrecompiler(tempDir, { | ||
presets: ['stage-2', 'es2015'], | ||
plugins: [customPlugin] | ||
}); | ||
sinon.stub(precompiler, '_createEspowerPlugin').returns(powerAssert); | ||
babel.transform.reset(); | ||
|
||
precompiler.precompileFile(fixture('es2015.js')); | ||
|
||
t.true(babel.transform.calledOnce); | ||
var options = babel.transform.firstCall.args[1]; | ||
|
||
t.true('filename' in options); | ||
t.true(options.sourceMaps); | ||
t.false(options.ast); | ||
t.true('inputSourceMap' in options); | ||
t.false(options.babelrc); | ||
t.same(options.presets, ['stage-2', 'es2015']); | ||
t.same(options.plugins, [customPlugin, powerAssert, transformRuntime]); | ||
t.end(); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than checking for a falsy value here you could define the default in
cli.js
. Change this line to:This will also save you a test 😎