This is fugly. You have to clutter up your tests with lots of requirejs
boilerplate in addition to just defining your mocks.

Loading Mock Dependencies From Different Paths

This involves using a separate require.config to define paths for each of the
dependencies that point to mocks instead of the original dependencies. This is
also fugly, requiring the creation of tons of test files and configurations
files.

Fake It In Node

This is my current solution, but is still far from ideal.

You create your own define function to provide your own mocks to the module
and put your tests in the callback. Then you eval the module to run your
tests, like so:

You get to use eval in anger, and imagine Crockford’s disapproving glare.

It still has some drawbacks, obviously.

Since you are testing in node, you can’t do anything with browser events or
DOM manipulation. Only good for testing logic.

Still a little clunky to set up. You need to mock out define in every
test, since that is where your tests actually run.

I am working on a test runner to give a nicer syntax for this kind of stuff,
but I still have no good solution for problem 1.

Conclusion

Mocking deps in requirejs sucks hard. I found a way that sortof works, but am
still not very happy with it. Please let me know if you have any better ideas.

Jamison cares about family and programming and React Rally and Soft Skills Engineering and 🏋️ and 🏂 and computing and business and the Dunning-Kreuger effect. He is a real human bean who you can reach on Twitter.