Javascript error collection for RSpec

This is an extension of Alessandro Rodi’s Article about Catch Javascript errors in your system tests

Proper testing of a rails application includes system tests, which are meant to test the app as a complete entity, verify the functionality and find miscommunications between components.

By default rspec does not care about javascript errors so we have to configure it to do so. Previously we did this directly in the rails helper, which made it look a bit messy and is also quite annoying to do for every project.

Let’s use a more modern approach and extract it into its own module. It will loop over all errors and bundle them up to fail together.

Now we can just include the module and assign the collect_errors function to every config hook we want. In our example it will be executed after every system test that has js set to true.

Here’s an example system test. We want to test the functionality of getting redirect to the login page if the user is not logged in. The test fulfils all the criteria set in our after hook. Therefore collect_errors will be executed afterwards.

The test would normally succeed but now it will look for javascript errors and warnings and fails if there are any.

Now no failing code will be missed by our tests. Though one important thing to note is that you can’t give back error codes from the server for expected behaviour.

Hopefully you found this helpful!

--

--