Skip to content

Commit 576f534

Browse files
Scott Martinnovemberborn
andauthored
Update examples in docs to use ES Modules
Co-authored-by: Mark Wubben <[email protected]>
1 parent 3131ccd commit 576f534

15 files changed

+51
-48
lines changed

docs/01-writing-tests.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ AVA will set `process.env.NODE_ENV` to `test`, unless the `NODE_ENV` environment
1919
To declare a test you call the `test` function you imported from AVA. Provide the required title and implementation function. Titles must be unique within each test file. The function will be called when your test is run. It's passed an [execution object](./02-execution-context.md) as its first argument.
2020

2121
```js
22-
const test = require('ava');
22+
import test from 'ava';
2323

2424
test('my passing test', t => {
2525
t.pass();
@@ -260,7 +260,7 @@ Available properties:
260260
* `snapshotDirectory`: directory where snapshots are stored, as a file URL string
261261

262262
```js
263-
const test = require('ava');
263+
import test from 'ava';
264264

265265
console.log('Test file currently being run:', test.meta.file);
266266
```

docs/02-execution-context.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Translations: [Français](https://github.com/avajs/ava-docs/blob/master/fr_FR/do
55
Each test or hook is called with an execution context. By convention it's named `t`.
66

77
```js
8-
const test = require('ava');
8+
import test from 'ava';
99

1010
test('my passing test', t => {
1111
t.pass();

docs/03-assertions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ This won't give you as nice an experience as you'd get with the [built-in assert
8484
You'll have to configure AVA to not fail tests if no assertions are executed, because AVA can't tell if custom assertions pass. Set the `failWithoutAssertions` option to `false` in AVA's [`package.json` configuration](./06-configuration.md).
8585

8686
```js
87-
const assert = require('assert');
87+
import assert from 'assert';
8888

8989
test('custom assertion', t => {
9090
assert(true);

docs/06-configuration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ You can now run your unit tests through `npx ava` and the integration tests thro
214214
By default, AVA prints nested objects to a depth of `3`. However, when debugging tests with deeply nested objects, it can be useful to print with more detail. This can be done by setting [`util.inspect.defaultOptions.depth`](https://nodejs.org/api/util.html#util_util_inspect_defaultoptions) to the desired depth, before the test is executed:
215215

216216
```js
217-
const util = require('util');
217+
import util from 'util';
218218

219-
const test = require('ava');
219+
import test from 'ava';
220220

221221
util.inspect.defaultOptions.depth = 5; // Increase AVA's printing depth
222222

docs/08-common-pitfalls.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ test('fetches foo', async t => {
4242
If you're using callbacks, promisify the callback function using something like [`util.promisify()`](https://nodejs.org/dist/latest/docs/api/util.html#util_util_promisify_original):
4343

4444
```js
45-
const {promisify} = require('util');
45+
import {promisify} from 'util';
4646

4747
test('fetches foo', async t => {
4848
const data = await promisify(fetch)();
@@ -61,7 +61,7 @@ By default AVA executes tests concurrently. This can cause problems if your test
6161
Take this contrived example:
6262

6363
```js
64-
const test = require('ava');
64+
import test from 'ava';
6565

6666
let count = 0;
6767
const incr = async () => {
@@ -88,7 +88,7 @@ test('increment twice', async t => {
8888
Concurrent tests allow for asynchronous tests to execute more quickly, but if they rely on shared state you this may lead to unexpected test failures. If the shared state cannot be avoided, you can execute your tests serially:
8989

9090
```js
91-
const test = require('ava');
91+
import test from 'ava';
9292

9393
let count = 0;
9494
const incr = async () => {

docs/recipes/browser-testing.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@ Create a helper file, prefixed with an underscore. This ensures AVA does not tre
2727
`test/_setup-browser-env.js`:
2828

2929
```js
30-
const browserEnv = require('browser-env');
30+
import browserEnv from 'browser-env';
3131
browserEnv();
3232
```
3333

3434
By default, `browser-env` will add all global browser variables to the Node.js global scope, creating a full browser environment. This should have good compatibility with most front-end libraries, however, it's generally not a good idea to create lots of global variables if you don't need to. If you know exactly which browser globals you need, you can pass an array of them.
3535

3636
```js
37-
const browserEnv = require('browser-env');
37+
import browserEnv from 'browser-env';
3838
browserEnv(['window', 'document', 'navigator']);
3939
```
4040

4141
You can expose more global variables by assigning them to the `global` object. For instance, jQuery is typically available through the `$` variable:
4242

4343
```js
44-
const browserEnv = require('browser-env');
45-
const jQuery = require('jquery');
44+
import browserEnv from 'browser-env';
45+
import jQuery from 'jquery';
4646

4747
browserEnv();
4848
global.$ = jQuery(window);
@@ -71,7 +71,7 @@ Write your tests and enjoy a mocked browser environment.
7171
`test.js`:
7272

7373
```js
74-
const test = require('ava');
74+
import test from 'ava';
7575

7676
test('Insert to DOM', t => {
7777
const div = document.createElement('div');

docs/recipes/endpoint-testing-with-mongoose.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ First, include the libraries you need:
3838

3939
```js
4040
// Libraries required for testing
41-
const test = require('ava');
42-
const request = require('supertest');
43-
const {MongoMemoryServer} = require('mongodb-memory-server');
44-
const mongoose = require('mongoose');
41+
import test from 'ava';
42+
import request from 'supertest';
43+
import {MongoMemoryServer} from 'mongodb-memory-server';
44+
import mongoose from 'mongoose';
4545

4646
// Your server and models
47-
const app = require('../server');
48-
const User = require('../models/User');
47+
import app from '../server';
48+
import User from '../models/User';
4949
```
5050

5151
Next start the in-memory MongoDB instance and connect to Mongoose:

docs/recipes/endpoint-testing.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ Since tests run concurrently, it's best to create a fresh server instance at lea
1111
Check out the example below:
1212

1313
```js
14-
const http = require('http');
15-
const test = require('ava');
16-
const got = require('got');
17-
const listen = require('test-listen');
18-
const app = require('../app');
14+
import http from 'http';
15+
import test from 'ava';
16+
import got from 'got';
17+
import listen from 'test-listen';
18+
import app from '../app';
1919

2020
test.before(async t => {
2121
t.context.server = http.createServer(app);

docs/recipes/isolated-mongodb-integration-tests.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ In your test file, import the module, and run the server.
2323
**Make sure to run the server at the start of your file, outside of any test cases.**
2424

2525
```js
26-
const test = require('ava');
27-
const {MongoDBServer} = require('mongomem');
26+
import test from 'ava';
27+
import {MongoDBServer} from 'mongomem';
2828

2929
test.before('start server', async t => {
3030
await MongoDBServer.start();

docs/recipes/passing-arguments-to-your-test-files.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ You can pass command line arguments to your test files. Use the `--` argument te
66

77
```js
88
// test.js
9-
const test = require('ava');
9+
import test from 'ava';
1010

1111
test('argv', t => {
1212
t.deepEqual(process.argv.slice(2), ['--hello', 'world']);

0 commit comments

Comments
 (0)