Skip to content
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

Closed
wbobeirne opened this issue Jul 30, 2018 · 7 comments
Closed

Unit testing functions that use workerize'd imports #48

wbobeirne opened this issue Jul 30, 2018 · 7 comments
Labels
question Further information is requested

Comments

@wbobeirne
Copy link

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.

@developit
Copy link
Owner

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>

@developit developit added the question Further information is requested label Aug 23, 2018
@wbobeirne
Copy link
Author

wbobeirne commented Aug 23, 2018

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.:

import { foo } from 'workerize-loader!./a'

function async iWantToUnitTestThis() {
  const number = await foo();
  return foo * 2;
}

I hacked around this by using mock-require and mocking the worker for each of those files before running my tests:

function makeWorkerFactory(module) {
  return () => module
}

const workers = ['list', 'of', 'worker.js', 'files']
workers.forEach(worker => {
  const source = require(worker)
  return mock(path, makeWorkerFactory(source))
})

But obviously this isn't ideal.

@developit
Copy link
Owner

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:
https://github.com/developit/jsdom-worker

@danreeves
Copy link

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.

@serdaroquai
Copy link

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 jest.config.js or if you are using create-react-app to package.json.

...and supply a mock worker at the path you described

module.exports = () => require('./some.worker');

Reference: https://twitter.com/james_k_nelson/status/1175259347422564357

@developit
Copy link
Owner

developit commented Feb 21, 2020

I added a variant of that solution to a new testing section in the readme. Cheers!

@gangsthub
Copy link

gangsthub commented Aug 17, 2020

The transform wasn't working for me either (as in #103). This is the error I was getting:

Cannot find module 'workerize-loader!~/core/referenceStyler'

image

I opted for using the moduleNameMapper as @serdaroquai suggested.

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! 🙌 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants