Skip to content

Commit

Permalink
Use CJS syntax in documentation
Browse files Browse the repository at this point in the history
Out of the box, ESM syntax will only work in Node.js 13, so having that
as the default example is misleading.

Leave it for the TypeScript recipe since TypeScript doesn't work out of
the box anyway. Leave the config example, which will be updated when we
update the config formats for ESM.
  • Loading branch information
novemberborn committed Dec 31, 2019
1 parent 9deaa41 commit 79b2ea3
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions docs/01-writing-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ To declare a test you call the `test` function you imported from AVA. Provide th
**Note:** In order for the [enhanced assertion messages](./03-assertions.md#enhanced-assertion-messages) to behave correctly, the first argument **must** be named `t`.

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

test('my passing test', t => {
t.pass();
Expand Down Expand Up @@ -272,7 +272,7 @@ test('context is unicorn', t => {
Helper files can determine the filename of the test being run by reading `test.meta.file`. This eliminates the need to pass `__filename` from the test to helpers.

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

console.log('Test currently being run: ', test.meta.file);
```
Expand Down
2 changes: 1 addition & 1 deletion docs/02-execution-context.md
Original file line number Diff line number Diff line change
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
import test from 'ava';
const test = require('ava');

test('my passing test', t => {
t.pass();
Expand Down
2 changes: 1 addition & 1 deletion docs/03-assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,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
import assert from 'assert';
const assert = require('assert');

test('custom assertion', t => {
assert(true);
Expand Down
6 changes: 3 additions & 3 deletions docs/04-snapshot-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export default HelloWorld;

```js
// Your test
import test from 'ava';
import render from 'react-test-renderer';
import HelloWorld from '.';
const test = require('ava');
const render = require('react-test-renderer');
const HelloWorld = require('.');

test('HelloWorld component', t => {
const tree = render.create(<HelloWorld/>).toJSON();
Expand Down
4 changes: 2 additions & 2 deletions docs/06-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,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
import util from 'util';
const util = require('util');

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

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

Expand Down
4 changes: 2 additions & 2 deletions docs/08-common-pitfalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ By default AVA executes tests concurrently. This can cause problems if your test
Take this contrived example:

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

let count = 0;
const incr = async () => {
Expand All @@ -109,7 +109,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
import test from 'ava';
const test = require('ava');

let count = 0;
const incr = async () => {
Expand Down
10 changes: 5 additions & 5 deletions docs/recipes/browser-testing.md
Original file line number Diff line number Diff line change
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
import browserEnv from 'browser-env';
const browserEnv = require('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
import browserEnv from 'browser-env';
const browserEnv = require('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
import browserEnv from 'browser-env';
import jQuery from 'jquery';
const browserEnv = require('browser-env');
const jQuery = require('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
import test from 'ava';
const test = require('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
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ First, include the libraries you need:

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

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

Next start the in-memory MongoDB instance and connect to Mongoose:
Expand Down
4 changes: 2 additions & 2 deletions docs/recipes/es-modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export default function sum(a, b) {

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

test('2 + 2 = 4', t => {
t.is(sum(2, 2), 4);
Expand Down
8 changes: 4 additions & 4 deletions docs/recipes/flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Create a `test.js` file.

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

const getFoo = () => 'foo';

Expand All @@ -49,8 +49,8 @@ By default, the type of `t.context` will be the empty object (`{}`). AVA exposes

```js
// @flow
import anyTest from 'ava';
import type {TestInterface} from 'ava';
const anyTest = require('ava');
const type {TestInterface} = require('ava');

const test: TestInterface<{foo: string}> = (anyTest: any);

Expand Down Expand Up @@ -79,7 +79,7 @@ The `t.throws()` and `t.throwsAsync()` assertions are typed to always return an

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

class CustomError extends Error {
parent: Error;
Expand Down
4 changes: 2 additions & 2 deletions docs/recipes/isolated-mongodb-integration-tests.md
Original file line number Diff line number Diff line change
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
import test from 'ava';
import {MongoDBServer} from 'mongomem';
const test = require('ava');
const {MongoDBServer} = require('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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ You can pass command line arguments to your test files. Use the `--` argument te

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

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

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

export default async function withPage(t, run) {
const browser = await puppeteer.launch();
Expand All @@ -32,8 +32,8 @@ export default async function withPage(t, run) {
`./test/main.js`

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

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

Expand Down
12 changes: 6 additions & 6 deletions docs/recipes/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ $ npm install --save-dev enzyme react-addons-test-utils react-dom
Then you can use Enzyme straight away:

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

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

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

const Foo = ({children}) =>
<div className="Foo">
Expand Down
6 changes: 3 additions & 3 deletions docs/recipes/vue.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ You can find more information about setting up Babel with AVA in [`@ava/babel`](
## Sample snapshot test

```js
import test from 'ava';
import Vue from 'vue';
import Component from 'component.vue';
const test = require('ava');
const Vue = require('vue');
const Component = require('component.vue');

test('renders', t => {
const vm = new Vue(Component).$mount();
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Don't forget to configure the `test` script in your `package.json` as per above.
Create a file named `test.js` in the project root directory:

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

test('foo', t => {
t.pass();
Expand Down

0 comments on commit 79b2ea3

Please sign in to comment.