Miscellaneous Jest Issues/Workarounds

October 28, 2014

I’ve been using Jest a bit lately and wanted to document some issues I’ve run into for future reference.

Debugging Jest Tests

Recently I had a test that was failing and from looking at the stack trace it wasn’t really clear why. I followed some advice I had seen about running jest in-band and then running node-inspector. Every time I tried to run node-inspector, however, it failed. Similar to the issue I encountered in my previous post, it appears that there is a forthcoming fix.

Until the fix makes it’s way into the release version, following the steps in this stackoverflow post should allow test debugging. Like the solution author, I’m not super thrilled about modifying the jest.js file outright but it’s nice to be able to debug the tests.

Mocking Third-Party Libraries (that aren’t CommonJS modules)

Another issue I’ve encountered is testing components that wrap third-party libraries that are not CommonJS modules. I tried a couple different hacks to try to shim the library in question into something that would load as a CommonJS module but ultimately was unsuccessful in the time I wanted to spend on that.

Thankfully, a post in the React Google Group led me to use Manual mocks as a way to work around these third-party libraries.

Creating a manual mock is pretty simple. Just create a new folder called __mocks__ at the same level as the __tests__ directory and create a CommonJS module with the same name/properties as the third-party module that is being mocked. Adding var someModule = require('moduleName') will cause someModule to get replaced with the mock when running through jest. Finally, set the third-party library as an external module in webpack.config.js and everything should be good-to-go for both the test and the “compiled” version of the code.


Ryan Lanciaux

Hi 👋 I'm Ryan Lanciaux. I run Spaceship Studio, LLC. a consultancy specializing in fast and dynamic web and native mobile applications.

I live in Ann Arbor with my wonderful family! In my freetime, I create electronic music.

You should follow me on Twitter