Skip to content

Commit 7f649ef

Browse files
authored
Merge pull request #107 from dpraimeyuu/patch-1
Update using workerize-loader within Jest tests
2 parents 7d3e23c + 44817eb commit 7f649ef

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

README.md

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ All worker code can now use Promises.
190190

191191
### Testing
192192

193+
## Without Webpack
193194
To test a module that is normally imported via `workerize-loader` when not using Webpack, import the module directly in your test:
194195

195196
```diff
@@ -199,28 +200,52 @@ To test a module that is normally imported via `workerize-loader` when not using
199200
const instance = worker();
200201
```
201202

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`):
203214

204215
```js
205-
// in your Jest configuration
206216
{
207-
"transform": {
208-
"workerize-loader(\\?.*)?!(.*)": "<rootDir>/workerize-jest.js"
217+
"jest": {
218+
"moduleNameMapper": {
219+
"workerize-loader(\\?.*)?!(.*)": "identity-obj-proxy"
220+
},
221+
"transform": {
222+
"workerize-loader(\\?.*)?!(.*)": "<rootDir>/workerize-jest.js",
223+
"^.+\\.[jt]sx?$": "babel-jest",
224+
"^.+\\.[jt]s?$": "babel-jest"
225+
}
209226
}
210227
}
211228
```
212229

213-
... 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):
214231

215232
```js
216233
module.exports = {
217-
process(src, filename, config, options) {
218-
return 'module.exports = () => require(' + JSON.stringify(filename.replace(/.+!/,'')) + ')';
219-
},
234+
process(src, filename) {
235+
return `
236+
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+
}
220245
};
221246
```
222247

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.
224249

225250
### Credit
226251

0 commit comments

Comments
 (0)