create-react-app-typescript-web-worker-setup
create-react-app-typescript-web-worker-setup copied to clipboard
Jest test cases are failing
Hey @dominique-mueller , great work on the package. And it's really of good help for CRA.
- I am using CRA, TypeScript, Craco (To override webpack) and Jest.
- I could achieve working with Comlink seamlessly by your workaround.
- My class that will use this syntax
worker-loader!../Somefolder/worker.tswill be exported initially with the app.
So, while running Jest tests, all my tests are getting failed because of this.
I am getting this error while running Jest tests
Cannot find module 'worker-loader!../Somefolder/worker.ts' from 'src/xxx/xxx/xxx/xxx/index.ts'
I have followed this solution. This has resolved this issue, but I got another issue like this
TypeError: ep.addEventListener is not a function
The above issue is reported in Comlink, but I think that's for another reason.
How could I get this working? Any ideas?
Regarding the broken imports: Jest within CRA does not rely on Webpack, thus the Webpack-specific loader syntax won't work. Now, when you use the exact webpack override config (README) you don't actually need the worker-loader! prefix as long as the worker file ends with .worker.ts|js. That at least might solve the issue of broken imports in Jest.
I am not sure when it comes to testing, however. This repo does explicitely not contain that aspect, and I haven't had the chance to explore it deeper. Because Jest runs on jsdom and not within an actual browser, Workers itself won't work and chances are that related APIs might not be available. Jest requires lots of mocking, and so might your Worker usage. You can see in the App.test.tsx file how I've simply mocked away the workers so that the tests at least pass (https://github.com/dominique-mueller/create-react-app-typescript-web-worker-setup/blob/main/src/App.test.tsx#L5). But that of course is not a real solution.