You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+34-9Lines changed: 34 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,6 +190,7 @@ All worker code can now use Promises.
190
190
191
191
### Testing
192
192
193
+
## Without Webpack
193
194
To test a module that is normally imported via `workerize-loader` when not using Webpack, import the module directly in your test:
194
195
195
196
```diff
@@ -199,28 +200,52 @@ To test a module that is normally imported via `workerize-loader` when not using
199
200
const instance = worker();
200
201
```
201
202
202
-
To test modules that rely on workerized imports when not using Webpack, you'll need to dig into your test runner a bit. For Jest, it's possible to define a custom `transform` that emulates workerize-loader on the main thread:
203
+
## With Webpack and Jest
204
+
205
+
In Jest, it's possible to define a custom `transform` that emulates workerize-loader on the main thread.
206
+
207
+
First, install `babel-jest` and `identity-object-proxy`:
208
+
209
+
```sh
210
+
npm i -D babel-jest identity-object-proxy
211
+
```
212
+
213
+
Then, add these properties to the `"transform"` and `"moduleNameMapper"` sections of your Jest config (generally located in your `package.json`):
... then add the `workerize-jest.js`shim to your project:
230
+
Finally, create the custom Jest transformer referenced above as a file `workerize-jest.js`in your project's root directory (where the package.json is):
async function asyncify() { return this.apply(null, arguments); }
237
+
module.exports = function() {
238
+
const w = require(${JSON.stringify(filename.replace(/^.+!/, ''))});
239
+
const m = {};
240
+
for (let i in w) m[i] = asyncify.bind(w[i]);
241
+
return m;
242
+
};
243
+
`;
244
+
}
220
245
};
221
246
```
222
247
223
-
Now your tests and any modules they import can use `workerize-loader!` prefixes.
248
+
Now your tests and any modules they import can use `workerize-loader!` prefixes, and the imports will be turned into async functions just like they are in Workerize.
0 commit comments