A common question I get is how to test JavaScript in Rails applications. My approach is almost radically simple and unsophisticated.
My Rails + JavaScript testing approach
I think of the fact that the application uses JavaScript like an inconsequential and irrelevant implementation detail. I test JavaScript-heavy applications using just RSpec + Capybara integration tests, the same exact way I'd test an application that has very little JavaScript or no JavaScript at all. I don't really have anything more to say about it since I literally don't do anything different from my regular RSpec + Capybara tests.
Single-page applications
What about single-page applications? I still use the same approach. When I used to build Angular + Rails SPAs, I would add a before(:all)
RSpec hook that would kick off a build of my Angular application before the test suite ran. After that point my RSpec + Capybara tests could interact with my SPA just as easily as if the application were a "traditional" Rails application.
Side note: traditional Rails applications are fine. Using Rails with React/Vue/Angular/etc. isn't "modern" and using Rails without any of those isn't "outdated". For most regular old boring business applications, Rails by itself without a front-end framework is not only a sufficient approach but a superior approach to an SPA because the complexity of development with plain Rails and only "JavaScript sprinkles" tends to be far lower than Rails with a JavaScript framework.
Testing JavaScript directly
Despite my typical approach of treating JavaScript as a detail, there are times when I want to have a little tighter control and test my JavaScript directly. In those cases I use Jasmine to test my JavaScript. But it's my goal to use such little JavaScript that I never get above that threshold of complexity where I feel the need to test my JavaScript directly with Jasmine. I've found that if I really try, I can get away with very little JavaScript in most applications without sacrificing any UI richness.
Top comments (1)
Your side note could be an article by itself! Devs tend to be early adopters and we sometimes get reflexively gravitate to the trendy while ignoring the pragmatic. I know two small-to-medium sized companies that lost a 8 months to a year replacing their rails front-end with an SPA (both Angular) with zero benefit to the end-user.
Having said that html-over-websockets is the new black, and rails seems perfectly positioned to do be the platform for it.