diff --git a/docs/recipes/shared-workers.md b/docs/recipes/shared-workers.md index 976891459..365a8c59d 100644 --- a/docs/recipes/shared-workers.md +++ b/docs/recipes/shared-workers.md @@ -34,6 +34,28 @@ const shared = registerSharedWorker({ Within a test process you can only register one worker for each `filename`. Filenames are compared as-is, without normalization. If you call `registerSharedWorker()` a second time, the same worker instance is returned. +If for some reason you want to load the same file as multiple different workers, you can append a unique hash to the end of the filename: + +```js +import crypto from 'crypto'; +import {registerSharedWorker} from 'ava/plugin'; + +const key = Math.random() > 0.5 ? 'worker-a' : 'worker-b'; + +const shared = registerSharedWorker({ + filename: new URL( + `file:${path.resolve( + __dirname, + 'worker.js' + )}#${encodeURIComponent(key)}` + ), + initialData: {workerKey: key}, + supportedProtocols: ['ava-4'] +}); +``` + +This works because the `filename` parameter accepts [URL](https://nodejs.org/api/url.html) objects, meaning you could use a query component for the key instead if you wanted. + You can supply a `teardown()` function which will be called after all tests have finished. If you call `registerSharedWorker()` multiple times then the `teardown()` function will be invoked for each registration, even though you only got one worker instance. The most recently registered `teardown()` function is called first, and so forth. `teardown()` functions execute sequentially. ```js