@ava/babel
Babel provider for AVA
Last updated 5 years ago by novemberborn .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install @ava/babel 
SYNC missed versions from official npm registry.

@ava/babel

Translations: Français

Adds Babel 7 support to AVA so you can use the latest JavaScript syntax in your tests. We do this by compiling test and helper files using our @ava/babel/stage-4 preset. We also use a second preset, @ava/babel/transform-test-files to enable enhanced assertion messages and detect improper use of t.throws() assertions.

By default our Babel pipeline is applied to test and helper files ending in .cjs and .js. If your project uses Babel then we'll automatically compile these files using your project's Babel configuration. The @ava/babel/transform-helper-files preset is applied first, and the @ava/babel/stage-4 last.

If you are using Babel for your source files then you must also configure source compilation.

Enabling Babel support

Add this package to your project:

npm install --save-dev @ava/babel

Then, enable Babel support either in package.json or ava.config.*:

package.json:

{
	"ava": {
		"babel": true
	}
}

Customize how AVA compiles your test files

You can override the default Babel configuration AVA uses for test file compilation in package.json or ava.config.*. For example, the configuration below adds support for JSX syntax and stage 3 features.

package.json:

{
	"ava": {
		"babel": {
			"testOptions": {
				"plugins": ["@babel/plugin-syntax-jsx"],
				"presets": ["@babel/preset-stage-3"]
			}
		}
	}
}

All Babel options are allowed inside the testOptions object.

Compile additional files

By default, only test files are compiled. You can compile additional files as tests by providing glob patterns:

package.json:

{
	"ava": {
		"babel": {
			"compileAsTests": ["test/helpers/**/*"]
		}
	}
}

Reset AVA's cache

AVA caches the compiled test and helper files. It automatically recompiles these files when you change them. AVA tries its best to detect changes to your Babel configuration files, plugins and presets. If it seems like your latest Babel configuration isn't being applied, however, you can reset AVA's cache:

$ npx ava reset-cache

Add additional extensions

You can configure AVA to recognize additional file extensions and compile those test & helper files using Babel.

package.json:

{
	"ava": {
		"babel": {
			"extensions": [
				"js",
				"jsx"
			]
		}
	}
}

See also AVA's extensions option.

Make AVA skip your project's Babel options

You may not want AVA to use your project's Babel options, for example if your project is relying on Babel 6. Set the babelrc and configFile options to false.

package.json:

{
	"ava": {
		"babel": {
			"testOptions": {
				"babelrc": false,
				"configFile": false
			}
		}
	}
}

Disable AVA's stage-4 preset

You can disable AVA's stage-4 preset.

package.json:

{
	"ava": {
		"babel": {
			"testOptions": {
				"presets": [
					["module:@ava/babel/stage-4", false]
				]
			}
		}
	}
}

Note that this does not stop AVA from compiling your test files using Babel.

If you want, you can disable the preset in your project's Babel configuration.

Preserve ES module syntax

By default AVA's stage-4 preset will convert ES module syntax to CommonJS. This can be disabled.

package.json:

{
	"ava": {
		"babel": {
			"testOptions": {
				"presets": [
					["module:@ava/babel/stage-4", {"modules": false}]
				]
			}
		}
	}
}

You'll have to use the esm module so that AVA can still load your test files. See our recipe for details.

Disable enhancements

By default, AVA will compile test and helper files with some additional enhancements. Disable this by setting compileEnhancements to false:

package.json:

{
	"ava": {
		"babel": {
			"compileEnhancements": false
		}
	}
}

Use Babel polyfills

AVA lets you write your tests using new JavaScript syntax, even on Node.js versions that otherwise wouldn't support it. However, it doesn't add or modify built-ins of your current environment. Using AVA would, for example, not provide modern features such as Object.fromEntries() to an underlying Node.js 10 environment.

By loading Babel's polyfill module you can opt in to these features. Note that this will modify the environment, which may influence how your program behaves.

You can enable the polyfill module by adding it to AVA's require option.

package.json:

{
	"ava": {
		"require": [
			"@babel/polyfill"
		]
	}
}

You'll need to install @babel/polyfill yourself.

Compile sources

AVA does not currently compile source files. You'll have to load Babel's register module, which will compile source files as needed.

You can enable the register module by adding it to AVA's require option.

package.json:

{
	"ava": {
		"require": [
			"@babel/register"
		]
	}
}

You'll need to install @babel/register yourself.

@babel/register will also process your test and helper files. For most use cases this is unnecessary. If you create a new file that requires @babel/register you can tell it which file paths to ignore. For instance in your test directory create _register.js:

// test/_register.js:
require('@babel/register')({
	// These patterns are relative to the project directory (where the `package.json` file lives):
	ignore: ['node_modules/*', 'test/*']
});

Now instead of requiring @babel/register, require ./test/_register instead.

package.json:

{
	"ava": {
		"require": [
			"./test/_register.js"
		]
	}
}

Note that loading @babel/register in every worker process has a non-trivial performance cost. If you have lots of test files, you may want to consider using a build step to compile your sources before running your tests. This isn't ideal, since it complicates using AVA's watch mode, so we recommend using @babel/register until the performance penalty becomes too great. Setting up a precompilation step is out of scope for this document, but we recommend you check out one of the many build systems that support Babel. There is an issue discussing ways we could make this experience better.

Webpack aliases

Webpack aliases can be used to provide a shortcut to deeply nested or otherwise inconvenient paths. If you already use aliases in your source files, you'll need to make sure you can use the same aliases in your test files.

Install babel-plugin-webpack-alias-7 as a dev-dependency. Then add the plugin to AVA's Babel config:

package.json:

{
	"ava": {
		"babel": {
			"testOptions": {
				"plugins": [
					[
						"babel-plugin-webpack-alias-7",
						{
							"config": "./path/to/webpack.config.test.js"
						}
					]
				]
			}
		}
	}
}

@ava/babel/stage-4

Aspires to bring finished ECMAScript proposals to AVA's test and helper files.

Efficiently applies the minimum of transforms to run the latest JavaScript syntax on Node.js 10 and 12.

Built-ins are not added or extended, so features like Proxies, Array.prototype.includes or String.prototype.padStart will only be available if the Node.js version running the tests supports it. Consult node.green for details.

Sometimes a particular feature is mostly implemented in Node.js. In that case transforms are not applied.

Not all proposals can be supported via Babel transforms, see below for details. Babel may require "syntax" plugins in order to parse certain files. These plugins should be applied explicitly since this preset may not include them. If you use AVA's Babel compilation this is already taken care of.

Supported proposals

Proposal Supported
Array.prototype.includes No
Object.values/Object.entries No
String padding No
Object.getOwnPropertyDescriptors No
Trailing commas in function parameter lists and calls Yes
Shared memory and atomics No
Lifting template literal restriction No
s (dotAll) flag for regular expressions Yes
RegExp named capture groups No
RegExp Lookbehind Assertions No
RegExp Unicode Property Escapes No
Promise.prototype.finally No
Asynchronous Iteration Partially
Optional catch binding Yes
JSON superset No
Symbol.prototype.description No
Function.prototype.toString revision No
Object.fromEntries No
Well-formed JSON.stringify No
String.prototype.{trimStart,trimEnd} No
Array.prototype.{flat,flatMap} No
String.prototype.matchAll No
import() Yes
Promise.allSettled No
Optional Chaining Yes
Nullish coalescing Operator Yes

@babel/plugin-proposal-async-generator-functions relies on Symbol.asyncIterator, which AVA does not polyfill for you.

Current Tags

  • 2.0.0                                ...           latest (3 years ago)

12 Versions

  • 2.0.0                                ...           3 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.7.0                                ...           5 years ago
  • 0.6.0                                ...           5 years ago
  • 0.5.0                                ...           5 years ago
  • 0.4.0                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
  • 0.0.0                                ...           5 years ago
  • 0.0.0-1                                ...           5 years ago
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dev Dependencies (6)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE