Skip to content

Commit

Permalink
Update examples in docs to use ES Modules
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Wubben <mark@novemberborn.net>
  • Loading branch information
scottdotjs and novemberborn committed Feb 13, 2022
1 parent 3131ccd commit 576f534
Show file tree
Hide file tree
Showing 15 changed files with 51 additions and 48 deletions.
4 changes: 2 additions & 2 deletions docs/01-writing-tests.md
Expand Up @@ -19,7 +19,7 @@ AVA will set `process.env.NODE_ENV` to `test`, unless the `NODE_ENV` environment
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.

```js
const test = require('ava');
import test from 'ava';

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

```js
const test = require('ava');
import test from 'ava';

console.log('Test file currently being run:', test.meta.file);
```
Expand Down
2 changes: 1 addition & 1 deletion docs/02-execution-context.md
Expand Up @@ -5,7 +5,7 @@ Translations: [Fran莽ais](https://github.com/avajs/ava-docs/blob/master/fr_FR/do
Each test or hook is called with an execution context. By convention it's named `t`.

```js
const test = require('ava');
import test from 'ava';

test('my passing test', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion docs/03-assertions.md
Expand Up @@ -84,7 +84,7 @@ This won't give you as nice an experience as you'd get with the [built-in assert
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).

```js
const assert = require('assert');
import assert from 'assert';

test('custom assertion', t => {
assert(true);
Expand Down
4 changes: 2 additions & 2 deletions docs/06-configuration.md
Expand Up @@ -214,9 +214,9 @@ You can now run your unit tests through `npx ava` and the integration tests thro
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:

```js
const util = require('util');
import util from 'util';

const test = require('ava');
import test from 'ava';

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

Expand Down
6 changes: 3 additions & 3 deletions docs/08-common-pitfalls.md
Expand Up @@ -42,7 +42,7 @@ test('fetches foo', async t => {
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):

```js
const {promisify} = require('util');
import {promisify} from 'util';

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

```js
const test = require('ava');
import test from 'ava';

let count = 0;
const incr = async () => {
Expand All @@ -88,7 +88,7 @@ test('increment twice', async t => {
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:

```js
const test = require('ava');
import test from 'ava';

let count = 0;
const incr = async () => {
Expand Down
10 changes: 5 additions & 5 deletions docs/recipes/browser-testing.md
Expand Up @@ -27,22 +27,22 @@ Create a helper file, prefixed with an underscore. This ensures AVA does not tre
`test/_setup-browser-env.js`:

```js
const browserEnv = require('browser-env');
import browserEnv from 'browser-env';
browserEnv();
```

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.

```js
const browserEnv = require('browser-env');
import browserEnv from 'browser-env';
browserEnv(['window', 'document', 'navigator']);
```

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

```js
const browserEnv = require('browser-env');
const jQuery = require('jquery');
import browserEnv from 'browser-env';
import jQuery from 'jquery';

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

```js
const test = require('ava');
import test from 'ava';

test('Insert to DOM', t => {
const div = document.createElement('div');
Expand Down
12 changes: 6 additions & 6 deletions docs/recipes/endpoint-testing-with-mongoose.md
Expand Up @@ -38,14 +38,14 @@ First, include the libraries you need:

```js
// Libraries required for testing
const test = require('ava');
const request = require('supertest');
const {MongoMemoryServer} = require('mongodb-memory-server');
const mongoose = require('mongoose');
import test from 'ava';
import request from 'supertest';
import {MongoMemoryServer} from 'mongodb-memory-server';
import mongoose from 'mongoose';

// Your server and models
const app = require('../server');
const User = require('../models/User');
import app from '../server';
import User from '../models/User';
```

Next start the in-memory MongoDB instance and connect to Mongoose:
Expand Down
10 changes: 5 additions & 5 deletions docs/recipes/endpoint-testing.md
Expand Up @@ -11,11 +11,11 @@ Since tests run concurrently, it's best to create a fresh server instance at lea
Check out the example below:

```js
const http = require('http');
const test = require('ava');
const got = require('got');
const listen = require('test-listen');
const app = require('../app');
import http from 'http';
import test from 'ava';
import got from 'got';
import listen from 'test-listen';
import app from '../app';

test.before(async t => {
t.context.server = http.createServer(app);
Expand Down
4 changes: 2 additions & 2 deletions docs/recipes/isolated-mongodb-integration-tests.md
Expand Up @@ -23,8 +23,8 @@ In your test file, import the module, and run the server.
**Make sure to run the server at the start of your file, outside of any test cases.**

```js
const test = require('ava');
const {MongoDBServer} = require('mongomem');
import test from 'ava';
import {MongoDBServer} from 'mongomem';

test.before('start server', async t => {
await MongoDBServer.start();
Expand Down
2 changes: 1 addition & 1 deletion docs/recipes/passing-arguments-to-your-test-files.md
Expand Up @@ -6,7 +6,7 @@ You can pass command line arguments to your test files. Use the `--` argument te

```js
// test.js
const test = require('ava');
import test from 'ava';

test('argv', t => {
t.deepEqual(process.argv.slice(2), ['--hello', 'world']);
Expand Down
6 changes: 3 additions & 3 deletions docs/recipes/puppeteer.md
Expand Up @@ -13,7 +13,7 @@ The first step is setting up a helper to configure the environment:
`./test/_withPage.js`

```js
const puppeteer = require('puppeteer');
import puppeteer from 'puppeteer';

module.exports = async (t, run) => {
const browser = await puppeteer.launch();
Expand All @@ -32,8 +32,8 @@ module.exports = async (t, run) => {
`./test/main.js`

```js
const test = require('ava');
const withPage = require('./_withPage');
import test from 'ava';
import withPage from './_withPage';

const url = 'https://google.com';

Expand Down
20 changes: 10 additions & 10 deletions docs/recipes/react.md
Expand Up @@ -47,8 +47,8 @@ Create a helper file, prefixed with an underscore. This ensures AVA does not tre
`test/_setup-enzyme-adapter.js`:

```js
const Enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({
adapter: new Adapter()
Expand Down Expand Up @@ -78,10 +78,10 @@ Then you can use Enzyme straight away:
`test.js`:

```js
const test = require('ava');
const React = require('react');
const PropTypes = require('prop-types');
const {shallow} = require('enzyme');
import test from 'ava';
import React from 'react';
import PropTypes from 'prop-types';
import {shallow} from 'enzyme';

const Foo = ({children}) =>
<div className="Foo">
Expand Down Expand Up @@ -131,10 +131,10 @@ $ npm install --save-dev jsx-test-helpers
Usage example:

```js
const test = require('ava');
const React = require('react');
const PropTypes = require('prop-types');
const {renderJSX, JSX} = require('jsx-test-helpers');
import test from 'ava';
import React from 'react';
import PropTypes from 'prop-types';
import {renderJSX, JSX} from 'jsx-test-helpers';

const Foo = ({children}) =>
<div className="Foo">
Expand Down
7 changes: 3 additions & 4 deletions docs/recipes/testing-with-selenium-webdriverjs.md
Expand Up @@ -25,10 +25,9 @@ Create the following files:
In both files, let's first include the packages:

```js
const test = require('ava');
const {Builder, By, Key, until} = require('selenium-webdriver');

require('chromedriver');
import test from 'ava';
import {Builder, By, Key, until} from 'selenium-webdriver';
import 'chromedriver';
```

In the `bingtest.js` file, add the following code, which tests whether searching for `webdriver` on Bing, returns results.
Expand Down
5 changes: 3 additions & 2 deletions docs/recipes/vue.md
Expand Up @@ -30,10 +30,11 @@ The first step is setting up a helper to configure the environment to transpile
```

```js
// ./test/_setup.js
// ./test/_setup.cjs

// Set up JSDom.
require('jsdom-global')()
const jsdomGlobal = require('jsdom-global');
jsdomGlobal();

// Fix the Date object, see <https://github.com/vuejs/vue-test-utils/issues/936#issuecomment-415386167>.
window.Date = Date
Expand Down
5 changes: 4 additions & 1 deletion docs/recipes/when-to-use-plan.md
Expand Up @@ -98,7 +98,10 @@ As stated in the previous example, using the `t.throws()` assertion with `async`
In most cases, it's a bad idea to use any complex branching inside your tests. A notable exception is for tests that are auto-generated (perhaps from a JSON document). Below `t.plan()` is used to ensure the correctness of the JSON input:

```js
const testData = require('./fixtures/test-definitions.json');
import fs from 'fs';
import path from 'path';

const testData = JSON.parse(fs.readFileSync(new URL('./fixtures/test-definitions.json', import.meta.url)));

for (const testDefinition of testData) {
test('foo or bar', t => {
Expand Down

0 comments on commit 576f534

Please sign in to comment.