Skip to content

Commit

Permalink
Merge pull request #107 from dpraimeyuu/patch-1
Browse files Browse the repository at this point in the history
Update using workerize-loader within Jest tests
  • Loading branch information
developit authored Jan 9, 2021
2 parents 7d3e23c + 44817eb commit 7f649ef
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ All worker code can now use Promises.

### Testing

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

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

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:
## With Webpack and Jest

In Jest, it's possible to define a custom `transform` that emulates workerize-loader on the main thread.

First, install `babel-jest` and `identity-object-proxy`:

```sh
npm i -D babel-jest identity-object-proxy
```

Then, add these properties to the `"transform"` and `"moduleNameMapper"` sections of your Jest config (generally located in your `package.json`):

```js
// in your Jest configuration
{
"transform": {
"workerize-loader(\\?.*)?!(.*)": "<rootDir>/workerize-jest.js"
"jest": {
"moduleNameMapper": {
"workerize-loader(\\?.*)?!(.*)": "identity-obj-proxy"
},
"transform": {
"workerize-loader(\\?.*)?!(.*)": "<rootDir>/workerize-jest.js",
"^.+\\.[jt]sx?$": "babel-jest",
"^.+\\.[jt]s?$": "babel-jest"
}
}
}
```

... then add the `workerize-jest.js` shim to your project:
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):

```js
module.exports = {
process(src, filename, config, options) {
return 'module.exports = () => require(' + JSON.stringify(filename.replace(/.+!/,'')) + ')';
},
process(src, filename) {
return `
async function asyncify() { return this.apply(null, arguments); }
module.exports = function() {
const w = require(${JSON.stringify(filename.replace(/^.+!/, ''))});
const m = {};
for (let i in w) m[i] = asyncify.bind(w[i]);
return m;
};
`;
}
};
```

Now your tests and any modules they import can use `workerize-loader!` prefixes.
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.

### Credit

Expand Down

0 comments on commit 7f649ef

Please sign in to comment.