avajs/ava

[bug] Cannot add hooks through require config

c-vetter opened this issue · 0 comments

With AVA 5.2.0 on Windows, I just ran into pretty much this issue: #1944
…the only difference being that I don't want to setup something like a database but prepare the worker's state.

I have this config:

{
        "extensions": {
            "ts": "module",
            "tsx": "module"
        },
        "nodeArguments": [
            "--loader=tsx",
            "--require=global-jsdom/register"
        ],
        "require": [
            "./src/setupTests.ts"
        ]
    }

In setupTests.ts, I have this, basically:

import ava from "ava"
import { setup, teardown } from "..."

ava.before(() => setup())
ava.after(() => teardown())

If I read #1944 (comment) correctly, that's the correct approach for worker-global setup. However, running either npx ava or npm test (the script being ava --watch), I get this Error for each test file:

Error: All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks.

That does not happen if I remove the require config and add this to my test files (which is what I'm going with now):

import "../../setupTests"

This looks like a bug with the require config to me. Otherwise, I misunderstand how require and workers relate. In any case, some more documentation on what require is supposed to do or how it works would be nice. Maybe a recipe with a real use-case.