Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ava/babel

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ava/babel

Babel provider for AVA

  • 2.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

@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

ProposalSupported
Array.prototype.includesNo
Object.values/Object.entriesNo
String paddingNo
Object.getOwnPropertyDescriptorsNo
Trailing commas in function parameter lists and callsYes
Shared memory and atomicsNo
Lifting template literal restrictionNo
s (dotAll) flag for regular expressionsYes
RegExp named capture groupsNo
RegExp Lookbehind AssertionsNo
RegExp Unicode Property EscapesNo
Promise.prototype.finallyNo
Asynchronous IterationPartially
Optional catch bindingYes
JSON supersetNo
Symbol.prototype.descriptionNo
Function.prototype.toString revisionNo
Object.fromEntriesNo
Well-formed JSON.stringifyNo
String.prototype.{trimStart,trimEnd}No
Array.prototype.{flat,flatMap}No
String.prototype.matchAllNo
import()Yes
Promise.allSettledNo
Optional ChainingYes
Nullish coalescing OperatorYes

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

Keywords

FAQs

Package last updated on 04 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc