Skip to content
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
16 changes: 8 additions & 8 deletions docs/recipes/babelrc.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ There are multiple options for configuring how AVA transpiles your tests using B

## AVA's default transpiler behavior

By default, AVA transpiles your tests (and only your tests) using the [`es2015-node4`](https://github.com/jbach/babel-preset-es2015-node4) on Node.js 4 or [`node6`](https://github.com/salakar/babel-preset-node6) on Node.js 6 and higher, and [`stage-2`](http://babeljs.io/docs/plugins/preset-stage-2/) Babel presets. This is a great option for small modules where you do not desire a build step to transpile your source before deploying to `npm`.
By default, AVA transpiles your tests and helper files using the [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) Babel preset. This is a great option for small modules where you do not desire a build step to transpile your source before deploying to `npm`.

## Customizing how AVA transpiles your tests

You can override the default Babel configuration AVA uses for test transpilation in `package.json`. For example, the configuration below adds the Babel `rewire` plugin, and opts to only use the Babel [`stage-3`](http://babeljs.io/docs/plugins/preset-stage-3/) preset (which is a subset of [`stage-2`](http://babeljs.io/docs/plugins/preset-stage-2/)).
You can override the default Babel configuration AVA uses for test transpilation in `package.json`. For example, the configuration below adds the Babel `rewire` plugin, and adds the Babel [`stage-3`](http://babeljs.io/docs/plugins/preset-stage-3/) preset.

```json
{
"ava": {
"babel": {
"plugins": ["rewire"],
"presets": ["es2015-node4", "stage-3"]
"presets": ["@ava/stage-4", "stage-3"]
}
}
}
Expand All @@ -43,7 +43,7 @@ To transpile your sources, you will need to define a [`babel config` ](http://ba
"require": ["babel-register"]
},
"babel": {
"presets": ["es2015-node4"]
"presets": ["@ava/stage-4"]
}
}
```
Expand All @@ -63,12 +63,12 @@ Using the `"inherit"` shortcut will cause your tests to be transpiled the same a
"babel": "inherit"
},
"babel": {
"presets": ["es2015-node4", "react"]
"presets": ["@ava/stage-4", "react"]
}
}
```

In the above example, both tests and sources will be transpiled using the [`es2015-node4`](https://github.com/jbach/babel-preset-es2015-node4) and [`react`](http://babeljs.io/docs/plugins/preset-react/) presets.
In the above example, both tests and sources will be transpiled using the [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) and [`react`](http://babeljs.io/docs/plugins/preset-react/) presets.

## Extend your source transpilation configuration

Expand All @@ -87,12 +87,12 @@ When specifying the Babel config for your tests, you can set the `babelrc` optio
}
},
"babel": {
"presets": ["es2015-node4", "react"]
"presets": ["@ava/stage-4", "react"]
}
}
```

In the above example, *sources* are compiled use [`es2015-node4`](https://github.com/jbach/babel-preset-es2015-node4) and [`react`](http://babeljs.io/docs/plugins/preset-react/), *tests* use those same plugins, plus the additional `custom` plugins specified.
In the above example, *sources* are compiled use [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) and [`react`](http://babeljs.io/docs/plugins/preset-react/), *tests* use those same plugins, plus the additional `custom` plugins specified.

## Extend an alternate config file.

Expand Down
10 changes: 0 additions & 10 deletions index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ type AssertContext = {
truthy(value: mixed, message?: string): void;
// Assert that value is falsy.
falsy(value: mixed, message?: string): void;
// DEPRECATED, use `truthy`. Assert that value is truthy.
ok(value: mixed, message?: string): void;
// DEPRECATED, use `falsy`. Assert that value is falsy.
notOk(value: mixed, message?: string): void;
// Assert that value is true.
true(value: mixed, message?: string): void;
// Assert that value is false.
Expand All @@ -60,12 +56,6 @@ type AssertContext = {
// Assert that value is not deep equal to expected.
notDeepEqual<U>(value: U, expected: U, message?: string): void;
// Assert that function throws an error or promise rejects.
// DEPRECATED, use `deepEqual`. Assert that value is deep equal to expected.
// @param error Can be a constructor, regex, error message or validation function.
same<U>(value: U, expected: U, message?: string): void;
// DEPRECATED use `notDeepEqual`. Assert that value is not deep equal to expected.
notSame<U>(value: U, expected: U, message?: string): void;
// Assert that function throws an error or promise rejects.
// @param error Can be a constructor, regex, error message or validation function.
throws(value: PromiseLike<mixed>, error?: ErrorValidator, message?: string): Promise<mixed>;
throws(value: () => void, error?: ErrorValidator, message?: string): mixed;
Expand Down
14 changes: 0 additions & 14 deletions lib/assert.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const util = require('util');
const assert = require('core-assert');
const deepEqual = require('lodash.isequal');
const observableToPromise = require('observable-to-promise');
Expand Down Expand Up @@ -184,16 +183,3 @@ x._snapshot = function (tree, optionalMessage, match, snapshotStateGetter) {
x.snapshot = function (tree, optionalMessage) {
x._snapshot.call(this, tree, optionalMessage);
};

/*
* Deprecated APIs
*/
x.ok = util.deprecate(x.truthy, getDeprecationNotice('ok()', 'truthy()'));
x.notOk = util.deprecate(x.falsy, getDeprecationNotice('notOk()', 'falsy()'));
x.same = util.deprecate(x.deepEqual, getDeprecationNotice('same()', 'deepEqual()'));
x.notSame = util.deprecate(x.notDeepEqual, getDeprecationNotice('notSame()', 'notDeepEqual()'));
x.error = util.deprecate(x.ifError, getDeprecationNotice('error()', 'ifError()'));

function getDeprecationNotice(oldApi, newApi) {
return `DEPRECATION NOTICE: ${oldApi} has been renamed to ${newApi} and will eventually be removed. See https://github.com/avajs/ava-codemods to help upgrade your codebase automatically.`;
}
78 changes: 18 additions & 60 deletions lib/babel-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const path = require('path');
const chalk = require('chalk');
const figures = require('figures');
const convertSourceMap = require('convert-source-map');
const semver = require('semver');
const colors = require('./colors');

function validate(conf) {
Expand All @@ -25,68 +24,26 @@ function validate(conf) {
return conf;
}

function lazy(initFn) {
let initialized = false;
let value;
function lazy(buildPreset) {
let preset;

return () => {
if (!initialized) {
initialized = true;
value = initFn();
return babel => {
if (!preset) {
preset = buildPreset(babel);
}

return value;
return preset;
};
}

const defaultPresets = lazy(() => {
const esPreset = semver.satisfies(process.version, '>=6') ?
'babel-preset-node6' :
'babel-preset-es2015-node4';
const stage4 = lazy(() => require('@ava/babel-preset-stage-4')());

return [
require('babel-preset-stage-2'),
require(esPreset) // eslint-disable-line import/no-dynamic-require
];
});

const rewritePlugin = lazy(() => {
const wrapListener = require('babel-plugin-detective/wrap-listener');

return wrapListener(rewriteBabelRuntimePaths, 'rewrite-runtime', {
generated: true,
require: true,
import: true
function makeTransformTestFiles(powerAssert) {
return lazy(babel => {
Copy link
Member

Choose a reason for hiding this comment

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

@novemberborn Do you remember why we have laziness here? There must be a reason. Maybe we should preserve the laziness?

Copy link
Member Author

Choose a reason for hiding this comment

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

It seemed unnecessary yesterday but I think I figured it out. This config is used even if all transforms have been cached. So we should keep the laziness in place.

Copy link
Member Author

Choose a reason for hiding this comment

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

Restored the lazy behavior, though in a somewhat simplified form.

return require('@ava/babel-preset-transform-test-files')(babel, {powerAssert});
});
});

function rewriteBabelRuntimePaths(path) {
const isBabelPath = /^babel-runtime[\\/]?/.test(path.node.value);

if (path.isLiteral() && isBabelPath) {
path.node.value = require.resolve(path.node.value);
}
}

const espowerPlugin = lazy(() => {
const babel = require('babel-core');
const createEspowerPlugin = require('babel-plugin-espower/create');

// Initialize power-assert
return createEspowerPlugin(babel, {
embedAst: true,
patterns: require('./enhance-assert').PATTERNS
});
});

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

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

Expand All @@ -95,7 +52,7 @@ function build(babelConfig, powerAssert, filePath, code) {
if (babelConfig === 'default') {
options = {
babelrc: false,
presets: defaultPresets()
presets: [stage4]
};
} else if (babelConfig === 'inherit') {
options = {
Expand All @@ -118,9 +75,10 @@ function build(babelConfig, powerAssert, filePath, code) {
ast: false
});

options.plugins = (options.plugins || [])
.concat(powerAssert ? espowerPlugin() : [])
.concat(defaultPlugins());
if (!options.presets) {
options.presets = [];
}
options.presets.push(makeTransformTestFiles(powerAssert));

return options;
}
Expand All @@ -143,8 +101,8 @@ function getSourceMap(filePath, code) {
module.exports = {
validate,
build,
pluginPackages: [
require.resolve('babel-core/package.json'),
require.resolve('babel-plugin-espower/package.json')
presetHashes: [
require('@ava/babel-preset-stage-4/package-hash'),
require('@ava/babel-preset-transform-test-files/package-hash')
]
};
16 changes: 9 additions & 7 deletions lib/caching-precompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ class CachingPrecompiler {
return `${result.code}\n${comment}`;
}
_createTransform() {
const pluginPackages = babelConfigHelper.pluginPackages;
const avaPackage = require.resolve('../package.json');
const packages = [avaPackage].concat(pluginPackages);
const majorNodeVersion = process.version.split('.')[0];
const babelConfig = JSON.stringify(this.babelConfig);
const packageSalt = babelConfig + majorNodeVersion + this.powerAssert;
const salt = packageHash.sync(packages, packageSalt);
const salt = packageHash.sync([
require.resolve('../package.json'),
require.resolve('babel-core/package.json')
], {
babelConfig: this.babelConfig,
majorNodeVersion: process.version.split('.')[0],
powerAssert: this.powerAssert,
presetHashes: babelConfigHelper.presetHashes
});

return cachingTransform({
factory: this._init,
Expand Down
10 changes: 4 additions & 6 deletions lib/enhance-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
module.exports = enhanceAssert;
module.exports.formatter = formatter;

// When adding patterns, don't forget to add to
// https://github.com/avajs/babel-preset-transform-test-files/blob/master/espower-patterns.json
// Then release a new version of that preset and bump the SemVer range here.
module.exports.PATTERNS = [
't.truthy(value, [message])',
't.falsy(value, [message])',
Expand All @@ -13,12 +16,7 @@ module.exports.PATTERNS = [
't.deepEqual(value, expected, [message])',
't.notDeepEqual(value, expected, [message])',
't.regex(contents, regex, [message])',
't.notRegex(contents, regex, [message])',
// Deprecated apis
't.ok(value, [message])',
't.notOk(value, [message])',
't.same(value, expected, [message])',
't.notSame(value, expected, [message])'
't.notRegex(contents, regex, [message])'
];

module.exports.NON_ENHANCED_PATTERNS = [
Expand Down
16 changes: 4 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"test-win": "tap --no-cov --reporter=classic --timeout=150 test/*.js test/reporters/*.js",
"visual": "node test/visual/run-visual-tests.js",
"prepublish": "npm run make-ts",
"make-ts": "babel-node --presets=babel-preset-es2015-node4 --plugins=transform-runtime types/make.js"
"make-ts": "node types/make.js"
},
"files": [
"lib",
Expand Down Expand Up @@ -87,6 +87,8 @@
"observables"
],
"dependencies": {
"@ava/babel-preset-stage-4": "^1.0.0",
"@ava/babel-preset-transform-test-files": "^1.0.0",
"arr-flatten": "^1.0.1",
"array-union": "^1.0.1",
"array-uniq": "^1.0.2",
Expand All @@ -95,14 +97,6 @@
"ava-init": "^0.2.0",
"babel-code-frame": "^6.16.0",
"babel-core": "^6.17.0",
"babel-plugin-ava-throws-helper": "^0.1.0",
"babel-plugin-detective": "^2.0.0",
"babel-plugin-espower": "^2.3.1",
"babel-plugin-transform-runtime": "^6.15.0",
"babel-preset-es2015-node4": "^2.1.0",
"babel-preset-node6": "^11.0.0",
"babel-preset-stage-2": "^6.17.0",
"babel-runtime": "^6.11.6",
"bluebird": "^3.0.0",
"caching-transform": "^1.0.0",
"chalk": "^1.0.0",
Expand Down Expand Up @@ -147,7 +141,7 @@
"multimatch": "^2.1.0",
"observable-to-promise": "^0.4.0",
"option-chain": "^0.1.0",
"package-hash": "^1.1.0",
"package-hash": "^1.2.0",
"pkg-conf": "^2.0.0",
"plur": "^2.0.0",
"power-assert-context-formatter": "^1.0.4",
Expand All @@ -157,7 +151,6 @@
"repeating": "^2.0.0",
"require-precompiled": "^0.1.0",
"resolve-cwd": "^1.0.0",
"semver": "^5.3.0",
"slash": "^1.0.0",
"source-map-support": "^0.4.0",
"stack-utils": "^0.4.0",
Expand All @@ -168,7 +161,6 @@
"update-notifier": "^1.0.0"
},
"devDependencies": {
"babel-cli": "^6.10.1",
"babel-preset-react": "^6.5.0",
"cli-table2": "^0.2.0",
"coveralls": "^2.11.4",
Expand Down
16 changes: 6 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Translations: [Español](https://github.com/avajs/ava-docs/blob/master/es_ES/rea
- Enforces writing atomic tests
- No implicit globals
- [Isolated environment for each test file](#process-isolation)
- [Write your tests in ES2015](#es2015-support)
- [Write your tests in ES2017](#es2017-support)
- [Promise support](#promise-support)
- [Generator function support](#generator-function-support)
- [Async function support](#async-function-support)
Expand Down Expand Up @@ -266,7 +266,7 @@ All of the CLI options can be configured in the `ava` section of your `package.j

Arguments passed to the CLI will always take precedence over the configuration in `package.json`.

See the [ES2015 support](#es2015-support) section for details on the `babel` option.
See the [ES2017 support](#es2017-support) section for details on the `babel` option.

## Documentation

Expand Down Expand Up @@ -671,21 +671,17 @@ test(t => {
});
```

### ES2015 support
### ES2017 support

AVA comes with built-in support for ES2015 through [Babel 6](https://babeljs.io). Just write your tests in ES2015. No extra setup needed. You can use any Babel version in your project. We use our own bundled Babel with the [`es2015`](https://babeljs.io/docs/plugins/preset-es2015/) and [`stage-2`](https://babeljs.io/docs/plugins/preset-stage-2/) presets, as well as the [`espower`](https://github.com/power-assert-js/babel-plugin-espower) and [`transform-runtime`](https://babeljs.io/docs/plugins/transform-runtime/) plugins.
AVA comes with built-in support for ES2017 through [Babel 6](https://babeljs.io). Just write your tests in ES2017. No extra setup needed. You can use any Babel version in your project. We use our own bundled Babel with our [`@ava/stage-4`](https://github.com/avajs/babel-preset-stage-4) and [`stage-2`](https://babeljs.io/docs/plugins/preset-stage-2/) preset, as well as [custom transforms](https://github.com/avajs/babel-preset-transform-test-files) for test and helper files.

The corresponding Babel config for AVA's setup is as follows:

```json
{
"presets": [
"es2015",
"stage-2"
],
"plugins": [
"espower",
"transform-runtime"
"@ava/stage-4",
"@ava/transform-test-files"
]
}
```
Expand Down
Loading