{"record":{"id":"343d7ce1b7b806ad","repo":"avajs/ava","slug":"all-tests-and-hooks-must-be-declared-synchronously","errorCode":null,"errorMessage":"All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks.","messagePattern":"All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/runner.js","lineNumber":89,"sourceCode":"\t\tthis.notifyTimeoutUpdate = timeoutMs => {\n\t\t\tthis.emit('stateChange', {\n\t\t\t\ttype: 'test-timeout-configured',\n\t\t\t\tperiod: timeoutMs,\n\t\t\t});\n\t\t};\n\n\t\tlet hasStarted = false;\n\t\tlet scheduledStart = false;\n\t\tconst meta = Object.freeze({\n\t\t\tfile: makeFileURL(options.file),\n\t\t\tget snapshotDirectory() {\n\t\t\t\tconst {file, snapshotDir: fixedLocation, projectDir} = options;\n\t\t\t\treturn makeFileURL(determineSnapshotDir({file, fixedLocation, projectDir}));\n\t\t\t},\n\t\t});\n\t\tthis.chain = createChain((metadata, testArgs) => { // eslint-disable-line complexity\n\t\t\tif (hasStarted) {\n\t\t\t\tthrow new Error('All tests and hooks must be declared synchronously in your test file, and cannot be nested within other tests or hooks.');\n\t\t\t}\n\n\t\t\tif (!scheduledStart) {\n\t\t\t\tscheduledStart = true;\n\t\t\t\tprocess.nextTick(() => {\n\t\t\t\t\thasStarted = true;\n\t\t\t\t\tthis.start();\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tmetadata.taskIndex = this.nextTaskIndex++;\n\n\t\t\tconst {args, implementation, title} = parseTestArgs(testArgs);\n\n\t\t\tmetadata.selected &&= this.checkSelectedByLineNumbers?.() ?? true;\n\n\t\t\tif (metadata.todo) {\n\t\t\t\tif (implementation) {","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/avajs/ava/blob/bbfd946322fdeca2b547a691d947fb4e18c0c67f/lib/runner.js#L71-L107","documentation":"Runner chain guard: test declarations (test(), hooks) were attempted after the runner already started executing — the test file declared tests asynchronously (after an await/timer) or nested a declaration inside another test/hook. AVA requires the full test plan to be registered synchronously at module load. The input at fault is the late or nested declaration call.","triggerScenarios":"Thrown at lib/runner.js:89 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Move all test(), test.before/beforeEach/after calls to the top level of the test file","Remove await/dynamic import before test declarations; perform async setup before any declaration or inside hooks","Never declare tests inside another test's implementation; use hooks or serial tests instead"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"bbfd946322fdeca2b547a691d947fb4e18c0c67f","analyzedAt":"2026-09-02T01:24:43.834Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}