-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit testing functions that use workerize'd imports #48
Comments
If you export async functions from the module you're workerizing, they are callable without workerize involved: export async function foo() {
return 42;
} import { foo } from 'workerize-loader!./a'
foo() // Promise<42> import { foo } from './a'
foo() // Promise<42> |
Yeah, that works like a charm for testing the worker functions themselves. The issue I have is that the function I want to unit test is in a file that includes that worker, or a file that includes that file etc. etc.:
I hacked around this by using mock-require and mocking the worker for each of those files before running my tests:
But obviously this isn't ideal. |
Is the issue that your running your test directly in Node so there is no Worker support? If so, just drop jsdom-worker in there: |
The issue is that workerize-loader changes the shape of the module. You can't use a module that imports a workerized module in jest, for example, because the default export doesn't exist unless you've run it through webpack first. |
an alternative way is to tell jest to map the module to a mock implementation "moduleNameMapper": {
"workerize-loader!../../worker/some.worker":"<rootDir>/src/worker/some.worker.mock.js"
} to ...and supply a mock worker at the path you described module.exports = () => require('./some.worker'); Reference: https://twitter.com/james_k_nelson/status/1175259347422564357 |
I added a variant of that solution to a new testing section in the readme. Cheers! |
The
I opted for using the Although I noticed it might be a little bit slow, the test is taking now 5s. Not sure where the problem is yet. Anyway, thank you for this great library! 🙌 ❤️ |
Has anyone come up with a way to write unit tests (e.g. jest, mocha) for functions that user workerize-loader imports? It'd be great if there was an easy way to replace the default workerize-loader behavior with a simple promise wrapper when in a testing environment. Any advice would be appreciated.
The text was updated successfully, but these errors were encountered: