What makes a good unit test?

Sorry if this seems dumb, but I’m trying to wrap my head around unit testing in JS, but I can’t think of what to test! Looking at my scripts, they pull data from an API or local database, they display content, they listen for clicks and they open and close divs, and uh…that’s about it. I could understand if we need to test that a value is in a certain range, for example, but I don’t have that. Maybe I need examples of what to assert…and why.

look at the jQuery/jQueryUI repository at github. there are extensive unit tests attached.

all those things you can check with a unit test.

Really? Wouldn’t you stub out the API call and use some kind of integration test for opening/closing divs?

sure, the API should be stubbed/mocked since you don’t want to test the API itself but the code processing the API result.

opening/closing divs are not subject to integration tests, if the trigger is independent (cf. jQueryUI Dialog /1/, they use unit tests to determine if an open/close action works)

/1/ - I had a closer look at them for ideas how I could test my jQueryUI plugin

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.