Home Manual Reference Source
Manual » Tutorial

Writing Tests

Test Setup/Teardown

Setup is performed via tests/config/setup.js. We require it with AVA's config, so that code should get run before test.

Teardown is perforved via test/config/teardown.js.

Client(s)

Each test file will need a before and after block for setting up clients and shutting them down.

In essence, our shell of a test looks like this:

import Client from '../../../src/clients/web/client.js'

let client

beforeAll(async () => {
  client = new Client(BROWSER_NAME, 'Example Test Name')
  await client.start()
})

afterEach(async () => {
  await client.clearCookies()
})

afterAll(async () => {
  await client.stop()
})

// Tests go here

So let's break it down into what our requirements for each test are:

  1. Before ALL tests in a file are run - create and start the browser(s)
  2. After EACH test in the file is run - clear out the cookies
  3. After ALL tess in a file are run - stop the browser

Additionally, there are a few things to note here, first: BROWSER_NAME. If your test requires a specific browser, specify it here. If not, the test will use the default browser we specify. Second, again notice that we are using async/await for our tests. Lastly, new Client takes a second argument, which is the test name. This is to identify the browser in either Zalenium or Sauce labs so we know what each session was doing.

Multiple clients

If your test requires multiple clients, you can create multiple clients as you see fit like so:

import Client from '../../../src/clients/web/client.js'

let clientA, clientB

beforeAll(async () => {
  clientA = new Client('chrome', 'BrowserA for Example Test')
  clientB = new Client('ie', 'BrowserB for Example Test)
  await clientA.start()
  await clientB.start()
})

afterEach(async () => {
  await clientA.clearCookies()
  await clientB.clearCookies()
})

afterAll(async () => {
  await clientA.stop()
  await clientB.stop()
})

// Tests go here

With these before and after sections done, our browser will start and then end for our test.

The Actual Test

Below is an example test. Notice that there is now a beforeEach block. We can use those for additional setup that needs to happen before each test in the file is run, such as creating users, logging in, or starting calls.

import Client from '../../../src/clients/web/client.js'

beforeAll(async () => {
  client = new Client(BROWSER_NAME, 'Getting Text from the Thing')
  await client.start()
})

beforeEach(async () => {
  t.context.secretPassword = await SomePasswordApiService.getPassword()
})

afterEach(async () => {
  await client.clearCookies()
})

afterAll(async () => {
  await client.stop()
})

test('get text from the thing', async () => {
  await client.myContreller.login('user@email.com', t.context.secretPassword)
  await client.myController.clickAround()
  const text = await client.myController.getSomeText()
  expect(text).toBe('expected text')
})

Expect vs Should vs Assert

Use the expect syntax for your expectations in the test.

Testing Journeys

At some point it may not be worth it to write small atomic tests if the setup for these tests is expensive or timely to use. This is where the concept of "testing journeys" comes in! A journey will be considered a full user flow where we can make assertions throughout the entire flow. The main difference between how we normally write tests is that the tests within the journey are entirely dependant on the test order. For an example, take a look at the p2pCall.journey.js test.