{"record":{"id":"498ee73076d5f7e5","repo":"avajs/ava","slug":"shared-workers-can-be-used-only-when-worker-thread","errorCode":null,"errorMessage":"Shared workers can be used only when worker threads are enabled","messagePattern":"Shared workers can be used only when worker threads are enabled","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/worker/plugin.js","lineNumber":95,"sourceCode":"\t\t\treturn publishMessage(data);\n\t\t},\n\n\t\tasync * subscribe() {\n\t\t\tyield * receiveMessages();\n\t\t},\n\t};\n}\n\nexport function registerSharedWorker({\n\tfilename,\n\tinitialData,\n\tsupportedProtocols,\n\tteardown,\n}) {\n\tconst options_ = getOptions();\n\n\tif (!options_.workerThreads) {\n\t\tthrow new Error('Shared workers can be used only when worker threads are enabled');\n\t}\n\n\tif (!supportedProtocols.includes('ava-4')) {\n\t\tthrow new Error(`This version of AVA (${pkg.version}) does not support any of the desired shared worker protocols: ${supportedProtocols.join(',')}`);\n\t}\n\n\tfilename = String(filename); // Allow URL instances.\n\n\tlet worker = workers.get(filename);\n\tif (worker === undefined) {\n\t\tworker = createSharedWorker(filename, initialData, async () => {\n\t\t\t// Run possibly asynchronous teardown functions serially, in reverse\n\t\t\t// order. Any error will crash the worker.\n\t\t\tconst teardownFns = workerTeardownFns.get(worker);\n\t\t\tif (teardownFns !== undefined) {\n\t\t\t\tfor await (const fn of [...teardownFns].toReversed()) {\n\t\t\t\t\tawait fn();\n\t\t\t\t}","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/avajs/ava/blob/bbfd946322fdeca2b547a691d947fb4e18c0c67f/lib/worker/plugin.js#L77-L113","documentation":"AVA's shared worker registration API (registerSharedWorker in lib/worker/plugin.js) requires Node.js worker_threads, which AVA disables in certain execution modes. When the resolved AVA options have workerThreads set to a falsy value, calling registerSharedWorker immediately throws this Error. AVA disables worker threads when running in environments where they cannot be used, most commonly when the precompile/`node:` worker mode is off or under test concurrency settings that force in-process execution.","triggerScenarios":"Calling t.registerSharedWorker() (which reaches registerSharedWorker) from a test file while AVA was configured or forced to run without worker threads — e.g. running AVA with worker threads disabled via configuration, or in an environment where worker_threads are unavailable/unsupported.","commonSituations":"Running AVA on a Node.js build without worker thread support, using a config that disables worker threads (older AVA defaults or CI constraints), running tests in restricted containers/runtimes where worker_threads are blocked, or using shared workers with AVA versions/modes that predate worker-thread-based test execution.","solutions":["Enable worker threads for AVA (upgrade to a modern AVA version where worker_threads is on by default, and remove any config that disables it).","Verify the runtime supports worker_threads: require('node:worker_threads') in the same Node version AVA uses; upgrade Node.js if it is missing.","If worker threads genuinely cannot be enabled, remove the shared worker and communicate via another mechanism (files, a local server) instead of t.registerSharedWorker()."],"exampleFix":"// before\ntest('share state', t => {\n  t.registerSharedWorker({ filename: './my-worker.js', supportedProtocols: ['ava-4'] });\n});\n\n// after — ensure AVA runs with worker threads enabled (modern AVA default)\n// ava.config.js: no `workerThreads: false`; Node >= 12; then register as before","handlingStrategy":"validation","validationCode":"import { worker_threads as hasWorkerThreads } from 'node:process';\n// or: const hasWorkerThreads = (() => { try { require('node:worker_threads'); return true; } catch { return false; } })();\nif (!hasWorkerThreads) {\n  console.warn('Skipping shared-worker registration: worker threads unavailable');\n} else {\n  t.registerSharedWorker({ filename, supportedProtocols: ['ava-4'] });\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only use shared workers on modern Node.js (>=12.11) where worker_threads are stable.","Never set workerThreads: false in AVA config if shared workers are used.","Feature-detect worker_threads availability in CI before enabling shared-worker tests."],"tags":["ava","worker-threads","shared-worker","configuration"],"backgroundTag":"worker-threads-disabled","analyzedSha":"bbfd946322fdeca2b547a691d947fb4e18c0c67f","analyzedAt":"2026-09-02T01:24:43.834Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}