-
Notifications
You must be signed in to change notification settings - Fork 20
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
Testing Worker in Jest #14
Comments
I’m running into this issue as well with: const worker = new Worker(new URL('../utils/worker.ts', import.meta.url)) Here, in a development environment, the call to Mocking the worker is possibly an option, but I think I can’t turn my worker file into a module because Firefox doesn’t support it, yet (see bug https://bugzilla.mozilla.org/show_bug.cgi?id=1247687; for what it’s worth, this seems to very much be in active development). I’m not sure how to proceed now. |
I’m now experimenting with a createWorker.ts: export function createWorker(url, onMessageHandler) {
const worker = new Worker(url)
worker.onmessage = onMessageHandler
return worker
} In my test: jest.mock('@/utils/createWorker', () => {
const { readFileSync } = require('fs')
const { resolve } = require('path')
const code = readFileSync(resolve(__dirname, '../utils/worker.js'), 'utf8')
const workerUrl = URL.createObjectURL(new global.Blob([code]))
return {
__esModule: true,
createWorker(_url, onMessageHandler) {
const worker = new global.Worker(workerUrl)
worker.onmessage = onMessageHandler
return worker
},
}
}) |
Hi,
I'm trying to test my worker inside a Vue.js project. I load the worker inside my component using the worker-plugin (I also tried loading my Worker using Webpack worker-loader, unfortunately it doesn't provide a compatible interface and I had to mock the worker in that case)
But when I run my tests with Jest I get
The text was updated successfully, but these errors were encountered: