hotwired / stimulus

A modest JavaScript framework for the HTML you already have
https://stimulus.hotwired.dev/
MIT License
12.62k stars 418 forks source link

Testing story? #34

Closed henrik closed 6 years ago

henrik commented 6 years ago

Do you have any thoughts on testing Stimulus controllers? Do you tend to unit test them in part or whole? Or rely on integrated feature tests that incorporate JS?

javan commented 6 years ago

We hope to provide a baked-in solution for 1.0 that may look something like https://github.com/stimulusjs/stimulus/pull/7. And, of course, you can use your application's existing test rig. We write high level feature tests using http://guides.rubyonrails.org/testing.html#system-testing.

henrik commented 6 years ago

@javan, thank you very much!

sstephenson commented 6 years ago

Quick update on testing.

We introduced the @stimulus/test package in version 1.0.0: https://github.com/stimulusjs/stimulus/pull/58

It’s currently undocumented but used internally by other @stimulus packages. You can review their test suites here:

You can use this library to test your applications right now. The API is stable. What we haven’t done yet is polish the experience around setting up and invoking Karma.

borislemke commented 5 years ago

@sstephenson It's been a while since this was posted, I was wondering if there was an update to the testing's suite documentation? I could not find a reference to it in your repo / handbook site.

nogtini commented 5 years ago

Happy new year! Any updates on this? It looks like there was a planned test harness for 1.1 that didn't make the cut (https://discourse.stimulusjs.org/t/what-s-planned-for-stimulus-1-1/144).

shime commented 5 years ago

I've recently written an article that explains how to write high-level tests of Stimulus controllers. I didn't use @stimulus/test and I used Jest instead of Karma.

nogtini commented 5 years ago

This is very, very much appreciated @shime. Do you have any experience testing them through RSpec?

shime commented 5 years ago

@nogtini I'm not 100% sure what do you mean by that. Before we started using Jest for this, we used to test Stimulus controllers through ordinary Rails system tests, so that's another approach you could take.

nogtini commented 5 years ago

@shime Sure, I mean only in the context of integration tests. Seeing stimulus controllers tested through something like Capybara I think would help for people who are newer to Rails overall, like me. I come from a JS background, so seeing how these things are tested within the context of an entire working application is often difficult to discern.

davidenglishmusic commented 5 years ago

Copying the component in as a fixture instead of directly testing app code doesn't bode well for catching regressions. It seems like it would be better to only test functions which transform data, leaving others that do UI interactions to the system tests.