Skip to content

Commit 0b27db5

Browse files
motssnovemberborn
authored andcommitted
Add ES Modules recipe
1 parent 632dc3e commit 0b27db5

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

docs/recipes/babel.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ By default AVA's stage-4 preset will convert ES module syntax to CommonJS. This
8181
}
8282
```
8383

84-
You'll have to use [`@std/esm`](https://github.com/standard-things/esm) so that AVA can still load your test files.
84+
You'll have to use [`@std/esm`](https://github.com/standard-things/esm) so that AVA can still load your test files. [See our recipe for details](./es-modules.md).
8585

8686
## Disable AVA's Babel pipeline
8787

docs/recipes/es-modules.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Using ES modules in AVA
2+
3+
As of Node.js 8.5.0, [ES modules](http://2ality.com/2017/09/native-esm-node.html) are natively supported, but behind the `--experimental-modules` command line flag. It works using the `.mjs` file extension. AVA does not currently support the command line option or the new file extension, but you *can* use the [`@std/esm`](https://github.com/standard-things/esm) module to use the new syntax.
4+
5+
Here's how you get it working with AVA.
6+
7+
First, install [`@std/esm`](https://github.com/standard-things/esm):
8+
9+
```
10+
$ npm install @std/esm
11+
```
12+
13+
Configure it in your `package.json` file, and add it to AVA's `"require"` option as well. Make sure to add it as the first item:
14+
15+
```json
16+
{
17+
"ava": {
18+
"require": [
19+
"@std/esm"
20+
]
21+
},
22+
"@std/esm": "js"
23+
}
24+
```
25+
26+
By default AVA converts ES module syntax to CommonJS. [You can disable this](./babel.md#preserve-es-module-syntax).
27+
28+
You can now use native ES modules with AVA:
29+
30+
```js
31+
// sum.mjs
32+
export default function sum(a, b) {
33+
return a + b;
34+
};
35+
```
36+
37+
```js
38+
// test.js
39+
import test from 'ava';
40+
import sum from './sum.mjs';
41+
42+
test('2 + 2 = 4', t => {
43+
t.is(sum(2, 2), 4);
44+
});
45+
```
46+
47+
Note that test files still need to use the `.js` extension.

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ It's the [Andromeda galaxy](https://simple.wikipedia.org/wiki/Andromeda_galaxy).
11401140
- [TypeScript](docs/recipes/typescript.md)
11411141
- [Flow](docs/recipes/flow.md)
11421142
- [Configuring Babel][Babel recipe]
1143+
- [Using ES modules](docs/recipes/es-modules.md)
11431144
- [Testing React components](docs/recipes/react.md)
11441145
- [Testing Vue.js components](docs/recipes/vue.md)
11451146
- [JSPM and SystemJS](docs/recipes/jspm-systemjs.md)

0 commit comments

Comments
 (0)