1. @ava/babel
Babel provider for AVA
@ava/babel
Package: @ava/babel
Created by: avajs
Last modified: Mon, 04 Apr 2022 16:41:54 GMT
Version: 2.0.0
License: MIT
Downloads: 23,475
Repository: https://github.com/avajs/babel

Install

npm install @ava/babel
yarn add @ava/babel

@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 preset-env.

package.json:

 {
	"ava": {
		"babel": {
			"extensions": [
				"js",
				"jsx"
			],
			"testOptions": {
				"plugins": ["@babel/plugin-syntax-jsx"],
				"presets": ["@babel/preset-env"]
			}
		}
	}
}

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.

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code