ava-patterns

Some useful helpers for tests in AVA

Usage no npm install needed!

<script type="module">
  import avaPatterns from 'https://cdn.skypack.dev/ava-patterns';
</script>

README

ava-patterns

npm CI Status dependencies Status devDependencies Status

Some useful helpers for tests in AVA.

Usage

Install ava-patterns by running:

yarn add ava-patterns

useTemporaryDirectory()

Create a temporary directory and delete it (and its contents) at the end of the test.

Returns an object with the following members:

  • path: The absolute path to the temporary directory.
  • writeFile(filePath, fileContents): Write a file with path relative to the temporary directory. Any leading whitespace in the file contents is stripped. If the file starts with a shebang, it is given executable permissions.
import test from 'ava'
import {useTemporaryDirectory} from 'ava-patterns'

test('writing files', async (t) => {
  const directory = await useTemporaryDirectory(t)
  await directory.writeFile('file.txt', `
    Hello World!
  `)
  t.pass()
})

runProcess()

Spawn a process and kill it at the end of the test.

The second argument supports the following options:

  • command: The command line command as an array of strings.
  • env: An object of environment variables.
  • cwd: The working directory in which to run the command

Returns an object with the following members:

  • output: A string containing all of the output from stdout and stderr.
  • outputStream: A Readable stream for both stdout and stderr.
  • waitForOutput(pattern, timeout = 1000): Enables waiting for a given substring or regular expression to be output, for up to a given timeout.
  • waitUntilExit(): Returns a promise that will resolve with the exit code.
  • childProcess: The underlying instance of ChildProcess
import test from 'ava'
import {runProcess} from 'ava-patterns'
import got from 'got'

test('running a Node server', async (t) => {
  const script = `
    import * as http from 'http'
    const server = http.createServer((request, response) => {
      response.end('Hello World!')
    })
    server.listen(10000, () => {
      console.log('Listening')
    })
  `

  const program = runProcess(t, {
    command: ['node', '--input-type', 'module', '--eval', script]
  })

  await program.waitForOutput('Listening')

  t.is(program.output, 'Listening\n')

  const response = await got('http://localhost:10000')
  t.is(response.body, 'Hello World!')
})

wait()

Wait for a given number of milliseconds.

import test from 'ava'
import {wait} from 'ava-patterns'

test('writing files', async (t) => {
  // perform action and wait for results
  await wait(500)
  // check results
})

http()

Make an HTTP request.

It takes in an object with properties method, url, optional headers, and an optional body and returns an object with properties status, headers, and body.

import {http} from 'ava-patterns'

test('sending HTTP requests', async (t) => {
  t.regex(await http('http://example.com'), /Example Domain/)

  const response = await http({
    method: 'POST',
    url: 'https://httpbin.org/post',
    body: 'Hello World!'
  })
  t.like(JSON.parse(response.body), {data: 'Hello World!'})
})