Skip to content

Commit bc09985

Browse files
authored
Add Jest testing example (fixes #48)
1 parent e767b7b commit bc09985

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,40 @@ If you're using [Babel](https://babeljs.io/) in your build, make sure you disabl
8585
}
8686
```
8787

88+
### Testing
89+
90+
To test a module that is normally imported via `workerize-loader` when not using Webpack, import the module directly in your test:
91+
92+
```diff
93+
-const worker = require('workerize-loader!./worker.js');
94+
+const worker = () => require('./worker.js');
95+
96+
const instance = worker();
97+
```
98+
99+
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:
100+
101+
```js
102+
// in your Jest configuration
103+
{
104+
"transform": {
105+
"workerize-loader(\\?.*)?!(.*)": "<rootDir>/workerize-jest.js"
106+
}
107+
}
108+
```
109+
110+
... then add the `workerize-jest.js` shim to your project:
111+
112+
```js
113+
module.exports = {
114+
process(src, filename, config, options) {
115+
return 'module.exports = () => require(' + JSON.stringify(filename.replace(/.+!/,'')) + ')';
116+
},
117+
};
118+
```
119+
120+
Now your tests and any modules they import can use `workerize-loader!` prefixes.
121+
88122
### Credit
89123

90124
The inner workings here are heavily inspired by [worker-loader](https://github.com/webpack-contrib/worker-loader). It's worth a read!

0 commit comments

Comments
 (0)