3

I would like to use test.before() to bootstrap my tests. The setup I have tried does not work:

// bootstrap.js
const test = require('ava')

test.before(t => {
  // do this exactly once for all tests
})


module.exports = { test }


// test1.js

const { test } = require('../bootstrap')

test(t => { ... {)

AVA will run the before() function before each test file. I could make a check within the before call to check if it has been called but I'd like to find a cleaner process. I have tried using the require parameter with:

"ava": {
  "require": [
    "./test/run.js"
  ]
 }

With:

// bootstrap,js
const test = require('ava')

module.exports = { test }


// run.js

const { test } = require('./bootstrap')

test.before(t => { })


// test1.js
const { test } = require('../bootstrap')

test(t => { ... {)

But that just breaks with worker.setRunner is not a function. Not sure what it expects there.

cyberwombat
  • 38,105
  • 35
  • 175
  • 251

2 Answers2

3

AVA runs each test file in its own process. test.before() should be used to set up fixtures that are used just by the process it's called in.

It sounds like you want to do setup that is reused across your test files / processes. Ideally that's avoided since you can end up creating hard-to-detect dependencies between the execution of different tests.

Still, if this is what you need then I'd suggest using a pretest npm script, which is run automatically when you do npm test.

Mark Wubben
  • 3,329
  • 1
  • 19
  • 16
  • Unfortunately that wont work - I need to "pass" data to the test for fixture handling - something I can do with require - well could in Mocha. – cyberwombat Oct 23 '17 at 15:25
  • 1
    If you're passing data then I'd assume that's test file specific. Why are you concerned about running the before hook for each test file? – Mark Wubben Oct 24 '17 at 09:29
  • I setup a database fixture. I need to create the connection once on test start then pass the connection to each test for fixture loading. I create a new db for each test so can run in parallel. Works great except for the lack of a real `before()`. Its not realistic to assume that all tests will never need a presetup. I can patch things to make it work but the use of globals or env is necessary which ironically was one of the things I liked about ava. – cyberwombat Oct 25 '17 at 04:39
  • Your use case sounds like what `test.before()` is meant for. Could you clarify why it doesn't work for you? – Mark Wubben Oct 26 '17 at 09:05
  • Because I need something to run once. Not some random number of times arbitrarily determined by how many files I chose to put my tests in. I don't even see why ava offers this before/after method - per file? Whats the point. – cyberwombat Oct 26 '17 at 15:05
  • It helps set up per-process modifications, stubs and fixtures. And then lets you safely tear them down. – Mark Wubben Oct 27 '17 at 17:07
  • 1
    Ideally - if it ran once. Every other test harness allows for this. This requires checks to see if it already ran which in the case of checking to see if Mongo has been started is a bit annoying. It's fine - I am working around it - just a bit weird imo. – cyberwombat Oct 27 '17 at 17:09
2

In your package.json you could run a setup script first...

"scripts": {
    "test": "node setup-test-database.js && ava '*.test.js'"
}

Then...

  • In that setup-test-database.js file, have it do all your bootstrappy needs, and save a test-config.json file with whatever you need to pass to the tests.
  • In each test you just need to add const config = require('./test-config.json'); and you'll have access to the data you need.
Luke
  • 18,811
  • 16
  • 99
  • 115